Using MatLab WHILE-LOOP: Using a while-loop, determine the greatest common divis
ID: 3685108 • Letter: U
Question
Using MatLab
WHILE-LOOP: Using a while-loop, determine the greatest common divisor (GCD) of two numbers, m and n. GCD = n when mod(m, n) = 0.
If mod(m, n) ~=0 then reassign values to m and n as follows and try again:
temp = m
m=n
n = mod(temp, n)
The above equations use a temp variable in order to preserve the original value of m, which is needed to recalculate and reassign the variable n.
Explanation / Answer
function GCD= GCD(n); x=1; p=n; while(size(n,2))>=2 p= n(:,size(n,2)-1:size(n,2)); n=n(1,1:size(n,2)-2); x=1; while(x~=0) x= max(p)-min(p); p = [x,min(p)]; end n=[n,max(p)]; p= []; end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.