Question
Consider the problem of finding the greatest common divisor (gcd) of two positive integers a and b. The algorithm presented here is a variation of
Consider the problem of finding the greatest common divisor (gcd) of two positive integers a and b. The algorithm presented here is a variation of Euclids algorithm, which is based on the following theorem:
THM: If a and b are integers with a > b such that b is not a divisor of a, then gcd(a, b) = gcd(b, a mod b).
This relationship between gcd(a, b) and gcd(b, a mod b) is the heart of the recursive solution. It specifies how you can solve the problem of computing gcd(a, b) in terms of another problem of the same type. Also, if b does divide a, then b = gcd(a, b), so an appropriate choice for the base case is (a mod b) = 0.
This theorem leads to the following recursive definition:
gcd(a, b) = b if (a mod b ) = 0
= gcd (b, a mod b) otherwise
Write a complete C++ program to implement this recursive definition.
Test Data: 1) gcd (100, 300)
2) gcd (1, 101)
3) gcd (5, 11)
4) gcd (301, 11)
5) gcd (16, 64)
Requirements: 1) Use these five test data above for your program.
2) Please turn both your source code and the output.
Note: Use screen print to capture the output according to the specified test data.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started