Open In App

Maximum GCD from Given Product of Unknowns

Improve
Improve
Like Article
Like
Save
Share
Report

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<bits/stdc++.h>
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




<?php
// PHP implementation of the approach
 
// Function to return the required gcd
function 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++;
    }
 
    // 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




<script>
    // Javascript implementation of the approach
     
    // Function to return the required gcd
    function max_gcd(n, p)
    {
        let count = 0;
        let 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 *= Math.pow(2, parseInt(count / n, 10));
 
        // Check all the possible numbers
        // that can divide p
        for (let i = 3; i <= parseInt(Math.sqrt(p), 10); i += 2)
        {
            count = 0;
            while (p % i == 0)
            {
                count++;
                p = parseInt(p / i, 10);
            }
            if (count > 0)
            {
                gcd *= Math.pow(i, parseInt(count / n, 10));
            }
        }
 
        // If n in the end is a prime number
        if (p > 2)
            gcd *= Math.pow(p, parseInt(1 / n, 10));
 
        // Return the required gcd
        return gcd;
    }
     
    let n = 3;
    let p = 80;
    document.write(max_gcd(n, p));
     
</script>


Output: 

2

 

Time Complexity: O(sqrtp*logn)

Auxiliary Space: O(1)



Last Updated : 23 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads