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

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

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