Solution Review: Find the Greatest Common Divisor
Explore how to implement and understand a recursive approach to find the greatest common divisor (GCD) of two numbers. This lesson guides you through the algorithm that reduces the problem by repeatedly subtracting smaller values until both numbers are equal, revealing the GCD. By the end, you will grasp the recursive logic and see step-by-step examples that illustrate the process clearly.
We'll cover the following...
Solution: Using Recursion
Explanation
The naive approach to finding of numbers is to list all their divisors. Then pick the common divisors, and then select the greatest out of them.
However, an easy mathematical simplification can make our task easier.
The idea behind calculating is: If , is the same as .
This is because if and both leave no remainder, then leaves no remainder, either.
This leads to the following algorithm:
...