# Find 2^(2^A) % B in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program to evaluate the equation 2^(2^A) % B.

We are going to find the value of the equation using a recursive function. Let's see the steps to solve the problem.

• Write a recursive function that takes 2 arguments A and B.

• If A is 1, then return 4 % B as 2^(2^1) % B = 4 % B.

• Else recursively call the function with A-1 and b.

• Return the result^2%B.

• Print the solution

## Example

Let's see the code.

Live Demo

#include <bits/stdc++.h>
using namespace std;
long long solveTheEquation(long long A, long long B) {
// 2^(2^1) % B = 4 % B
if (A == 1) {
return (4 % B);
}
else {
long long result = solveTheEquation(A - 1, B);
return result * result % B;
}
}
int main() {
long long A = 37, B = 467;
cout << solveTheEquation(A, B) << endl;
return 0;
}

## Output

If you execute the above code, then you will get the following result.

113

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.