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:
- Program to find parity
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass
- Find a peak element
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- Find the maximum distance covered using n bikes
- Find if two rectangles overlap
- Find the number of zeroes
- Find the smallest number whose digits multiply to a given number n
- Find next greater number with same set of digits
- Find n'th number in a number system with only 3 and 4
- Find the missing number in Arithmetic Progression