Given two integers A and B, the task is to calculate 22A % B.
Input: A = 3, B = 5
223 % 5 = 28 % 5 = 256 % 5 = 1.
Input: A = 10, B = 13
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:
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find the value of f(n) / f(r) * f(n-r)
- Find K such that |A - K| = |B - K|
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find N from the value of N!
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find Kth smallest value for b such that a + b = a | b
- Find minimum x such that (x % k) * (x / k) == n
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Find sum of the series 1-2+3-4+5-6+7.......
- Find (a^b)%m where 'b' is very large
- Find the value of N XOR'ed to itself K times
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find the value of ln(N!) using Recursion
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9
- Find if the Vacation can be taken or not
- Find (a^b)%m where 'a' is very large
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.