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

Please show the code for the following problems in beginner MATLAB code. Please

ID: 3847582 • Letter: P

Question

Please show the code for the following problems in beginner MATLAB code. Please be sure to include well detailed comments so that I may be able to understand all functions and processes. Thanks in advance. Please read instructions carefully and make sure that programs run properly.

Problems In this lab, we will use MATLAB to implement the Greatest Common Divisor (GCD) and the Least Common Multiple (LCM). Please carefully read and follow the descriptions of each problem. 1. Prime Factorization of a Number (25 points Write a function that takes as a parameter a positive integer and returns a list (array or vector in MATLAB) of the prime factors of the given integer. For example, if the parameter is 20, you should return 2 2 5. 2. GCD (25 points) Write a function that takes as a parameter a list (array or vector in MATLAB) of 2 or more positive integers. Return the GCD of all elements in the list. For example, the GCD of 10 20 30 is 10 Note: Implement the Euclidean algorithm function for computing the greatest common divi- sor of two integers as explained in CSC 1500 class note. Call this function EuclidGCD which is called in the function GCD 3. LCM (25 Points) Write a function that takes as a parameter a list (array or vector in MATLAB) 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 4. Functions Tester (25 points) 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: 1) Find the prime factorization of a number 2) Find the GCD of a series of numbers 3) Find the LCM of a series of numbers 4) Quit Your function should continuously display the menu until the quit option is selected.

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
%}