Program to find out the value of a power of 2 in Python

PythonServer Side ProgrammingProgramming

Suppose we are given two integer numbers p and q. We have to find out the value of 22^p mod q. The output has to be an integer.

So, if the input is like p = 5, q = 6, then the output will be 4

To solve this, we will follow these steps −

  • res := 2^(2^p) mod q
  • return res

Example

Let us see the following implementation to get better understanding −

def solve(p, q):
res = pow(2, 2 ** p, q)
return res
print(solve(5, 6))

Input

5, 6

Output

4
raja
Published on 20-Oct-2021 07:46:42
Advertisements