# GCD of a number raised to some power and another number

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

First Approach : Brute Force approach is to first compute a^n, then compute GCD of a^n and b.

## C++

 `// CPP program to find GCD of a^n and b. ` `#include ` `using` `namespace` `std; ` ` `  `typedef` `long` `long` `int` `ll; ` ` `  `ll gcd(ll a, ll b) ` `{ ` `    ``if` `(a == 0) ` `        ``return` `b; ` `    ``return` `gcd(b % a, a); ` `} ` ` `  `// Returns GCD of a^n and b. ` `ll powGCD(ll a, ll n, ll b) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``a = a * a; ` ` `  `    ``return` `gcd(a, b); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``ll a = 10, b = 5, n = 2; ` `    ``cout << powGCD(a, n, b); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find GCD of a^n and b. ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  ` `  `static` `long` `gcd(``long` `a, ``long` `b) ` `{ ` `    ``if` `(a == ``0``) ` `        ``return` `b; ` `    ``return` `gcd(b % a, a); ` `} ` ` `  `// Returns GCD of a^n and b. ` `static` `long` `powGCD(``long` `a, ``long` `n, ``long` `b) ` `{ ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``a = a * a; ` ` `  `    ``return` `gcd(a, b); ` `} ` ` `  `// Driver code ` `    ``public` `static` `void` `main (String[] args) { ` `    ``long` `a = ``10``, b = ``5``, n = ``2``; ` `    ``System.out.println(powGCD(a, n, b)); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python 3 program to find  ` `# GCD of a^n and b. ` `def` `gcd(a, b): ` `    ``if` `(a ``=``=` `0``): ` `        ``return` `b ` `    ``return` `gcd(b ``%` `a, a) ` ` `  `# Returns GCD of a^n and b. ` `def` `powGCD(a, n, b): ` `    ``for` `i ``in` `range``(``0``, n ``+` `1``, ``1``): ` `        ``a ``=` `a ``*` `a ` ` `  `    ``return` `gcd(a, b) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `10` `    ``b ``=` `5` `    ``n ``=` `2` `    ``print``(powGCD(a, n, b)) ` `     `  `# This code is contributed  ` `# by Surendra_Gangwar `

## C#

 `// C# program to find GCD of a^n and b.  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `public` `static` `long` `gcd(``long` `a, ``long` `b) ` `{ ` `    ``if` `(a == 0) ` `    ``{ ` `        ``return` `b; ` `    ``} ` `    ``return` `gcd(b % a, a); ` `} ` ` `  `// Returns GCD of a^n and b.  ` `public` `static` `long` `powGCD(``long` `a,  ` `                          ``long` `n, ``long` `b) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``a = a * a; ` `    ``} ` ` `  `    ``return` `gcd(a, b); ` `} ` ` `  `// Driver code  ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``long` `a = 10, b = 5, n = 2; ` `    ``Console.WriteLine(powGCD(a, n, b)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Shrikant13 `

## PHP

 ` `

Output:

```5
```

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++

 `// C++ program of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `typedef` `long` `long` `int` `ll; ` ` `  `/* Calculates modular exponentiation, i.e., ` `   ``(x^y)%p in O(log y) */` `ll power(ll x, ll y, ll p) ` `{ ` `    ``ll res = 1; ``// Initialize result ` ` `  `    ``x = x % p; ``// Update x if it is more than or ` `    ``// equal to p ` ` `  `    ``while` `(y > 0) { ` ` `  `        ``// If y is odd, multiply x with result ` `        ``if` `(y & 1) ` `            ``res = (res * x) % p; ` ` `  `        ``// y must be even now ` `        ``y = y >> 1; ``// y = y/2 ` `        ``x = (x * x) % p; ` `    ``} ` `    ``return` `res; ` `} ` ` `  ` `  `ll gcd(ll a, ll b) ` `{ ` `    ``if` `(a == 0) ` `        ``return` `b; ` `    ``return` `gcd(b % a, a); ` `} ` ` `  `// Returns GCD of a^n and b ` `ll powerGCD(ll a, ll b, ll n) ` `{ ` `    ``ll e = power(a, n, b); ` `    ``return` `gcd(e, b); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``ll a = 5, b = 4, n = 2; ` `    ``cout << powerGCD(a, b, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program of the above approach  ` `import` `java.util.*; ` `class` `Solution{ ` `   `  `   `  `/* Calculates modular exponentiation, i.e.,  ` `   ``(x^y)%p in O(log y) */` `static` `long` `power(``long` `x, ``long` `y, ``long` `p)  ` `{  ` `    ``long` `res = ``1``; ``// Initialize result  ` `   `  `    ``x = x % p; ``// Update x if it is more than or  ` `    ``// equal to p  ` `   `  `    ``while` `(y > ``0``) {  ` `   `  `        ``// If y is odd, multiply x with result  ` `        ``if` `((y & ``1``)!=``0``)  ` `            ``res = (res * x) % p;  ` `   `  `        ``// y must be even now  ` `        ``y = y >> ``1``; ``// y = y/2  ` `        ``x = (x * x) % p;  ` `    ``}  ` `    ``return` `res;  ` `}  ` `   `  `   `  `static` `long` `gcd(``long` `a, ``long` `b)  ` `{  ` `    ``if` `(a == ``0``)  ` `        ``return` `b;  ` `    ``return` `gcd(b % a, a);  ` `}  ` `   `  `// Returns GCD of a^n and b  ` `static` `long` `powerGCD(``long` `a, ``long` `b, ``long` `n)  ` `{  ` `    ``long` `e = power(a, n, b);  ` `    ``return` `gcd(e, b);  ` `}  ` `   `  `// Driver code  ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``long` `a = ``5``, b = ``4``, n = ``2``;  ` `    ``System.out.print( powerGCD(a, b, n));  ` ` `  `}  ` `} ` `//contributed by Arnab Kundu `

## Python3

 `# Python3 program of the above approach ` `  `  `# Calculates modular exponentiation, i.e., ` ` ``# (x^y)%p in O(log y)  ` `def` `power( x,  y,  p): ` ` `  `    ``res ``=` `1`  `# Initialize result ` `  `  `    ``x ``=` `x ``%` `p ``# Update x if it is more than or ` `    ``# equal to p ` `  `  `    ``while` `(y > ``0``) : ` `  `  `        ``# If y is odd, multiply x with result ` `        ``if` `(y & ``1``): ` `            ``res ``=` `(res ``*` `x) ``%` `p ` `  `  `        ``# y must be even now ` `        ``y ``=` `y >> ``1`   `# y = y/2 ` `        ``x ``=` `(x ``*` `x) ``%` `p ` `     `  `    ``return` `res ` `  `  `  `  `def` `gcd(a,  b): ` ` `  `    ``if` `(a ``=``=` `0``): ` `        ``return` `b ` `    ``return` `gcd(b ``%` `a, a) ` `  `  `# Returns GCD of a^n and b ` `def` `powerGCD( a,  b,  n): ` ` `  `    ``e ``=` `power(a, n, b) ` `    ``return` `gcd(e, b) ` `  `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``a ``=` `5` `    ``b ``=` `4` `    ``n ``=` `2` `    ``print` `(powerGCD(a, b, n)) `

## C#

 `// C# program of the above approach  ` `using` `System; ` `class` `GFG ` `{ ` ` `  `/* Calculates modular exponentiation,  ` `i.e.,  (x^y)%p in O(log y) */` `static` `long` `power(``long` `x, ``long` `y, ``long` `p)  ` `{  ` `    ``long` `res = 1; ``// Initialize result  ` ` `  `    ``x = x % p; ``// Update x if it is more  ` `               ``// than or equal to p  ` ` `  `    ``while` `(y > 0)  ` `    ``{  ` ` `  `        ``// If y is odd, multiply x  ` `        ``// with result  ` `        ``if` `((y & 1) != 0)  ` `            ``res = (res * x) % p;  ` ` `  `        ``// y must be even now  ` `        ``y = y >> 1; ``// y = y/2  ` `        ``x = (x * x) % p;  ` `    ``}  ` `    ``return` `res;  ` `}  ` ` `  `static` `long` `gcd(``long` `a, ``long` `b)  ` `{  ` `    ``if` `(a == 0)  ` `        ``return` `b;  ` `    ``return` `gcd(b % a, a);  ` `}  ` ` `  `// Returns GCD of a^n and b  ` `static` `long` `powerGCD(``long` `a, ``long` `b, ` `                             ``long` `n)  ` `{  ` `    ``long` `e = power(a, n, b);  ` `    ``return` `gcd(e, b);  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main() ` `{  ` `    ``long` `a = 5, b = 4, n = 2;  ` `    ``Console.Write( powerGCD(a, b, n));  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 ` 0)  ` `    ``{  ` ` `  `        ``// If y is odd, multiply x  ` `        ``// with result  ` `        ``if` `(``\$y` `& 1)  ` `            ``\$res` `= (``\$res` `* ``\$x``) % ``\$p``;  ` ` `  `        ``// y must be even now  ` `        ``\$y` `= ``\$y` `>> 1; ``// y = y/2  ` `        ``\$x` `= (``\$x` `* ``\$x``) % ``\$p``;  ` `    ``}  ` `    ``return` `\$res``;  ` `}  ` ` `  `function` `gcd (``\$a``, ``\$b``)  ` `{  ` `    ``if` `(``\$a` `== 0)  ` `        ``return` `\$b``;  ` `    ``return` `gcd(``\$b` `% ``\$a``, ``\$a``);  ` `}  ` ` `  `// Returns GCD of a^n and b  ` `function` `powerGCD(``\$a``, ``\$b``, ``\$n``)  ` `{  ` `    ``\$e` `= power(``\$a``, ``\$n``, ``\$b``);  ` `    ``return` `gcd(``\$e``, ``\$b``);  ` `}  ` ` `  `// Driver code  ` `\$a` `= 5; ` `\$b` `= 4; ` `\$n` `= 2;  ` `echo` `powerGCD(``\$a``, ``\$b``, ``\$n``);  ` ` `  `// This code is contributed by Sachin. ` `?> `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.