calculate mod using pow function python

前端 未结 3 959
自闭症患者
自闭症患者 2020-12-06 05:46

So, If i would like to calculate the value of 6^8 mod 5 using the pow function, what should I put in a line??

In the assumption that You don\'t need to import it fir

3条回答
  •  情歌与酒
    2020-12-06 06:19

    check the docs of pow:

    pow(6, 8, 5)
    

    does what you want.

    do not use a ** b % n! while this will give the correct result it will be by orders of magnitude slower if you do calculations for bigger numbers. pow will do the modulo operation in every step while ** will first do the exponentiation in the integers (which may result in a huge number) and take the modulus only at the end.

    now if you are interested in numbers that are bigger than 32 bit you may want to have a look at gmpy2 for even more speed.

提交回复
热议问题