Given four numbers A, B, C and M, where M is prime number. Our task is to find ABC (mod M).
Input : A = 2, B = 4, C = 3, M = 23 Output : 6 243(mod 23) = 6
A Naive Approach is to calculate res = BC and then calculate Ares % M by modular exponential. The problem of this approach is that we can’t apply directly mod M on BC, so we have to calculate this value without mod M. But if we solve it directly then we will come up with the large value of exponent of A which will definitely overflow in final answer.
An Efficient approach is to reduce the BC to a smaller value by using the Fermat’s Little Theorem, and then apply Modular exponential.
According the Fermat's little a(M - 1) = 1 (mod M) if M is a prime. So if we rewrite BC as x*(M-1) + y, then the task of computing ABC becomes Ax*(M-1) + y which can be written as Ax*(M-1)*Ay. From Fermat's little theorem, we know Ax*(M-1) = 1. So task of computing ABC reduces to computing Ay What is the value of y? From BC = x * (M - 1) + y, y can be written as BC % (M-1) We can easily use the above theorem such that we can get A ^ (B ^ C) % M = (A ^ y ) % M Now we only need to find two things as:- 1. y = (B ^ C) % (M - 1) 2. Ans = (A ^ y) % M
Time Complexity: O(log(B) + log(C))
Auxiliary space: O(1)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find value of y mod (2 raised to power x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- How to compute mod of a big number?
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Compute n! under modulo p
- Euler's criterion (Check if square root under modulo p exists)
- Number Theory | Generators of finite cyclic group under addition
- Multiply large integers under large modulo
- Equalizing array using increment under modulo 3
- Expressing a fraction as a natural number under modulo 'm'
- Compute power of power k times % m
- Largest power of k in n! (factorial) where k may not be prime
- Count of distinct power of prime factor of N
- Sum of each element raised to (prime-1) % prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X