std::gcd | C++ inbuilt function for finding GCD
Last Updated :
31 Jan, 2023
In many competitive programming problems, we need to find greatest common divisor also known as gcd. Euclids algorithm to find gcd has been discussed here.
C++ has the built-in function for calculating GCD. This function is present in header file.
Syntax for C++14 :
Library: 'algorithm'
__gcd(m, n)
Parameter : m, n
Return Value : 0 if both m and n are zero,
else gcd of m and n.
Syntax for C++17 :
Library: 'numeric'
gcd(m, n)
Parameter : m, n
Return Value : 0 if both m and n are zero,
else gcd of m and n.
CPP
// CPP program to illustrate
// gcd function of C++ STL
#include <iostream>
#include <algorithm>
// #include<numeric> for C++17
using namespace std;
int main()
{
cout << "gcd(6, 20) = " << __gcd(6, 20) << endl; // gcd(6,20) for C++17
}
Time Complexity: O(logn)
Auxiliary Space: O(1)
Program to Find the gcd of all numbers in a vector.
C++
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = { 12, 15, 18, 21, 24 };
int ans =__gcd(numbers[0], numbers[1]);
for (int i = 2; i < numbers.size(); i++)
{
ans = __gcd(ans, numbers[i]);
}
cout << "The GCD of the numbers in the vector is: " <<ans<<endl;
return 0;
}
OutputThe GCD of the numbers in the vector is: 3
Time Complexity: O(k*logn)
Auxiliary Space: O(k)
Note: If either M or N is not an integer type, or if either is (possibly cv-qualified) bool, the program is ill-formed. Also, If either |m| or |n| is not representable as a value of type std::common_type_t, the behavior is undefined.
CPP
// CPP program to illustrate
// undefined behavior of
// gcd function of C++ STL
#include <iostream>
#include <algorithm>
// #include<numeric> for C++17
using namespace std;
int main()
{
cout << "gcd(2.0, 8) = " << __gcd(2.0, 8) << endl; // gcd(2.0,8) for C++17
}
Output:
Error, As the data type float is not supported by std::gcd.
Similar Reads
Exception Handling using classes in C++ In C++, unexpected issues may occur during program execution such as attempting to divide by zero, accessing a non-existent file or using invalid data. These issues are called exceptions. These exceptions must be handled to avoid abnormal termination of the program.C++ provides try-catch block to ha
5 min read
GCD of Two Numbers in C++ GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that exactly divides both numbers. In this article, we will learn to write a C++ program to find the GCD of two numbers.Example:Input: a = 12, b = 16Output: 4Explanation: As 4 is the largest number whic
3 min read
C++ Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) CPP // C++ program to find GCD of two or // mo
3 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Euclidean algorithms (Basic and Extended) The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.Examples:input: a = 12, b = 20Output: 4Explanatio
9 min read