Given three numbers a, b, n. Find GCD(an, b).
Examples:
Input : a = 2, b = 3, n = 3
Output : 1
2^3 = 8. GCD of 8 and 3 is 1.
Input : a = 2, b = 4, n = 5
Output : 4
First Approach : Brute Force approach is to first compute a^n, then compute GCD of a^n and b.
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
ll powGCD(ll a, ll n, ll b)
{
for ( int i = 0; i < n; i++)
a = a * a;
return gcd(a, b);
}
int main()
{
ll a = 10, b = 5, n = 2;
cout << powGCD(a, n, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long gcd( long a, long b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static long powGCD( long a, long n, long b)
{
for ( int i = 0 ; i < n; i++)
a = a * a;
return gcd(a, b);
}
public static void main (String[] args) {
long a = 10 , b = 5 , n = 2 ;
System.out.println(powGCD(a, n, b));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def powGCD(a, n, b):
for i in range ( 0 , n + 1 , 1 ):
a = a * a
return gcd(a, b)
if __name__ = = '__main__' :
a = 10
b = 5
n = 2
print (powGCD(a, n, b))
|
C#
using System;
class GFG
{
public static long gcd( long a, long b)
{
if (a == 0)
{
return b;
}
return gcd(b % a, a);
}
public static long powGCD( long a,
long n, long b)
{
for ( int i = 0; i < n; i++)
{
a = a * a;
}
return gcd(a, b);
}
public static void Main( string [] args)
{
long a = 10, b = 5, n = 2;
Console.WriteLine(powGCD(a, n, b));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function powGCD( $a , $n , $b )
{
for ( $i = 0; $i < $n ; $i ++)
$a = $a * $a ;
return gcd( $a , $b );
}
$a = 10;
$b = 5;
$n = 2;
echo powGCD( $a , $n , $b );
?>
|
Javascript
<script>
function gcd(a , b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function powGCD(a , n , b)
{
for (i = 0; i < n; i++)
a = a * a;
return gcd(a, b);
}
var a = 10, b = 5, n = 2;
document.write(powGCD(a, n, b));
</script>
|
Time Complexity: O(n + log(min(a, b)), where n, a and b represents the given integer.
Auxiliary Space: O(log(min(a, b))), due to the recursive stack space.
But, what if n is very large (say > 10^9). Modular Exponentiation is the way. We know (a*b) % m = ( (a%m) * (b%m) ) % m). We also know gcd(a, b) = gcd(b%a, a) . So instead of computing ” pow(a, n), we use modular exponentiation.
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(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 (a == 0)
return b;
return gcd(b % a, a);
}
ll powerGCD(ll a, ll b, ll n)
{
ll e = power(a, n, b);
return gcd(e, b);
}
int main()
{
ll a = 5, b = 4, n = 2;
cout << powerGCD(a, b, n);
return 0;
}
|
Java
import java.util.*;
class Solution{
static long power( 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 (a == 0 )
return b;
return gcd(b % a, a);
}
static long powerGCD( long a, long b, long n)
{
long e = power(a, n, b);
return gcd(e, b);
}
public static void main(String args[])
{
long a = 5 , b = 4 , n = 2 ;
System.out.print( powerGCD(a, b, n));
}
}
|
Python3
def power( 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 (a = = 0 ):
return b
return gcd(b % a, a)
def powerGCD( a, b, n):
e = power(a, n, b)
return gcd(e, b)
if __name__ = = "__main__" :
a = 5
b = 4
n = 2
print (powerGCD(a, b, n))
|
C#
using System;
class GFG
{
static long power( 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 (a == 0)
return b;
return gcd(b % a, a);
}
static long powerGCD( long a, long b,
long n)
{
long e = power(a, n, b);
return gcd(e, b);
}
public static void Main()
{
long a = 5, b = 4, n = 2;
Console.Write( powerGCD(a, b, n));
}
}
|
PHP
<?php
function power( $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 ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function powerGCD( $a , $b , $n )
{
$e = power( $a , $n , $b );
return gcd( $e , $b );
}
$a = 5;
$b = 4;
$n = 2;
echo powerGCD( $a , $b , $n );
?>
|
Javascript
<script>
function power(x , y , p) {
var 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 (a == 0)
return b;
return gcd(b % a, a);
}
function powerGCD(a , b , n) {
var e = power(a, n, b);
return gcd(e, b);
}
var a = 5, b = 4, n = 2;
document.write(powerGCD(a, b, n));
</script>
|
Time Complexity: O(logn + log(min(a, b)), where n, a and b represents the given integer.
Auxiliary Space: O(log(min(a, b))), due to the recursive stack space.