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

Write a MATLAB function M-file named gcd_lcm that accepts two positive integers,

ID: 3768343 • Letter: W

Question

Write a MATLAB function M-file named gcd_lcm that accepts two positive integers, and computes their greatest common divisor and least common multiple. You may not use MATLAB built-in functions gcd, 1cm or rem. No credit will be given if these, or any similar functions, are used. Please ask if you are not sure. The function should return a red error message for the following conditions. Any input is not positive Any input is not an integer Write a MATLAB script GCD_LCMtable to print a table of 10 randomly-generated numbers, and their gcd & 1cm, using your function GCD_LCM. Here is the code to use for generating random numbers: You must use function gcd lcm.

Explanation / Answer


function y=gdc_lcm(x)
n=length(x);
if n<2
y=x;
elseif n==2
y=lcm(x);
else
y=lcm(x(1:2));
for i=3:n
    y=lcm(x(i),y);
end
end


function GCD(a, b)
r = a - b * floor( a/b );
while r = 0
a = b;
b = r;
r = a - b * floor( a/b );
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote