I am searching for an algorithm that allow me to compute (2^n)%d with n and d 32 or 64 bits integers.
The problem is that it’s impossible to store 2^n in memory even with multiprecision libraries, but maybe there exist a trick to compute (2^n)%d only using 32 or 64 bits integers.
Thank you very much.
Take a look at the Modular Exponentiation algorithm.
The idea is not to compute
2^n. Instead, you reduce modulusdmultiple times while you are powering up. That keeps the number small.Combine the method with Exponentiation by Squaring, and you can compute
(2^n)%din onlyO(log(n))steps.Here’s a small example:
2^130 % 123 = 40