Skip to content
Related Articles

Related Articles

Improve Article

Find 2^(2^A) % B

  • Difficulty Level : Easy
  • Last Updated : 22 Apr, 2021
Geek Week

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

Examples:  

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

Input: A = 10, B = 13 
Output:
 

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




// 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;
}

Java




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

Python3




# 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

C#




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

PHP




<?php
// PHP implementation of the approach
  
// Function to return 2^(2^A) % B
function 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
echo F($A, $B);
 
// This code contributed by Rajput-Ji

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return 2^(2^A) % B
function F(A, B)
{
 
    // Base case, 2^(2^1) % B = 4 % B
    if (A == 1)
        return (4 % B);
    else
    {
        var temp =  F(A - 1, B);
        return (temp * temp) % B;
    }
}
 
// Driver code
var A = 25, B = 50;
 
// Print 2^(2^A) % B
document.write( F(A, B));
 
// This code is contributed by noob2000
 
</script>
Output: 
46

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :