Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
Last Updated :
14 Feb, 2023
Question problem states that find gcd() of two numbers out of which one number can be as big as (10^9)^(10^9) which cannot be stored in datatypes like long long int in C++
Examples:
Input : 1 1 1
Output : 1
Input : 10248585 1000000 12564
Output : 9
We know from Euclid’s algorithm that, gcd(a, b) = gcd(a % b, b). Now the problem remains to find a^n mod c. This could be done using modular exponentiation with O(logn) complexity.
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll modPower(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll gcdPow(ll a, ll n, ll c)
{
if (a % c == 0)
return c;
ll modexpo = modPower(a, n, c);
return gcd(modexpo, c);
}
int main()
{
ll a = 10248585, n = 1000000, c = 12564;
cout << gcdPow(a, n, c);
return 0;
}
|
Java
class GFG
{
static long modPower( long x, long y,
long p)
{
long res = 1 ;
x = x % p;
while (y > 0 )
{
if ((y & 1 ) > 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static long gcd( long a, long b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static long gcdPow( long a,
long n, long c)
{
if (a % c == 0 )
return c;
long modexpo = modPower(a, n, c);
return gcd(modexpo, c);
}
public static void main(String[] args)
{
long a = 10248585 ,
n = 1000000 , c = 12564 ;
System.out.println(gcdPow(a, n, c));
}
}
|
Python 3
def modPower(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def gcdPow(a, n, c):
if (a % c = = 0 ):
return c
modexpo = modPower(a, n, c)
return gcd(modexpo, c)
if __name__ = = "__main__" :
a = 10248585
n = 1000000
c = 12564
print (gcdPow(a, n, c))
|
C#
using System;
class GFG
{
static long modPower( long x, long y,
long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) > 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long gcd( long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdPow( long a,
long n, long c)
{
if (a % c == 0)
return c;
long modexpo = modPower(a, n, c);
return gcd(modexpo, c);
}
public static void Main()
{
long a = 10248585,
n = 1000000, c = 12564;
Console.Write(gcdPow(a, n, c));
}
}
|
PHP
<?php
function modPower( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function gcdPow( $a , $n , $c )
{
if ( $a % $c == 0)
return $c ;
$modexpo = modPower( $a , $n , $c );
return gcd( $modexpo , $c );
}
$a = 10248585;
$n = 1000000;
$c = 12564;
echo gcdPow( $a , $n , $c );
?>
|
Javascript
<script>
function modPower(x,y,p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) > 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function gcd(a,b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function gcdPow(a,n,c)
{
if (a % c == 0)
return c;
let modexpo = modPower(a, n, c);
return gcd(modexpo, c);
}
let a = 10248585,
n = 1000000, c = 12564;
document.write(gcdPow(a, n, c));
</script>
|
Time Complexity: O(log(n)+log(c)).
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...