-1

Here is my code for trying to solve Project Euler's third problem in Python:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

For some reason, this doesn't give an answer. I know that there is a double for loop, which is quite inefficient, but I don't know if the program is still running and trying to compute, or if there is an error. It might even be the break in the nested for loop that breaks all of the for loops. How can I be more efficient with this code? I'm not asking for a direct complete rewrite of my code, but little bits and pieces would be great. (And a few hints as well....!)

def prime_selector(primed_num):
    factors = []
    prime_factors = []
    for x in range(1, primed_num + 1):
        if primed_num % x == 0:
            factors.append(x)
    for i in factors:
        if i > 1:
            for j in range(2, i):
                if i % j == 0:
                    break
            else:
                prime_factors.append(i)
    prime_factors.sort(reverse=True)
    print(prime_factors[0])


prime_selector(600851475143)
AKX
  • 93,995
  • 11
  • 81
  • 98
Mint Studios
  • 102
  • 8
  • Surely you can add `print()` statements within those for loops to track their progress. :) – AKX Sep 08 '20 at 17:40
  • It is not that it is endless, but you are trying to count to 600,851,475,143 which is too much, even for modern computers. You need to come up with a better tactic. Have you looked into trial division? – Christian Sloper Sep 08 '20 at 17:43
  • 1
    and a hint: for any composite number n, there can be only one prime factor above sqrt(n) . Try to find all the others. – Christian Sloper Sep 08 '20 at 17:44
  • There are many interesting ideas in https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n and https://stackoverflow.com/questions/2211990/how-to-implement-an-efficient-infinite-generator-of-prime-numbers-in-python – Thierry Lathuille Sep 08 '20 at 17:51
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). In particular, we expect you to do appropriate research before posting here. There are *many* sites that teach you to find the prime factors of a number, including code in the language of your choice. – Prune Sep 08 '20 at 17:54
  • Yes, I know, but I wanted to try to make something for myself. – Mint Studios Sep 08 '20 at 17:57
  • Christian Sloper, when you mean sqrt(n), should I just take out the portion for the sqrt for the big number with an if statement? – Mint Studios Sep 08 '20 at 17:59
  • You only need to check up to the square root of the number to find all factors but one. – Christian Sloper Sep 08 '20 at 18:05
  • Christian Sloper, could you put your comment as an answer? It was my solution. – Mint Studios Sep 09 '20 at 18:13

1 Answers1

0

I suggest trying a this differently. For example the number 825. It is 355*11. The largest prime is 11. but you can get that with a different method.

825 can't be divided by 2, but can by 3. so 825/3=275.

275 can't be divided 3 or 4 but can by 5. 275/5=55.

55 again 5. 55/5=11.

11 can't be divided by any number larger then 5 that's not itself. therefore it is a prime.

This is a hint-ish answer. I can give you the code if you will need it tho.