简体   繁体   English

寻找一个数的最大素因数的python代码问题(Project Euler问题3)

[英]Problem with python code for finding largest prime factor of a number (Project Euler problem 3)

I'm currently trying to solve Project Euler problem 3 using python, which is trying to find the largest prime factor of a number.我目前正在尝试使用 python 解决Project Euler 问题 3 ,它试图找到一个数字的最大质因数。 The method I'm using is essentially brute forcing my way through each integer less than the integer in question, checking if it's a factor of said integer, and then checking if it is prime.我使用的方法本质上是强制我通过每个小于所讨论的整数的整数,检查它是否是所述整数的一个因子,然后检查它是否是素数。 However, for some reason, my code doesn't seem to be working.但是,由于某种原因,我的代码似乎不起作用。

I've tried to use create a function which goes through each integer (i) less than a given integer (n), and if i is divisible by n, the function then proceeds to check if i is a prime number by going through every integer less or equal to i (x).我尝试使用创建一个函数,该函数遍历小于给定整数 (n) 的每个整数 (i),如果 i 可被 n 整除,则该函数将继续通过遍历每个整数来检查 i 是否为质数小于或等于 i (x) 的整数。 if x is a factor of i, then the value is added to a zero-integer defined as (a).如果 x 是 i 的因子,则该值将添加到定义为 (a) 的零整数中。 After that, if a ends up adding to equal i + 1, then that means the factor is prime, since the only numbers divisible were itself and 1. The code is as below:之后,如果 a 最终加起来等于 i + 1,那么这意味着该因子是素数,因为唯一可整除的数字是它本身和 1。代码如下:

def primefactor(n):
    for i in range(1, n+1): #goes through each integer less than n
        if n % i == 0: #checks if integer is a factor
            for x in range(1, i+1):  #another loop to check if the factor is prime
                a = 0
                primenumbers = []
                if i % x == 0:
                    a += x
                if a == i + 1:
                    primenumbers.append(i)
    print(primenumbers)

primefactor(13195)

What i expected the output to be was for it to print a list of all of the prime factors of the number, in this case, [5, 7, 13, 29] , but instead, all I got was an empty list, []我期望的输出是它打印一个数字的所有质因数的列表,在这种情况下, [5, 7, 13, 29] ,但相反,我得到的只是一个空列表, []

This issue with your code is you assign primenumbers to an empty list each iteration with primenumbers = [] .您的代码的这个问题是您每次使用primenumbers = []primenumbers分配给一个空列表。

Also if you don't want to brute force it, a good method to solve solutions like this is to google a formula such as Formula for primes and you will find:此外,如果您不想强制使用它,解决此类解决方案的一个好方法是在谷歌上搜索一个公式,例如质数公式,您会发现:

By Wilson's theorem, n+1 is prime if and only if n! mod(n + 1) = n根据威尔逊定理, n+1是素数当且仅当n! mod(n + 1) = n n! mod(n + 1) = n . n! mod(n + 1) = n

So you can do something like this:所以你可以做这样的事情:

# This is just used as caching to make it faster
# it is not needed.
from functools import lru_cache
from math import factorial

@lru_cache()
def isprime(x):
    n = x - 1
    if n == factorial(n) % (n + 1):
        return True
    else:
        return False

def primefactor(n):
    primenumbers = []
    for i in range(1, n + 1): #goes through each integer less than n
        if n % i == 0: #checks if integer is a factor
            isprime(i) and primenumbers.append(i)
    print(primenumbers)

primefactor(13195)

Output:输出:

[1, 5, 7, 13, 29]

There are also significantly faster solutions then this such that does go through all numbers 0 to n : Algorithm to find Largest prime factor of a number还有明显更快的解决方案,这样确实会遍历所有数字 0 到n找到数字的最大素因数的算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM