[英]Question about modulus of large numbers in Python
I used Fermat's Little Theorem and found that 40^65 % 7 = 3. But when I use the following code in Python the answer it prints is 2.0: 我使用了Fermat的Little Theorem,发现40 ^ 65%7 = 3.但是当我在Python中使用以下代码时,它打印的答案是2.0:
print((math.pow(40,65) % 7))
Why does Python give the result incorrectly as 2.0? 为什么Python将结果错误地指定为2.0?
Thank you 谢谢
math.pow(40,65)
returns a float, which is an approximation. math.pow(40,65)
返回一个浮点数,这是一个近似值。
Try (40**65) % 7
instead. 尝试
(40**65) % 7
代替。
Once you're happy that the maths works, you can use the built-in function pow
to calculate powers and mods in combination: 一旦您对数学运算感到满意,您可以使用内置函数
pow
来组合计算幂和mod:
pow(40, 65, 7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.