Maximum GCD from Given Product of Unknowns
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: 2
{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++
#include<bits/stdc++.h>
using namespace std;
long max_gcd( long n, long p)
{
int count = 0;
long gcd = 1;
while (p % 2 == 0)
{
p >>= 1;
count++;
}
if (count > 0)
gcd *= ( long ) pow (2, count / n);
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 (p > 2)
gcd *= ( long ) pow (p, 1 / n);
return gcd;
}
int main()
{
long n = 3;
long p = 80;
cout << max_gcd(n, p);
}
|
Java
class GFG {
static long max_gcd( long n, long p)
{
int count = 0 ;
long gcd = 1 ;
while (p % 2 == 0 ) {
p >>= 1 ;
count++;
}
if (count > 0 )
gcd *= ( long )Math.pow( 2 , count / n);
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 (p > 2 )
gcd *= ( long )Math.pow(p, 1 / n);
return gcd;
}
public static void main(String[] args)
{
long n = 3 ;
long p = 80 ;
System.out.println(max_gcd(n, p));
}
}
|
Python3
import math
def max_gcd(n, p):
count = 0 ;
gcd = 1 ;
while (p % 2 = = 0 ):
p >> = 1 ;
count = count + 1 ;
if (count > 0 ):
gcd = gcd * pow ( 2 , count / / n);
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 (p > 2 ) :
gcd = gcd * pow (p, 1 / / n);
return gcd;
n = 3 ;
p = 80 ;
print (max_gcd(n, p));
|
C#
using System;
class GFG
{
static long max_gcd( long n, long p)
{
int count = 0;
long gcd = 1;
while (p % 2 == 0)
{
p >>= 1;
count++;
}
if (count > 0)
gcd *= ( long )Math.Pow(2, count / n);
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 (p > 2)
gcd *= ( long )Math.Pow(p, 1 / n);
return gcd;
}
public static void Main()
{
long n = 3;
long p = 80;
Console.WriteLine(max_gcd(n, p));
}
}
|
PHP
<?php
function max_gcd( $n , $p )
{
$count = 0;
$gcd = 1;
while ( $p % 2 == 0)
{
$p >>= 1;
$count ++;
}
if ( $count > 0)
$gcd *= pow(2, (int)( $count / $n ));
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 ( $p > 2)
$gcd *= pow( $p , (int)(1 / $n ));
return $gcd ;
}
$n = 3;
$p = 80;
echo (max_gcd( $n , $p ));
|
Javascript
<script>
function max_gcd(n, p)
{
let count = 0;
let gcd = 1;
while (p % 2 == 0)
{
p >>= 1;
count++;
}
if (count > 0)
gcd *= Math.pow(2, parseInt(count / n, 10));
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 (p > 2)
gcd *= Math.pow(p, parseInt(1 / n, 10));
return gcd;
}
let n = 3;
let p = 80;
document.write(max_gcd(n, p));
</script>
|
Time Complexity: O(sqrtp*logn)
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...