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

Then use this function to calculate the common divisors of random integer pairs.

ID: 3628521 • Letter: T

Question

 

Then use this function to calculate the common divisors of random integer pairs. Use the " rand" function to generate 100 sample pairs, calculate their greatest command divisors, and plot all these divisors against their indexes. The figure should mark each divisor by "blue circle" and no line connects them. Put a reasonable title to your figure. And do not show axes. Put everything you write inside a script file. A sample script is here. Write comments.

 

[code]

 

% Clears screen and deletes all the variables in the workspace
clear; clc

% Asks the user for input and takes only positive numbers into account
a = input('First number: ');
b = input('Second number: ');
a = abs(a);
b = abs(b); 

 % This is the real trick, normally performed a number of times
r = a - b*floor(a/b); 

% Repeats the operation until updates of a equal updates of b
while r ~= 0
    a = b;
    b = r;
    r = a - b*floor(a/b);
end 

% Displays the result
GCD = b

 

 

[/code]

 

 

 

 

 

PLEASE HELP IN MATLAB

 

Thank You

Explanation / Answer

function gcd = mygcd(lhs, rhs)
r = a - b*floor(a/b);

while r ~= 0
    a = b;
    b = r;
    r = a - b*floor(a/b);
end

% Displays the result
gcd = b


return

for i = 1:100

d1=1+fix(100*rand);
d2=1+fix(100*rand);

     res = mygcd(d1,d2);
     res_array = [res_array, res];
end

plot(res_array,'bo',);