C++ Program to Find the GCD of Two Numbers
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:
- Create a function gcd(), and pass two numbers to it.
- Create a function getFactors(), which returns a vector containing all the prime factors of the number passed to it.
- Create a function isPrime(), which checks whether a number is a prime or not.
- Inside the getFactors() function, use the isPrime() function to check whether the factors are prime numbers or not, then return the vector.
- Inside the gcd() function, check if a or b is equal to 0, if yes, return .
- After that, initialize a variable GCD=1 inside the gcd() function and multiply it by the factors common in both vectors.
- Return the GCD.
Implementation:
Output:
In the above example, we find the GCD of two user input numbers and 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:
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 .
Proof of the Euclidean Algorithm:
Suppose we have 3 integers: a, b, and c, such that .
The GCD(a,b) evenly divides and by definition.
As a result, and are multiples of GCD(a,b), i.e., , for some positive integer and , for some positive integer .
Putting the values of and in :
.
An illustration of this is in the figure below:
The evenly divides and by definition.
As a result, and are multiples of , i.e., , for some positive integer and , for some positive integer .
Putting the values of and in :
.
An illustration of this is in the figure below:
The divides both and and divides both and .
as is the "GREATEST" common divisor of and .
as is the "GREATEST" common divisor of and .
An illustration of this is in the figure below:
Therefore, , where ,
.
Steps of the algorithm:
- Create a function , and pass two numbers to it.
- If or is , return .
- If , return .
- If , return .
- If none of those conditions satisfy, return .
Implementation:
Output:
In the above example, we are computing the GCD of two user input numbers and using Euclid's Algorithm.
Time Complexity:
In this algorithm, the number of steps is linear, e.g., , in which we will subtract from in each recursion so that the time complexity will be .
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, and , where is the quotient and is the remainder, then .
Proof of the algorithm:
We have just proved that , but what if we repeatedly apply this rule to itself? Let's set what happens.
and so on...
Therefore, , where ,
Steps of the algorithm
- Create a function , and pass two numbers to it.
- If , return .
- Otherwise, return .
Implementation:
Output:
In the above example, we are computing the GCD of two user input numbers and using the Optimized Euclid's Algorithm.
Time Complexity: O(log max(a,b))
In this algorithm, we are recursively calling , reducing the second parameter by in each function call, so the time complexity will be .
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
- The GCD (Greatest Common Divisor) is also known as HCF (Highest Common Factor).
- GCD of two numbers is the greatest positive integer that completely divides both numbers.
- We can find the GCD of two numbers using the Prime Factorization method.
- Euclidean algorithm to find GCD of two numbers states that .
- Optimized Euclidean algorithm states that if and , where and are the two numbers, is quotient and is the remainder, then
- We can also find the GCD of two numbers in C++ using the inbuilt __gcd() GCD function.