Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中跳转大数时出现溢出错误_Python_Integer Overflow - Fatal编程技术网

在Python中跳转大数时出现溢出错误

在Python中跳转大数时出现溢出错误,python,integer-overflow,Python,Integer Overflow,我试图用一段代码来寻找Wilson素数,以获得一点乐趣,并让我回到编码的摇摆中,然而,当我尝试除以172时,我发现了这一点+1乘173它给我一个溢出错误。以下是我正在使用的代码: import math x = 2 while x < 1000: if math.factorial(x-1) + 1 % x == 0 and (math.factorial(x-1) + 1 / 5) % x == 0 : print(x) x += 1 当我跑步时,我会:

我试图用一段代码来寻找Wilson素数,以获得一点乐趣,并让我回到编码的摇摆中,然而,当我尝试除以172时,我发现了这一点+1乘173它给我一个溢出错误。以下是我正在使用的代码:

import math
x = 2
while x < 1000:
    if math.factorial(x-1) + 1 % x == 0 and (math.factorial(x-1) + 1 / 5) % x == 0 :
        print(x)
    x += 1
当我跑步时,我会:

五,

十三,

溢出错误:整数除法结果对于浮点来说太大


我更改了代码,发现当数字173用作x时,就会出现错误。有人能告诉我为什么会这样吗?我环顾四周,只发现答案是python中使用的数字大小没有限制。提前感谢

问题不是阶乘,而是你的计算

(math.factorial(x-1) + 1 / 5) % x
因为x是一个整数,所以阶乘返回一个整数。但是,Python 3中的1/5返回浮点值0.2。将整数添加到浮点会返回一个整数,因此Python尝试将阶乘转换为浮点

然而,Python3的整数可以是任意大小的,但浮动不是这样的。浮点值仅限于计算机的数字处理器,通常为8字节长,并且具有最大大小。超出该大小,因此Python返回一个错误

如果你想在阶乘上加一,然后把这个和除以5,然后用x取模,你应该加括号,使用整数除法操作符//而不是浮点除法操作符/。我不知道你想做什么,所以我无法为你更正代码。但是试试//操作符