在python中处理任意大的数字

在python中处理任意大的数字,python,Python,我通过Euler项目来提高我的编程技能。在重新查看问题3的代码后,我遇到了一个有趣的问题。这是我的密码: # prime numbers are only divisible by unity and themselves # (1 is not considered a prime number by convention) def isprime(n): '''check if integer n is a prime''' # make sure n is a positi

我通过Euler项目来提高我的编程技能。在重新查看问题3的代码后,我遇到了一个有趣的问题。这是我的密码:

# prime numbers are only divisible by unity and themselves
# (1 is not considered a prime number by convention)
def isprime(n):
    '''check if integer n is a prime'''
    # make sure n is a positive integer
    n = abs(int(n))
    # 0 and 1 are not primes
    if n < 2:
        return False
    # 2 is the only even prime number
    if n == 2: 
        return True    
    # all other even numbers are not primes
    if not n & 1: 
        return False
    # range starts with 3 and only needs to go up the squareroot of n
    # for all odd numbers
    for x in range(3, int(n**0.5)+1, 2):
        if n % x == 0:
            return False
    return True


try:
    num = int(input('Please input a natural number:'))
except ValueError:
    print("Erm.. No. I need a number.")


mylist = []
check = True
newnum = num
i= 0


if isprime(num):
    print("%r is a prime number."%num)

else:
    while check:
        if isprime(i):
            if newnum % i ==0:
                mylist.append(i)
                print("%r is a prime factor of %r"%(i,num))
                newnum = newnum/i
                i=0

                if newnum ==1:
                    check = False

        if i==num:
            print("I guess the program broke.")
            check = False
        i+=1

    print ("The largest prime factor for %r is:"%num)
    print (max(mylist))
    print ("The list of prime factors for %r is:"%num)
    print (mylist)
质数只能被单位和自身整除 #(1按惯例不被视为质数) def iPrime(n): ''检查整数n是否为素数'' #确保n是一个正整数 n=abs(int(n)) #0和1不是素数 如果n<2: 返回错误 #2是唯一的偶数素数 如果n==2: 返回真值 #所有其他偶数都不是素数 如果不是n&1: 返回错误 #范围从3开始,只需要增加n的平方根 #对于所有奇数 对于范围(3,int(n**0.5)+1,2)内的x: 如果n%x==0: 返回错误 返回真值 尝试: num=int(输入('请输入一个自然数:')) 除值错误外: 打印(“呃……不,我需要一个号码。”) mylist=[] 检查=正确 newnum=num i=0 如果是iPrime(num): 打印(“%r”是质数。“%num) 其他: 检查时: 如果是(i): 如果newnum%i==0: mylist.append(一) 打印(“%r是%r”%(i,num)的素因子) newnum=newnum/i i=0 如果newnum==1: 检查=错误 如果i==num: 打印(“我想程序坏了。”) 检查=错误 i+=1 打印(“%r的最大素因子为:”%num) 打印(最大值(mylist)) 打印(“%r的基本因子列表为:”%num) 打印(mylist) 所以我遇到的问题是,这段代码将永远运行在超过17位的数字上(我怀疑任何高于144155188075855872的数字都是2^59;它适用于大约18位数字,而不是其他数字)

我发现,如果我输入一个比这个数字高的数字,并用Windows计算器检查答案,答案将非常接近整数,但它将有一个小数部分

如何更改函数以接受并正确计算任意大的数字?(最好不使用非标准库)


谢谢

Python整数是任意精度的。我在您的代码中看到的唯一一件可能无法在高精度下工作的事情是浮点计算:

int(n**0.5)+1
因为浮点数是近似值,所以对于大于64位浮点数所能精确表示的数值,您将得到舍入误差(大约发生在2到50左右)。相反,请使用整数计算:

for x in itertools.count(3, 2):
    if x > n ** 2:
        break
    if n % x == 0:
        return False

“正确”是什么意思?你使用Python2吗?或者3?@StefanoSanfilippo我会从
print
函数和
int(输入(
而不是
int(原始输入))假设Python 3.x(
。可能是这样,但它们在Python 2中都是有效的语句。或者你的意思是
x**2>n
?你可以使用
takewhile
any
来摆脱显式循环。我不确定它是否更具可读性,或者小的性能优势是否会更明显,但可能是。通过快速测试,titertools版本在CPython 3.3.2中似乎快了约2%,在PyPy 2.1.0b3/3.2.3中慢了12%,所以…没关系,你觉得哪个更容易阅读就更好。将
x**2>n
替换为
x*x>n
n%x==0
替换为
而在itertools版本中,
any(n%x==0代表…)
使用
并非所有(n%x表示…
)都会产生更大的差异。因此,我对其进行了测试。我不确定问题出在哪里,但它无法识别任何超过第一次初始it测试的内容。编辑:@Robᵩ 发现一个错误。在修复该错误并在号码987654321987654321上运行它之后,它仍然声称29是一个基本因子,但事实并非如此。请使用987654321987654321检查您的程序并回答。