Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIFind 2^(2^A) % B

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

 

Time Complexity: O(A)
Auxiliary Space: O(A), due to recursive call stack

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments