Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LCM Write a function that takes as a parameter a list (array or vector in MATLAB

ID: 3822095 • Letter: L

Question

LCM Write a function that takes as a parameter a list (array or vector in MATLAB/C++) of 2 or more positive integers. Return the LCM of all elements in the list. For example, the LCM of 10 20 30 is 60. Functions Tester Write a function that tests the above functions by asking the user for input and displaying the output to screen. Your function should display the following menu: Find the prime factorization of a number Find the GCD of a series of numbers Find the LCM of a series of numbers Quit Your function should continuously display the menu until the quit option is selected. Have fun and ensure your functions are well commented.

Explanation / Answer

%mtlab code

function primefactors = prime(n)
primefactors = [];
i = 1;
for j=2:n
while(true)
if(mod(n,j) == 0)
primefactors(i) = j;
i = i + 1;
n = n/j;
else
break;
end
end
end
end

function b = euclidGCD(a,b)
while ( a != 0 )
c = a;
a = mod(b,a);
b = c;
end
end

function result = arrayGcd(array)
result = euclidGCD(array(1), array(2));
for i=3:length(array)
result = euclidGCD(result, array(i));
end
end

function result = arraylcm(array)
result = array(1);
for i=2:length(array)
result = ( ((array(i)*result))/(euclidGCD(array(i), result)) );
end
end

% fnction tester
while true
fprintf(' 1.Prime factorizaion 2.GCD 3.LCM 4.Quit ');
choice=input('Enter your choice: ');
  
if choice == 1
n = input('Enter a number: ');
primefactors = prime(n)
elseif choice == 2
array = input('Enter array of positive number: ');
result = arrayGcd(array)
elseif choice == 3
array = input('Enter array of positive number: ');
result = arraylcm(array)
else
break;
end
end


%{
output:


1.Prime factorizaion
2.GCD   
3.LCM   
4.Quit
Enter your choice: 1
Enter a number: 20
primefactors =   
2 2 5

1.Prime factorizaion
2.GCD   
3.LCM   
4.Quit
Enter your choice: 2
Enter array of positive number: [10 20 30]
result = 10

1.Prime factorizaion
2.GCD   
3.LCM
4.Quit
Enter your choice: 2
Enter array of positive number: [10 20 30]
result = 10

1.Prime factorizaion
2.GCD   
3.LCM   
4.Quit
Enter your choice: 3
Enter array of positive number: [10 20 30]
result = 60

1.Prime factorizaion
2.GCD   
3.LCM   
4.Quit
Enter your choice: 4
%}