Stuck on Project Euler #3 in python

前端 未结 9 2137
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 09:25

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

Ok, so i am working on projec

相关标签:
9条回答
  • 2020-12-20 10:02
    • the number 600851475143 is big to discourage you to use brute-force.
    • the oddNumbers function in going to put 600851475143 / 2 numbers in odd_list, that's a lot of memory.
    • checking that a number can be divided by an odd number does not mean the odd number is a prime number. the algorithm you provided is wrong.
    • there are a lot of mathematical/algorithm tricks about prime numbers, you should and search them online then sieve through the answers. you might also get to the root of the problem to make sure you have squared away some of the issues.

    you could use a generator to get the list of odds (not that it will help you):

    odd_list = xrange(1, number+1, 2)
    

    here are the concepts needed to deal with prime numbers:

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

    if you are really stuck, then there are solutions already there:

    • Project Euler #3, infinite loop on factorization
    • Project Euler 3 - Why does this method work?
    • Project Euler Question 3 Help
    0 讨论(0)
  • 2020-12-20 10:06

    The simple solution is trial division. Let's work through the factorization of 13195, then you can apply that method to the larger number that interests you.

    Start with a trial divisor of 2; 13195 divided by 2 leaves a remainder of 1, so 2 does not divide 13195, and we can go on to the next trial divisor. Next we try 3, but that leaves a remainder of 1; then we try 4, but that leaves a remainder of 3. The next trial divisor is 5, and that does divide 13195, so we output 5 as a factor of 13195, reduce the original number to 2639 = 13195 / 5, and try 5 again. Now 2639 divided by 5 leaves a remainder of 4, so we advance to 6, which leaves a remainder of 5, then we advance to 7, which does divide 2639, so we output 7 as a factor of 2639 (and also a factor of 13195) and reduce the original number again to 377 = 2639 / 7. Now we try 7 again, but it fails to divide 377, as does 8, and 9, and 10, and 11, and 12, but 13 divides 2639. So we output 13 as a divisor of 377 (and of 2639 and 13195) and reduce the original number again to 29 = 377 / 13. As this point we are finished, because the square of the trial divisor, which is still 13, is greater than the remaining number, 29, which proves that 29 is prime; that is so because if n=pq, then either p or q must be less than, or equal to the square root of n, and since we have tried all those divisors, the remaining number, 29, must be prime. Thus, 13195 = 5 * 7 * 13 * 29.

    Here's a pseudocode description of the algorithm:

    function factors(n)
        f = 2
        while f * f <= n
            if f divides n
                output f
                n = n / f
            else
                f = f + 1
        output n
    

    There are better ways to factor integers. But this method is sufficient for Project Euler #3, and for many other factorization projects as well. If you want to learn more about prime numbers and factorization, I modestly recommend the essay Programming with Prime Numbers at my blog, which among other things has a python implementation of the algorithm described above.

    0 讨论(0)
  • 2020-12-20 10:11
    i = 3
    factor = []
    number = 600851475143
    while i * i <= number:
        if number % i != 0:
            i += 2
        else:
            number /= i
            factor.append(i)
    while number >= i:
        if number % i != 0:
            i += 2
        else:
            number /= i
            factor.append(i)
    print(max(factor))
    
    0 讨论(0)
提交回复
热议问题