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

in Matlab For two integers m and n, their GCD(Greatest Common Divisor) can be co

ID: 3684060 • Letter: I

Question

in Matlab For two integers m and n, their GCD(Greatest Common Divisor) can be computed by a recursive function. Write a recursive function gcd(m,n) to find their Greatest Common Divisor. Once m is 0, the function returns n. Once n is 0, the function returns m. If neither is 0, the function can recursively calculate the Greatest Common Divisor with two smaller parameters. One is n, the second is m mod n (Hint: m mod n can be computed by function mod(m, n ) in MATLAB). There are various ways to calculate Greatest Common Divisor in Mathematics. However, please follow the description. Otherwise no credit will be offered.

Explanation / Answer

Function:

function result = GCD( m,n )
if(n~=0)
result= GCD(n,mod(m,n));
else
result = m;
end

end

Result:

>> GCD(25,2)

ans =

1

>> GCD(25,10)

ans =

5

>> GCD(82,4)

ans =

2