0

How is it possible to calculate modulo of the following equation:

$3^{P^5} \mod P$

If $P$ is a really big prime number 174449211009120179071170527.

Thanks

emacs drives me nuts
  • 10,390
  • 2
  • 12
  • 31
hmffa
  • 1

1 Answers1

0

To find $a^M$ mod$p$, first write $M$ in binary, i.e. $$M=c_0+c_1 \times 2 + c_2 \times 2^2 + ... +c_n \times 2^n, c_i=0 \text { or }1,0 \le i\le n $$ The $c_i$ are easy to calculate-just keep dividing by 2 and taking the remainder. Then $$a^M=a^{c_0} \times a^{c_1 \times 2} \times a^{c_2 \times 2^2} \times ... \times a^{c_n \times 2^n}$$ and $$a^M \mod p=a^{c_0} \mod p \times a^{c_1 \times 2} \mod p \times a^{c_2 \times 2^2} \mod p\times ... \times a^{c_n \times 2^n}\mod p$$. Note that $$a^{2^{i+1}}=(a^{2^i})^2$$ and $$a^{2^{i+1}}\mod p=(a^{2^i}\mod p)^2$$ Thus all you need to do is start with $result$ as 1 and $powerofa$ as $a$ and keep track of $powerofa= a^{2^i}$, each time squaring the previous $powerofa$ and leaving the previous $result$ as is if $c_i=0$ or multipling it by $powerofa$ if $c_i=1$, making all calculations mod $p.$ Such a program is easy to write in the language of your choice, fast to execute and uses little storage space.

P. Lawrence
  • 5,674