Why is pow(a, d, n) so much faster than a**d % n?

前端 未结 4 1490
终归单人心
终归单人心 2020-12-02 09:11

I was trying to implement a Miller-Rabin primality test, and was puzzled why it was taking so long (> 20 seconds) for midsize numbers (~7 digits). I eventually found the fol

4条回答
  •  没有蜡笔的小新
    2020-12-02 09:35

    There are shortcuts to doing modular exponentiation: for instance, you can find a**(2i) mod n for every i from 1 to log(d) and multiply together (mod n) the intermediate results you need. A dedicated modular-exponentiation function like 3-argument pow() can leverage such tricks because it knows you're doing modular arithmetic. The Python parser can't recognize this given the bare expression a**d % n, so it will perform the full calculation (which will take much longer).

提交回复
热议问题