Maximum possible GCD for a pair of integers with product N | C ++ Program to implement the above approach ; Function to return / the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxGcd ( int N ) { int maxGcd = INT_MIN , A , B ; for ( int i = 1 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i , B = N / i ; maxGcd = max ( maxGcd , __gcd ( A , B ) ) ; } } return maxGcd ; } int main ( ) { int N = 18 ; cout << getMaxGcd ( N ) ; return 0 ; }