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

The Euclidean algorithm for the GCD (greatest common divisor) of two positive in

ID: 3550851 • Letter: T

Question

The Euclidean algorithm for the GCD (greatest common divisor) of two

positive integers is the world's oldest surviving non-trivial algorithm. It

has many theoretical and practical applications. For example, it is used in

the public key encryption system that is used to secure electronic commerce.

It has a short recursive implementation based on the following two facts.


gcd(a; b) =    {a    if b = 0;

                     {gcd(b; a%b)     otherwise.


Use the above fact to write a recursive method in Java that computes and

returns the gcd of two positive integers.

Explanation / Answer

int gcd(int m,int n) { if(n==0) return m; else if(n>m) return gcd(n,m); else return gcd(n,m%n); }

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