Solution Review: Greatest Common Divisor
Let’s take a detailed look at the previous challenge’s solution.
We'll cover the following
Solution
There are many ways to find the GCD of two integers. We’ll use Euclid’s algorithm to calculate the GCD:
-
If
n = 0
thenGCD(n, m) = m
. This is a termination condition. -
If
m = 0
thenGCD(n, m) = n
. This is a termination condition. -
Write
n
in the form of a quotient remainder .q
is the quotient, andr
is the remainder. -
Since
GCD(N,M) = GCD(M,R)
, use the Euclidean Algorithm to findGCD(M,R)
.
Coding exercise
Get hands-on with 1400+ tech skills courses.