# Maximum GCD from Given Product of Unknowns

• Last Updated : 01 Jun, 2021

Given two integers N and P where P is the product of N unknown integers, the task is to find the GCD of those integers. There can be different group of integers possible that give the same product, in that case, print the GCD which is maximum among all possible groups.
Examples:

Input: N = 3, P = 24
Output:
{1, 1, 24}, {1, 2, 12}, {1, 3, 8}, {1, 4, 6}, {2, 2, 6} and {2, 3, 4}
are the only integer groups possible with product = 24
And they have GCDs 1, 1, 1, 1, 2 and 1 respectively.
Input: N = 5, P = 1
Output:

Approach: Let g be the gcd of a1, a2, a3, …, an. Since, ai must be a multiple of g for each i and their product P = a1 * a2 * a3 * … * an must be a multiple of gn. The answer is the maximum g such that gn % P = 0
Let P = k1p1 * k2p2 * k3p3 * … * knpt. Then g must be of the form k1p1 * k2p2 * k3p3′ * … * knpt. In order to maximise g we must choose pi = pi / N
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include``using` `namespace` `std;` `// Function to return the required gcd``long` `max_gcd(``long` `n, ``long` `p)``{``    ``int` `count = 0;``    ``long` `gcd = 1;` `    ``// Count the number of times 2 divides p``    ``while` `(p % 2 == 0)``    ``{` `        ``// Equivalent to p = p / 2;``        ``p >>= 1;``        ``count++;``    ``}` `    ``// If 2 divides p``    ``if` `(count > 0)``        ``gcd *= (``long``)``pow``(2, count / n);` `    ``// Check all the possible numbers``    ``// that can divide p``    ``for` `(``long` `i = 3; i <= ``sqrt``(p); i += 2)``    ``{``        ``count = 0;``        ``while` `(p % i == 0)``        ``{``            ``count++;``            ``p = p / i;``        ``}``        ``if` `(count > 0)``        ``{``            ``gcd *= (``long``)``pow``(i, count / n);``        ``}``    ``}` `    ``// If n in the end is a prime number``    ``if` `(p > 2)``        ``gcd *= (``long``)``pow``(p, 1 / n);` `    ``// Return the required gcd``    ``return` `gcd;``}` `// Driver code``int` `main()``{``    ``long` `n = 3;``    ``long` `p = 80;``    ``cout << max_gcd(n, p);``}``    ` `// This code is contributed by Code_Mech`

## Java

 `// Java implementation of the approach``class` `GFG {` `    ``// Function to return the required gcd``    ``static` `long` `max_gcd(``long` `n, ``long` `p)``    ``{``        ``int` `count = ``0``;``        ``long` `gcd = ``1``;` `        ``// Count the number of times 2 divides p``        ``while` `(p % ``2` `== ``0``) {` `            ``// Equivalent to p = p / 2;``            ``p >>= ``1``;``            ``count++;``        ``}` `        ``// If 2 divides p``        ``if` `(count > ``0``)``            ``gcd *= (``long``)Math.pow(``2``, count / n);` `        ``// Check all the possible numbers``        ``// that can divide p``        ``for` `(``long` `i = ``3``; i <= Math.sqrt(p); i += ``2``) {``            ``count = ``0``;``            ``while` `(p % i == ``0``) {``                ``count++;``                ``p = p / i;``            ``}``            ``if` `(count > ``0``) {``                ``gcd *= (``long``)Math.pow(i, count / n);``            ``}``        ``}` `        ``// If n in the end is a prime number``        ``if` `(p > ``2``)``            ``gcd *= (``long``)Math.pow(p, ``1` `/ n);` `        ``// Return the required gcd``        ``return` `gcd;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `n = ``3``;``        ``long` `p = ``80``;``        ``System.out.println(max_gcd(n, p));``    ``}``}`

## Python3

 `# Python3 implementation of the approach``import` `math` `# Function to return the required gcd``def` `max_gcd(n, p):` `    ``count ``=` `0``;``    ``gcd ``=` `1``;` `    ``# Count the number of times 2 divides p``    ``while` `(p ``%` `2` `=``=` `0``):``    ` `        ``# Equivalent to p = p / 2;``        ``p >>``=` `1``;``        ``count ``=` `count ``+` `1``;``    ` `    ``# If 2 divides p``    ``if` `(count > ``0``):``        ``gcd ``=` `gcd ``*` `pow``(``2``, count ``/``/` `n);` `    ``# Check all the possible numbers``    ``# that can divide p``    ``for` `i ``in` `range``(``3``, (``int``)(math.sqrt(p)), ``2``):``    ` `        ``count ``=` `0``;``        ``while` `(p ``%` `i ``=``=` `0``):``        ` `            ``count ``=` `count ``+` `1``;``            ``p ``=` `p ``/``/` `i;``        ` `        ``if` `(count > ``0``):``        ` `            ``gcd ``=` `gcd ``*` `pow``(i, count ``/``/` `n);``        ` `    ``# If n in the end is a prime number``    ``if` `(p > ``2``) :``        ``gcd ``=` `gcd ``*` `pow``(p, ``1` `/``/` `n);` `    ``# Return the required gcd``    ``return` `gcd;` `# Driver code``n ``=` `3``;``p ``=` `80``;``print``(max_gcd(n, p));` `# This code is contributed by Shivi_Aggarwal`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `// Function to return the required gcd``static` `long` `max_gcd(``long` `n, ``long` `p)``{``    ``int` `count = 0;``    ``long` `gcd = 1;` `    ``// Count the number of times 2 divides p``    ``while` `(p % 2 == 0)``    ``{` `        ``// Equivalent to p = p / 2;``        ``p >>= 1;``        ``count++;``    ``}` `    ``// If 2 divides p``    ``if` `(count > 0)``        ``gcd *= (``long``)Math.Pow(2, count / n);` `    ``// Check all the possible numbers``    ``// that can divide p``    ``for` `(``long` `i = 3; i <= Math.Sqrt(p); i += 2)``    ``{``        ``count = 0;``        ``while` `(p % i == 0)``        ``{``            ``count++;``            ``p = p / i;``        ``}``        ``if` `(count > 0)``        ``{``            ``gcd *= (``long``)Math.Pow(i, count / n);``        ``}``    ``}` `    ``// If n in the end is a prime number``    ``if` `(p > 2)``        ``gcd *= (``long``)Math.Pow(p, 1 / n);` `    ``// Return the required gcd``    ``return` `gcd;``}` `// Driver code``public` `static` `void` `Main()``{``    ``long` `n = 3;``    ``long` `p = 80;``    ``Console.WriteLine(max_gcd(n, p));``}``}` `// This code is contributed by Ryuga`

## PHP

 `>= 1;``        ``\$count``++;``    ``}` `    ``// If 2 divides p``    ``if` `(``\$count` `> 0)``        ``\$gcd` `*= pow(2, (int)(``\$count` `/ ``\$n``));` `    ``// Check all the possible numbers``    ``// that can divide p``    ``for` `(``\$i` `= 3; ``\$i` `<= (int)sqrt(``\$p``); ``\$i` `+= 2)``    ``{``        ``\$count` `= 0;``        ``while` `(``\$p` `% ``\$i` `== 0)``        ``{``            ``\$count``++;``            ``\$p` `= (int)(``\$p` `/ ``\$i``);``        ``}``        ``if` `(``\$count` `> 0)``        ``{``            ``\$gcd` `*= pow(``\$i``, (int)(``\$count` `/ ``\$n``));``        ``}``    ``}` `    ``// If n in the end is a prime number``    ``if` `(``\$p` `> 2)``        ``\$gcd` `*= pow(``\$p``, (int)(1 / ``\$n``));` `    ``// Return the required gcd``    ``return` `\$gcd``;``}` `// Driver code``\$n` `= 3;``\$p` `= 80;``echo``(max_gcd(``\$n``, ``\$p``));` `// This code is contributed by Code_Mech`

## Javascript

 ``
Output:
`2`

My Personal Notes arrow_drop_up