OverflowError Python int 太大而无法转换为 C long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22114088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:16:03  来源:igfitidea点击:

OverflowError Python int too large to convert to C long

pythonpython-2.7

提问by Suhas

#!/usr/bin/python
import sys,math
n = input("enter a number to find the factors   :   ")
j,flag,b= 0l,False,0l
for b in xrange(1,n+1):
    a = n + (b*b)
    j = long(math.sqrt(a))
    if a == j*j:
        flag = True
        break
if flag:
    c = j+b
    d = j-b
    print "the first factor is   :   ",c ,"  and the second factor is   :   ",d

when I run this code it is throwing different types of errors for different inputs.

当我运行这段代码时,它会为不同的输入抛出不同类型的错误。

The following is the one kind of input

以下是一种输入

linux@terminal:~$ ./fermat.py
enter a number to find the factors   :   544564564545456
Traceback (most recent call last):
  File "./fermat.py", line 8, in <module>
    for b in range(1,n+1):
MemoryError

This is for second input

这是第二个输入

linux@terminal:~$ ./fermat.py
enter a number to find the factors   :   28888888888888888888888888888888888444444444444444444444444
Traceback (most recent call last):
  File "./fermat.py", line 8, in <module>
    for b in range(1,n+1):
OverflowError: range() result has too many items

And this is for third output

这是第三个输出

linux@terminal:~$ ./fermat.py
enter a number to find the factors   :   28888888888888888888888888888888888444444444444444444444444
Traceback (most recent call last):
  File "./fermat.py", line 8, in <module>
    for b in xrange(1,n+1):
OverflowError: Python int too large to convert to C long

Actually I was writing code for Fermat factorization to find the factors of a given number. And my requirement is even if give a hundred digit number as input it should give the output for that input number.

实际上,我正在编写费马因式分解的代码来查找给定数字的因数。我的要求是即使给出一百位数字作为输入,它也应该给出该输入数字的输出。

Is there any way to get rid this kind of problem? I am using Ubuntu with python 2.7.5+

有没有办法摆脱这种问题?我正在使用 Ubuntu 和 python 2.7.5+

回答by user2357112 supports Monica

Annoyingly, in Python 2, xrangerequires its arguments to fit into a C long. There isn't quite a drop-in replacement in the standard library. However, you don't quite need a drop-in replacement. You just need to keep going until the loop breaks. That means you want itertools.count, which is like an xrangethat just keeps going:

令人讨厌的是,在 Python 2 中,xrange需要将其参数放入 C long 中。标准库中没有完全替代品。但是,您并不完全需要直接替换。你只需要继续下去,直到循环breaks。这意味着您想要itertools.count,这就像一个xrange不断前进的:

import itertools
for b in itertools.count(1):
    ...

Also, note that your code has other bugs. It attempts to apply Fermat factorization to even numbers, but Fermat factorization doesn't work on even numbers. Additionally, it fails to consider the case where nis a square, so it won't work for n=9.

另外,请注意您的代码还有其他错误。它尝试将费马分解应用于偶数,但费马分解不适用于偶数。此外,它没有考虑 wheren是正方形的情况,因此它不适用于n=9.

回答by WhyAreYouReadingThis

Btw if you still want a factor function that works with big numbers, here:

顺便说一句,如果您仍然想要一个可以处理大数字的因子函数,请在这里:

from math import sqrt
def factors(n):
return set(reduce(list.__add__,
            ([i, n//i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0)))

So now you only need to say:

所以现在你只需要说:

>>> factors(largenumhere)

For a load of factors :D

对于大量因素:D