Skip to content Skip to sidebar Skip to footer

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 inste

Solution 1:

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.

Solution 2:

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 andstop.
3) Divide n by f. If the remainder is0, then f is a factor of n,
     so output f andset n = n / f, thenreturntoStep2.
4) Since the remainder in the prior step was not0, set f = f + 1andreturntoStep2.

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.

Solution 3:

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

Post a Comment for "Euler 3 Python. Putting The Prime Numbers Into A List"