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

Write a function named greatestCommonDivisor which takes in two inputs and retur

ID: 3567878 • Letter: W

Question

Write a function named greatestCommonDivisor which takes in two inputs and returns the greatest common divisor of the inputs. The greatest common divisor for two numbers is a number which divides both the numbers with a remainder of 0.

For example the greatest common divisor of 12 and 16 is 4 since 4 is the largest number that perfectly divides both 12 and 16. You could find the remainder of when two numbers are divided by using the inbuilt MATLAB function rem.

For example when 5 is divided by 3 the remainder is 2. Hence

To find the greatest common divisor using two numbers the following (somewhat rudimentary) algorithm can be used

Assume we are two find the greatest common divisor between numbers a and b

Decide which of the two numbers out of a or b is smaller

Go from 1 upto the smaller number and in each step check if each number divides a and b without a remainder

The largest number to divide both a and b is the greatest common divisor between a and b

Your function greatestCommonDivisor should be able to accept scalars, vectors and matrices. In the case of vectors and matrices it the function should return the greatest common divisor of corresponding elements

For example,

Do not use the input MATLAB function gcd

Explanation / Answer

%you can use this function GCD

function GCD(a, b)
% Main concept
r = a - b * floor( a/b );

% Repeat the operation until updates of ' a ' equal updates of ' b '
while r ~= 0
a = b;
b = r;
% This is the main concept iterated
r = a - b * floor( a/b );
end

% Show result
b

%%

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