Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
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++
// CPP program to find GCD of a^n and b. #include <bits/stdc++.h> #define ll long long int using namespace std; /* Iterative Function to calculate (x^y)%p in O(log y) */ll modPower(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; } // Finds GCD of a and b ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // Finds GCD of a^n and c ll gcdPow(ll a, ll n, ll c) { // check if c is a divisor of a if (a % c == 0) return c; // First compute (a^n) % c ll modexpo = modPower(a, n, c); // Now simply return GCD of modulo // power and c. return gcd(modexpo, c); } // Driver code int main() { ll a = 10248585, n = 1000000, c = 12564; cout << gcdPow(a, n, c); return 0; } |
chevron_right
filter_none
Java
// Java program to find // GCD of a^n and b. class GFG { /* Iterative Function to calculate (x^y)%p in O(log y) */static long modPower(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; } // Finds GCD of a and b static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } // Finds GCD of a^n and c static long gcdPow(long a, long n, long c) { // check if c is a divisor of a if (a % c == 0) return c; // First compute (a^n) % c long modexpo = modPower(a, n, c); // Now simply return GCD // of modulo power and c. return gcd(modexpo, c); } // Driver code public static void main(String[] args) { long a = 10248585, n = 1000000, c = 12564; System.out.println(gcdPow(a, n, c)); } } // This code is contributed by mits |
chevron_right
filter_none
Python 3
# Python3 program to find # GCD of a^n and b. # Iterative Function to # calculate (x^y)%p in O(log y) def modPower(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 # Finds GCD of a and b def gcd(a, b): if (b == 0): return a return gcd(b, a % b) # Finds GCD of a^n and c def gcdPow(a, n, c): # check if c is a divisor of a if (a % c == 0): return c # First compute (a^n) % c modexpo = modPower(a, n, c) # Now simply return GCD of # modulo power and c. return gcd(modexpo, c) # Driver code if __name__ == "__main__": a = 10248585 n = 1000000 c = 12564 print(gcdPow(a, n, c)) # This code is contributed # by ChitraNayal |
chevron_right
filter_none
C#
// C# program to find // GCD of a^n and b. using System; class GFG { /* Iterative Function to calculate (x^y)%p in O(log y) */static long modPower(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; } // Finds GCD of a and b static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } // Finds GCD of a^n and c static long gcdPow(long a, long n, long c) { // check if c is a divisor of a if (a % c == 0) return c; // First compute (a^n) % c long modexpo = modPower(a, n, c); // Now simply return GCD // of modulo power and c. return gcd(modexpo, c); } // Driver code public static void Main() { long a = 10248585, n = 1000000, c = 12564; Console.Write(gcdPow(a, n, c)); } } // This code is contributed // by ChitraNayal |
chevron_right
filter_none
PHP
<?php // PHP program to find GCD // of a^n and b. /* Iterative Function to calculate (x^y)%p in O(log y) */function modPower($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; } // Finds GCD of a and b function gcd($a, $b) { if ($b == 0) return $a; return gcd($b, $a % $b); } // Finds GCD of a^n and c function gcdPow($a, $n, $c) { // check if c is a divisor of a if ($a % $c == 0) return $c; // First compute (a^n) % c $modexpo = modPower($a, $n, $c); // Now simply return GCD // of modulo power and c. return gcd($modexpo, $c); } // Driver code $a = 10248585; $n = 1000000; $c = 12564; echo gcdPow($a, $n, $c); // This code is contributed // by Akanksha Rai(Abby_akku) ?> |
chevron_right
filter_none
Output:
9
Recommended Posts:
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find the value of f(n) / f(r) * f(n-r)
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find 2^(2^A) % B
- Given two numbers a and b find all x such that a % x = b
- Find the value of ln(N!) using Recursion
- Find the sum of all multiples of 2 and 5 below N
- Find the other number when LCM and HCF given
- Find x and y satisfying ax + by = n
- Find (a^b)%m where 'b' is very large
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find two numbers whose sum and GCD are given
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.



