Skip to main content
 首页 » 编程设计

python之python 是否显示真正大整数的问题

2025年05月04日12dyllove98

我有一个代码可以计算 600851475143 的最高质因数。

def PRIME(a):                #Check if no is prime 
    f = 0 
    i = 2 
    while(i < a/2):          #No factor of a no can be greater than a/2 
        if (a % i == 0): 
            f = 1 
            break 
        i = i + 1 
 
    if(f == 1): 
        return 0 
    else: 
        return 1 
 
 
def PFIND(a): 
    for i in range(1, 100000):   #Iteratively check if the no is prime 
        if PRIME(a/2 - i):       #No factor of a no can be greater than a/2. 
            if (a % (a/2 - i) == 0): 
                return (a/2 - i) 
print PFIND(600851475143) 

但是代码不断运行,没有给出任何输出。

请您参考如下方法:

Python 对大整数的支持很好。我认为你的问题是你正在做非常慢的算法,用于蛮力查找因子和测试你的因子是否是质数。如果您只是颠倒这两个测试的顺序(即先测试它是否是一个因数,然后再测试它是否是质数),它会进行得更快。

但也许问题在于使用更复杂的算法。也许你应该使用 Rabin-Miller test而不是蛮力。