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++
// 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
Output:
46
Recommended Posts:
- Find the value of f(n) / f(r) * f(n-r)
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find larger of x^y and y^x
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
- Find (a^b)%m where 'b' is very large
- Find maximum value of x such that n! % (k^x) = 0
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Find the sum of all multiples of 2 and 5 below N
- Find the value of ln(N!) using Recursion
- Find minimum x such that (x % k) * (x / k) == n
- Find minimum x such that (x % k) * (x / k) == n | Set-2
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.



