Open In App

GCD of a number raised to some power and another number

Last Updated : 08 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// CPP program to find GCD of a^n and b.
#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);
}
 
// 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




<?php
// PHP program to find GCD of a^n and b
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
 
// Returns GCD of a^n and b.
function powGCD($a, $n, $b)
{
    for ($i = 0; $i < $n; $i++)
        $a = $a * $a;
 
    return gcd($a, $b);
}
 
// Driver code
$a = 10;
$b = 5;
$n = 2;
 
echo powGCD($a, $n, $b);
 
// This code is contributed by ANKITRAI1
?>


Javascript




<script>
// javascript program to find GCD of a^n and b.   
function gcd(a , b)
{
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
    // Returns GCD of a^n and b.
    function powGCD(a , n , b)
    {
        for (i = 0; i < n; i++)
            a = a * a;
 
        return gcd(a, b);
    }
 
    // Driver code
        var a = 10, b = 5, n = 2;
        document.write(powGCD(a, n, b));
 
// This code is contributed by gauravrajput1
</script>


Output: 

5

 

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




// C++ program of the above approach
#include <bits/stdc++.h>
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




<?php
// PHP program of the above approach
// Calculates modular exponentiation,
// i.e.,(x^y)%p in O(log y)
 
function 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;
}
 
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.
?>


Javascript




<script>
 
// Javascript program of the above approach
 
    /*
      Calculates modular exponentiation,
      i.e., (x^y)%p in O(log y)
     */
    function power(x , y , p) {
        var 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;
    }
 
    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) {
        var e = power(a, n, b);
        return gcd(e, b);
    }
 
    // Driver code
     
        var a = 5, b = 4, n = 2;
        document.write(powerGCD(a, b, n));
 
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

1

 

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.



Similar Reads

Larger of a^b or b^a (a raised to power b or b raised to power a)
Given two numbers [Tex]$a and b$ [/Tex], find which is greater [Tex]a^b or \, b^a [/Tex].If [Tex]a^b &gt; b^a [/Tex], print a^b is greater If [Tex]a^b &lt; b^a [/Tex], print b^a is greater If [Tex]a^b = b^a [/Tex], print Both are equalExamples: Input : 3 5 Output : a^b is greater 3^5 = 243, 5^3 = 125. Since, 243&gt;125, therefore a^b &gt; b^a. Inpu
5 min read
Find GCD between the sum of two given integers raised to the power of N and their difference
Given three positive integers, P, Q and N, the task is to find the GCD of (PN + QN) and (P - Q) under modulo 109 + 7. Examples: Input: p = 10, q = 6, n = 5Output: 4Explanation: pn + qn = 105 + 65 = 107776 and p - q = 4. GCD of 107776 and 4 is 4. Input: p = 7, q = 2 and n = 5Output: 1Explanation: pn + qn = 75 + 25 = 16839 and p - q = 5. GCD of 16839
12 min read
Find the sum of power of bit count raised to the power B
Given an integer, array A. Find the sum of set bits raised to the power A[i] for each element in A[i].Example: Input: N = 3, A[] = {1, 2, 3}Output: 10Explanation:Set bit of each array element is1 = 1 set bit, 2 = 1 set bit, 3 = 2 set bit store each set bit in b[i].Compute sum of power(b[i], i) where i is ranging from 1 to n., that is sum = power(1,
6 min read
Number of digits in 2 raised to power n
Let n be any power raised to base 2 i.e 2n. We are given the number n and our task is to find out the number of digits contained in the number 2n.Examples: Input : n = 5 Output : 2 Explanation : 2n = 32, which has only 2 digits. Input : n = 10 Output : 4 Explanation : 2n = 1024, which has only 4 digits. We can write 2n using logarithms as: 2n = 10n
5 min read
Minimum removals in a number to be divisible by 10 power raised to K
Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K or print -1 if it is impossible.Examples: Input : N = 10904025, K = 2 Output : 3 Explanation : We can remove the digits 4, 2 and 5 such that the number becomes 10900 which is divisible
12 min read
Find last five digits of a given five digit number raised to power five
Given a five-digit number N., The task is to find the last five digits of the given number raised to the power of 5 after modifying it by arranging the digits as: first digit, third digit, fifth digit, fourth digit, second digit.Examples: Input : N = 12345Output : 71232Explanation : After modification the number becomes 13542. (13542)5 is 455422043
9 min read
Check if a number can be expressed as x^y (x raised to power y)
Given a positive integer n, find if it can be expressed as xy where y &gt; 1 and x &gt; 0. x and y both are integers. Examples : Input: n = 8 Output: true 8 can be expressed as 23 Input: n = 49 Output: true 49 can be expressed as 72 Input: n = 48 Output: false 48 can't be expressed as xyRecommended PracticeCheck if a number can be expressed as x^yT
11 min read
Find X to minimize XOR of N and division of N by 2 raised to power X
Given a binary string S of a number N, the task is to find a number X such that we can minimize the value of N by changing N = N ??N/2X?, where 1 ? X ? |S| and ? denotes the bitwise XOR operation. Examples: Input: S = "110" Output: 1?Explanation: Since S = 110 is the binary representation of 6, N = 6. On choosing X = 1, N becomes 6 ? ?6 / 21? = 5.
4 min read
Find unit digit of x raised to power y
Given two numbers x and y, find unit digit of xy. Examples : Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6. Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow for slightly larger values of x and y.Method 2 (Eff
11 min read
Find value of y mod (2 raised to power x)
Given two positive integer x and y. we have to find the value of y mod 2x. That is remainder when y is divided by 2x. Examples: Input : x = 3, y = 14 Output : 6 Explanation : 14 % 23 = 14 % 8 = 6. Input : x = 4, y = 14 Output : 14 Explanation : 14 % 24 = 14 % 16 = 14. To solve this question we can use pow() and modulo operator and can easily find t
7 min read
Article Tags :
Practice Tags :