关于 Python 中大数模数的问题

Question about modulus of large numbers in Python

我使用费马小定理发现 40^65 % 7 = 3。但是当我在 Python 中使用以下代码时,它打印的答案是 2.0:

print((math.pow(40,65) % 7))

为什么 Python 给出的结果不正确为 2.0?

谢谢

math.pow(40,65) returns 一个浮点数,这是一个近似值。

试试 (40**65) % 7

一旦您对数学的工作感到满意,您可以使用内置函数 pow 来计算幂和 mods 的组合:

pow(40, 65, 7)