C++ Program to Find the GCD of Two Numbers

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The GCD (Greatest Common Divisor) of two numbers is the highest common number dividing them without leaving any remainder. GCD is also known as HCF (Highest Common Factor).

What is GCD (Greatest Common Divisor)?

As the name explains, the GCD (Greatest Common Divisor) of two numbers is the greatest positive integer that divides the numbers without leaving any remainder.
GCD is also known as HCF (Highest Common Factor).

As we have studied in school, We can find the GCD of two numbers by multiplying their common prime factors.

In this example, we will find GCD of 140 & 240,

Prime factors of 140: 2, 2, 5, 7

Prime factors of 270: 2, 3, 3, 3, 5,

Common Prime factors of 140 & 270: 2, 5

GCD(140,270) = Product of the common factors = 2 * 5 = 10

Algorithms to Find the GCD of Two Numbers

There are many algorithms for finding the GCD of two numbers, but the underlying principle in all of them is the same, i.e., the GCD of two numbers is equal to the product of their common factors. We can also use the inbuilt GCD function C++, discussed in the 4th approach.

Approach 1 - Prime Factorization method

In this algorithm, we will find the common factors of both numbers and then return the product of the common factors as the GCD of the two numbers.

Steps of the algorithm:

  1. Create a function gcd(), and pass two numbers to it.
  2. Create a function getFactors(), which returns a vector containing all the prime factors of the number passed to it.
  3. Create a function isPrime(), which checks whether a number is a prime or not.
  4. Inside the getFactors() function, use the isPrime() function to check whether the factors are prime numbers or not, then return the vector.
  5. Inside the gcd() function, check if a or b is equal to 0, if yes, return a+ba+b.
  6. After that, initialize a variable GCD=1 inside the gcd() function and multiply it by the factors common in both vectors.
  7. Return the GCD.

Implementation:

Output:

In the above example, we find the GCD of two user input numbers 7575 and 5050 using the Prime Factorization method. At first, we store all the prime factors of both numbers in different vectors; after that, we find the intersection of both vectors and multiply the common factors by the GCD of those two numbers. Lastly, we will return and then print the GCD.

Time Complexity: O(max(a,b))O(max(a, b))
The time complexity of the getFactors() function is O(max(a, b) as its while loop is amortized, whereas the time complexity of gcd() is (max(a, b)).
The asymptotic time complexity will be O(max(a, b)).

Approach 2 - Euclidean Algorithm

The Euclidean Algorithm is an efficient method for calculating the GCD of two numbers, named after the ancient Greek mathematician Euclid. This algorithm was first described in his book "Elements".

The Euclidean Algorithm states that GCD(a,b)=GCD(a,ba)GCD(a,b) = GCD(a, b-a).

Proof of the Euclidean Algorithm:
Suppose we have 3 integers: a, b, and c, such that ab=ca-b = c.

The GCD(a,b) evenly divides aa and bb by definition.
As a result, aa and bb are multiples of GCD(a,b), i.e., a=GCD(a,b)xa = GCD(a,b)*x, for some positive integer xx and b=GCD(a,b)yb = GCD(a,b)*y, for some positive integer yy.

Putting the values of aa and bb in ab=ca - b = c :
c=xGCD(a,b)yGCD(a,b)\implies c = x*GCD(a,b) - y*GCD(a,b).
c=(xy)GCD(a,b)\implies c = (x-y)*GCD(a,b)

An illustration of this is in the figure below:

Euclidean Algorithm Approach Two

The GCD(b,c)GCD(b,c) evenly divides bb and cc by definition.
As a result, bb and cc are multiples of GCD(b,c)GCD(b,c), i.e., b=GCD(b,c)pb = GCD(b,c)*p, for some positive integer pp and c=GCD(b,c)qc = GCD(b,c)*q, for some positive integer qq.

Putting the values of bb and cc in ab=ca - b = c :
apGCD(b,c)=qGCD(b,c)\implies a - p*GCD(b,c) = q*GCD(b,c).
a=(p+q)GCD(b,c)\implies a = (p+q)*GCD(b,c)

An illustration of this is in the figure below:

Euclidean Algorithm Illustration

The GCD(a,b)GCD(a,b) divides both bb and cc and GCD(b,c)GCD(b,c) divides both aa and bb.
GCD(a,b)GCD(b,c)\implies GCD(a,b) \leq GCD(b,c) as GCD(b,c)GCD(b,c) is the "GREATEST" common divisor of bb and cc.
GCD(b,c)GCD(a,b)\implies GCD(b,c) \leq GCD(a,b) as GCD(a,b)GCD(a,b) is the "GREATEST" common divisor of aa and bb.

An illustration of this is in the figure below:

Euclidean Algorithm Illustration Two

Therefore, GCD(a,b)=GCD(b,c)GCD(a,b) = GCD(b,c), where c=abc = a - b,
GCD(a,b)=GCD(b,ab)\implies GCD(a,b) = GCD(b,a-b).

Euclidean Algorithm Illustration Three

Steps of the algorithm:

  1. Create a function gcd()gcd(), and pass two numbers to it.
  2. If aa or bb is 00, return a+ba+b.
  3. If a==ba == b, return aa.
  4. If a>ba > b, return gcd(ab,a)gcd(a-b, a).
  5. If none of those conditions satisfy, return gcd(ab,a)gcd(a-b, a).

Implementation:

Output:

In the above example, we are computing the GCD of two user input numbers 8888 and 100100 using Euclid's Algorithm.

Time Complexity: O(a+b)O(a+b)
In this algorithm, the number of steps is linear, e.g., GCD(x,1)GCD(x, 1), in which we will subtract 11 from xx in each recursion so that the time complexity will be O(a+b)O(a+b).

Approach 3 - Optimized Euclidean Algorithm (Using the modulo operator)

As we have just learned about the Euclidean Algorithm in the previous section, We will now learn about its optimized version, i.e., using the modulo operator.

The Optimized Euclidean Alogrithm states that if, a=bq+ra = b*q + r and b0b\neq0, where qq is the quotient and rr is the remainder, then GCD(a,b)=GCD(b,r)GCD(a, b) = GCD(b, r).

Proof of the algorithm:
We have just proved that GCD(a,b)=GCD(b,ab)GCD(a,b) = GCD(b, a-b), but what if we repeatedly apply this rule to itself? Let's set what happens.

GCD(a,b)=GCD(b,ab)GCD(a,b) = GCD(b, a-b) GCD(b,ab)=GCD(b,a2b)=GCD(b,a3b)\implies GCD(b, a-b) = GCD(b, a-2*b) = GCD(b, a-3*b) and so on...

Therefore, GCD(a,b)=GCD(b,aqb)GCD(a,b) = GCD(b, a-q*b), where aqb=ra-q*b = r,
GCD(a,b)=GCD(b,r)\implies GCD(a,b) = GCD(b, r)

Optimized Euclidean Algorithm

Steps of the algorithm

  1. Create a function gcd()gcd(), and pass two numbers to it.
  2. If b=0b=0, return aa.
  3. Otherwise, return gcd(b,a%b)gcd(b, a\%b).

Implementation:

Output:

In the above example, we are computing the GCD of two user input numbers 5454 and 00 using the Optimized Euclid's Algorithm.

Time Complexity: O(log max(a,b))
In this algorithm, we are recursively calling GCD(b,a%b)GCD(b,a\%b), reducing the second parameter by a%ba\%b in each function call, so the time complexity will be O(logmax(a,b))O(log\;max(a,b)).

Approach 4 - Using __gcd(a,b) function.

We can also use the inbuilt GCD function C++ __gcd() included in the <algorithm> header file to find the GCD of two numbers.

Implementation:

Output:

In the above example, we are computing the GCD of two user input numbers, 54 and 22, using the GCD function C++.

Conclusion

  1. The GCD (Greatest Common Divisor) is also known as HCF (Highest Common Factor).
  2. GCD of two numbers is the greatest positive integer that completely divides both numbers.
  3. We can find the GCD of two numbers using the Prime Factorization method.
  4. Euclidean algorithm to find GCD of two numbers states that GCD(a,b)=GCD(b,ba)GCD(a, b) = GCD(b, b-a).
  5. Optimized Euclidean algorithm states that if b0b \neq 0 and a=bq+ra = b * q + r, where aa and bb are the two numbers, qq is quotient and rr is the remainder, then GCD(a,b)=GCD(b,r)GCD(a, b) = GCD(b, r)
  6. We can also find the GCD of two numbers in C++ using the inbuilt __gcd() GCD function.