简体   繁体   中英

Euler 3 Python. Putting the prime numbers into a list

Im still pretty new to python and I'm trying to get all of the prime numbers from 600851475143 into a list. However, I keep getting a random assortment of numbers in the list instead of the prime numbers. I'm not really sure where I am going wrong. Thank you for your time

import math
factors_list = []
prime_factors = []

def number_factors(s):
    s = int(math.sqrt(s))
    for num in range(2, s):
        for i in range(2, num):
            if (num % i) == 0:
                factors_list.append(num)
            else:
                prime_factors.append(num)

number_factors(600851475143)

print factors_list
print prime_factors

Currently you append to prime_factor every time if (num % i) == 0 . So, for example, if num=12 (not prime), and i=5 you'll do the append to prime_factor .

Instead, you should only append if it has no divisors at all, not just a single number doesn't divide evenly.

I'll warn you ahead of time though, this problem is not only about calculating prime numbers, but that 600851475143 is a very large number. So you should probably get your current code working as a learning exercise, but you'll need to rethink your approach to the full solution.

Here's a better algorithm for factoring n . I'll describe it in words, so you can work out the coding yourself.

1) Set f = 2. Variable f represents the current trial factor.
2) If f * f > n, then n must be prime, so output n and stop.
3) Divide n by f. If the remainder is 0, then f is a factor of n,
     so output f and set n = n / f, then return to Step 2.
4) Since the remainder in the prior step was not 0, set f = f + 1
     and return to Step 2.

For instance, to factor 13195, first set f = 2; the test in Step 2 is not satisfied, the remainder in Step 3 is 1, so in Step 4 set f = 3 and return to Step 2. Now the test in Step 2 is not satisfied, the remainder in Step 3 is 1, so in Step 4 set f = 4 and return to Step 2. Now the test in Step 2 is not satisfied, the remainder in Step 3 is 3, so in Step 4 set f = 5 and return to Step 2.

Now the test in Step 2 is not satisfied, but the remainder in Step 3 is 0, so 5 is a factor of 13195; output 5, set n = 2639, and return to Step 2. Now the test in Step 2 is not satisfied, the remainder in Step 3 is 4, so in Step 4 set f = 6 and return to Step 2. Now the test in Step 2 is not satisfied, the remainder in Step 3 is 5, so in Step 4 set f = 7 and return to Step 2.

Now the test in Step 2 is not satisfied, but the remainder in Step 3 is 0, so 7 is a factor of 2639 (and also of 13195); output 7, set n = 377, and return to Step 2. Now the test in Step 2 is not satisfied, the remainder in Step 3 is 6, so in Step 4 set f = 8 and return to Step 2. Continue in this way until f = 13.

Now the test in Step 2 is not satisfied, but the remainder in Step 3 is 0, so 13 is a factor of 377 (and also of 2639 and 13195); output 13, set n = 29, and return to Step 2. Here the test in Step 2 is satisfied, since 13 * 13 = 169 which is greater than 29, so 29 is prime, output it and halt. The final factorization is 5 * 7 * 13 * 29 = 13195.

The factorization of 600851475143 works in exactly the same way, except that it takes longer. There are better ways to factor integers. But this algorithm is simple, and is sufficient for PE3.

This will run quite slowly for large numbers. Consider the case in which the algorithm attempts to find the prime factors where num = 1000000. Your nested FOR loop will generate 1million operations before the next number is even considered!

Consider using the Sieve of Eratosthones to get all of the prime numbers up to a certain integer. It is not as efficient as certain other Sieves, but is easy to implement. Spend some time reading the theory behind the sieve before implementing--this will help your understanding of later problems.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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