Find 2^(2^A) % B

Given two integers A and B, the task is to calculate 22A % B.

Examples:

Input: A = 3, B = 5
Output: 1
223 % 5 = 28 % 5 = 256 % 5 = 1.

Input: A = 10, B = 13
Output: 3

Approach: The problem can be efficiently solved by breaking it into sub-problems without overflow of integer by using recursion.

Let F(A, B) = 22A % B.
Now, F(A, B) = 22A % B
= 22 * 2A – 1 % B
= (22A – 1 + 2A – 1) % B
= (22A – 1 * 22A – 1) % B
= (F(A – 1, B) * F(A – 1, B)) % B
Therefore, F(A, B) = (F(A – 1, B) * F(A – 1, B)) % B.
The base case is F(1, B) = 221 % B = 4 % B.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
  
// Function to return 2^(2^A) % B
ll F(ll A, ll B)
{
  
    // Base case, 2^(2^1) % B = 4 % B
    if (A == 1)
        return (4 % B);
    else
    {
        ll temp =  F(A - 1, B);
        return (temp * temp) % B;
    }
}
  
// Driver code
int main()
{
    ll A = 25, B = 50;
  
    // Print 2^(2^A) % B
    cout << F(A, B);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
class GFG 
    // Function to return 2^(2^A) % B 
    static long F(long A, long B) 
    
      
        // Base case, 2^(2^1) % B = 4 % B 
        if (A == 1
            return (4 % B); 
        else
        
            long temp = F(A - 1, B); 
            return (temp * temp) % B; 
        
    
      
    // Driver code 
    public static void main(String args[]) 
    
        long A = 25, B = 50
      
        // Print 2^(2^A) % B 
        System.out.println(F(A, B)); 
    
  
// This code is contributed by Ryuga 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return 2^(2^A) % B
def F(A, B):
  
    # Base case, 2^(2^1) % B = 4 % B
    if (A == 1):
        return (4 % B);
    else:
        temp = F(A - 1, B);
        return (temp * temp) % B;
  
# Driver code
A = 25;
B = 50;
  
# Print 2^(2^A) % B
print(F(A, B));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
class GFG
{
      
// Function to return 2^(2^A) % B
static long F(long A, long B)
{
  
    // Base case, 2^(2^1) % B = 4 % B
    if (A == 1)
        return (4 % B);
    else
    {
        long temp = F(A - 1, B);
        return (temp * temp) % B;
    }
}
  
// Driver code
static void Main()
{
    long A = 25, B = 50;
  
    // Print 2^(2^A) % B
    System.Console.WriteLine(F(A, B));
}
}
  
// This code is contributed by mits

chevron_right


PHP

Output:

46


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.