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

时间:2023-02-03 18:20:22
#!/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.

实际上我正在为Fermat分解编写代码以找到给定数字的因子。我的要求是,即使给出一个百位数作为输入,它应该给出输入数字的输出。

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+

2 个解决方案

#1


39  

Annoyingly, in Python 2, xrange requires 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 xrange that just keeps going:

令人讨厌的是,在Python 2中,xrange要求其参数适合C long。标准库中没有完全替代品。但是,您不需要直接替换。你只需要继续前进,直到循环中断。这意味着你需要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 n is a square, so it won't work for n=9.

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

#2


0  

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

#1


39  

Annoyingly, in Python 2, xrange requires 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 xrange that just keeps going:

令人讨厌的是,在Python 2中,xrange要求其参数适合C long。标准库中没有完全替代品。但是,您不需要直接替换。你只需要继续前进,直到循环中断。这意味着你需要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 n is a square, so it won't work for n=9.

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

#2


0  

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