简体   繁体   English

找到 13195 的最大素数

[英]Finding the largest prime factor of 13195

I'm trying to solve a problem from the Project Euler archive: https://projecteuler.net/problem=3我正在尝试解决 Project Euler 存档中的一个问题: https://projecteuler.net/problem=3

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? 13195 的质因数是 5、7、13 和 29。数字 600851475143 的最大质因数是多少?

I tried solving for 13195 first.我尝试先解决 13195。 My original idea was to create a list with all the prime numbers lower than the given number.我最初的想法是创建一个包含所有低于给定数字的素数的列表。 I used the Sieve of Erathostenes algorithm to do this.我使用 Erathostenes 算法的筛子来做到这一点。 Then, using a for loop I iterated through all those prime numbers adding the ones that are factors of the given number to a separate list.然后,使用 for 循环,我遍历所有这些素数,将作为给定数字的因子的素数添加到单独的列表中。 I sorted the list and printed the largest factor.我对列表进行了排序并打印了最大的因素。 When I ran the script I got one extra number in the factors list that was not a factor of the given number.当我运行脚本时,我在因子列表中得到了一个额外的数字,它不是给定数字的因子。

for num = 13915 the output should have been [13, 29, 5, 7] but instead I got [13, 29, 377, 5, 7]对于num = 13915 ,output 应该是 [13, 29, 5, 7] 但我得到的是 [13, 29, 377, 5, 7]

I wasn't able to figure out where 377 came from.我无法弄清楚 377 的来源。 I'm aware my solution is not the most efficient one but I tried solving this problem myself.我知道我的解决方案不是最有效的,但我尝试自己解决这个问题。 So my questions are:所以我的问题是:

Where did 377 come from? 377 是从哪里来的? Can you suggest a more efficient way to solve this problem?你能提出一个更有效的方法来解决这个问题吗?

I apologize for any inconveniences as I'm new to problem solving in Python.对于给您带来的任何不便,我深表歉意,因为我是 Python 中问题解决的新手。

primes = []
num = 13195
factors = []
for i in range(1, num + 1):
    #sieve of erathostenes
    if i % 2 != 0 and i % 3 != 0 and i % 5 != 0 and i % 7 != 0 and i != 1:
        primes.append(i)

if num >= 10:
    primes.append(2)
    primes.append(3)
    primes.append(5)
    primes.append(7)

sorted(primes)

for i in range(len(primes)):
    prime = primes[i]
    if num % prime == 0:
        factors.append(primes[i])


print(factors)
factors.sort()
print(factors[len(factors) - 1])

You don't need to generate primes.您不需要生成素数。 A direct factorisation of the number will be more efficient:数字的直接因式分解将更有效:

def primeFactors(N):  # generator for all prime factors
    p = 2
    while p*p<=N:       # only need up to √N (remaining N)
        while N%p == 0: # p is a factor (will be a prime)
            yield p     # return primes as many times as they appear
            N //= p     # remove the prime factor from the number
        p += 1 + p%2    # next potential prime
    if N>1: yield N     # beyond √N whatever remains is a prime factor

output: (take the maximum factor found) output:(取找到的最大因子)

print(max(primeFactors(600851475143))) # 6857

use this to get the factors of a number sorted from largest to smallest使用它来获取从最大到最小排序的数字的因子

number = 13915
list_of_factors=sorted([int(number/n) for n in range(2,number//2 +1) if number % n ==0 ],reverse=True) 

result is [2783, 1265, 605, 253, 121, 115, 55, 23, 11, 5]结果是 [2783, 1265, 605, 253, 121, 115, 55, 23, 11, 5]

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

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