简体   繁体   English

Python找到主要因素

[英]Python find Prime Factors

So I need to find the prime factors of a number using a python program and I am able to find all the factors using: 因此,我需要使用python程序查找数字的素因,并且能够使用以下方法查找所有因数:

def primeFactors(n):
    list = []
    for x in range(2,n//2):
       if n % x == 0:
           list.append(x)
return list

But I don't understand how I'm suppose to make the program ignore factors that are a multiple of the prime factors. 但是我不明白如何使程序忽略主要因素的倍数。

I found the following code: 我发现以下代码:

def primes(n):
divisors = [ d for d in range(2,n//2+1) if n % d == 0 ]
return [ d for d in divisors if \
         all( d % od != 0 for od in divisors if od != d ) ]

But I don't actually understand what it does, and this is for an assignment, so I can't just copy and paste. 但是我实际上不了解它的作用,这是为了完成作业,所以我不能只是复制和粘贴。

So I was wondering if someone could lead me in the right direction of what I'm suppose to do. 所以我想知道是否有人可以引导我朝着我应该做的正确方向发展。 Also I can't use any fancy functions it has to be done mainly using the built in stuff like loops and lists and basic math. 我也不能使用任何花哨的功能,它主要是使用诸如循环和列表以及基本数学之类的内置功能来完成的。 And I'm using python 2.7 not 3.0. 我正在使用python 2.7而不是3.0。

Divisors gets all of the possible divisors, same as you have done in your code. 除数可以获取所有可能的除数,就像您在代码中所做的一样。 The value returned by 传回的值

[ d for d in divisors if \
         all( d % od != 0 for od in divisors if od != d ) ]

Keeps a divisor d if for all other possible divisors d is not divisible by any other divisor. 如果对于所有其他可能的除数d不能被任何其他除数整除,则保留除数d all returns true if all values passed to it are true, and the expression inside all simply checks if a given divisor d is not a divisor of any other divisor od in your list. 如果传递给它的所有值都为true,则all返回true,并且all内部的表达式仅检查给定的除数d是否为列表中其他除数od除数。 In this way the final list of values returned only includes the divisors that are not a multiple of the factors already present. 这样,最终返回的值列表仅包含除数不是已经存在的因子的倍数。

def primes(n):
    divisors = [ d for d in range(2,n//2+1) if n % d == 0 ]
    return [ d for d in divisors if \
         all( d % od != 0 for od in divisors if od != d ) ]

Use an example, say n = 12 divisors will be the following list [2, 3, 4, 6] 举例来说,假设n = 12个除数将是以下列表[2、3、4、6]

Now the return part is a bit tricky and for good reason, one shouldn't be abusing list comprehension in such a way. 现在return部分有点棘手,并且有充分的理由,不应以这种方式滥用列表理解。

So lets turn this part into an actual loop: 因此,让我们将这部分变成一个实际的循环:

ans = []
for d in divisors:
    for od in divisors:
        if od != d:
            if d % od == 0:
                break
    else: ans.append(d)
return ans

Yes the else is in the right spot 是的,其他都在正确的位置

Continuing with the example, what this part does is to search through the list of divisors and to see if any of them is divisible by any of the others. 继续该示例,该部分要做的是搜索除数列表,并查看除数是否可被除数所除。 If this is so, we ignore that one, otherwise we keep it. 如果是这样,我们将忽略该消息,否则将其保留。

The end result is a list of prime factors. 最终结果是主要因素列表。


I also mentioned in my comment that the divisors list can also be generated using: 我在评论中还提到除数列表也可以使用以下方法生成:

divisors = [2] + [ d for d in xrange(3,int(n**2+1), 2) if n % d == 0]

This method is useful because it avoids including even numbers except 2, which is the only even prime. 该方法很有用,因为它避免包括除2以外的偶数,后者是唯一的偶数质数。 Also the size of divisors is smaller because we only check up to the square root of a number which is most times smaller than the number divided by 2 除数的大小也较小,这是因为我们只检查最大比该数字除以2的数字大二的平方根

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

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