Greatest Common Divisor using the Euclidean Algorithm
Below is an example of how to create the recursive function of computing the greatest common divisor using the Euclidean Algorithm.
* This tutorial assumes you understand the following.
Euclid’s Algorithm
When studying computer science a famous algorithm taught to students is the Euclidean Algorithm which is used to compute the greatest common divisor of two integers. It’s definition is below.
![]()
Computing various examples
So how does it work? Well, let’s follow the definition, but before doing that, do you remember how to compute the remainder of two integers in c++? If you don’t, well it’s the % (mod) operator.
gcd(45, 9)
= gcd(9, 45 % 9 ) = gcd(9, 0)
= 9
// Computing the gcd of 54 and 12 //
gcd(54, 12)
= gcd(12, 54 % 12) = gcc(12, 6)
= gcd(6, 12 % 6) = gcd(6, 0)
= 6


