Python Forum
3rd problem from project Euler
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3rd problem from project Euler
#1
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
-
I have written a programm for solving this problem, it easily displays prime factors of 13195, but once I insert a bigger number (like 600851475143), a programm goes into an infinite loop, it returns neither error, nor answer. Please help, where is the problem in code?

My code:

import math
a = []
b = range(1,600851475143)

def is_prime(n):
    if n == 2:
        return True
    if n % 2 == 0 or n <= 1:
        return False

    sqr = int(math.sqrt(n)) + 1

    for divisor in range(3, sqr, 2):
        if n % divisor == 0:
            return False
    return True

b = (filter(is_prime,b))

for i in b:
    if 600851475143 % i == 0:
        a.append(i)
    else:
        pass
print (a)
print("The largest prime factory is",a[-1::])
Reply
#2
You can improve the algorithm, if you define some primes.
Here an example:

def is_prime(n):
    primes = [
        2, 3, 5, 7, 11,
        13, 17, 19, 23,
        29, 31, 37, 41, 
        43, 47
    ]
    
    if n == 2:
        return True
    elif n % 2 == 0 or n <= 1:
        return False
    
    # speed up with known primes
    if n > 50 and any(n % p == 0 for p in primes):
        return False
 
    sqr = int(math.sqrt(n)) + 1
       
    for divisor in range(3, sqr, 2):
        if n % divisor == 0:
            return False
    return True
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Jun-05-2019, 10:06 AM)DeaD_EyE Wrote: You can improve the algorithm, if you define some primes. Here an example:
 def is_prime(n): primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ] if n == 2: return True elif n % 2 == 0 or n <= 1: return False # speed up with known primes if n > 50 and any(n % p == 0 for p in primes): return False sqr = int(math.sqrt(n)) + 1 for divisor in range(3, sqr, 2): if n % divisor == 0: return False return True 
I got the point, but, don't you think it is too slow way to solve the problem? My programm is too slow for that big integer, and I guess I should think up about completely different algorithm to solve it, should i? Or i can somehow improve this one?
Reply
#4
If I casually observe it then it seems too brute-force approach.

As far as I understand one don't need to know all primes in range. One need to go up on only int(600851475142 / 2). Over that there can't be suitable factors. It seems to me, that it's not effective to calculate all primes in range and then try to find which are dividers. There is no point finding and checking primes which are large than half of range.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Jun-05-2019, 10:56 AM)perfringo Wrote: If I casually observe it then it seems too brute-force approach. As far as I understand one don't need to know all primes in range. One need to go up on only int(600851475142 / 2). Over that there can't be suitable factors. It seems to me, that it's not effective to calculate all primes in range and then try to find which are dividers. There is no point finding and checking primes which are large than half of range.
You're right, i decided to take sqrt(x) of initial number, and i reduced execution time down to O(logn):D
Reply
#6
See similar thread:
https://python-forum.io/Thread-Juicy-hig...torization
Reply
#7
I think you forget, that for each call, the iterations are summed up.
This is interesting for big numbers.

If you check for example the number 179424673, the sum of total iterations is in the range of:
sum(int(math.sqrt(i)) + 1 for i in range(3, 179424673))
# one minute later....
Output:
1602346003985
Of course you won't hit the maximum possible iterations, because some values are sorted out
by dividing with already known primes.
Calling the same function very often, causes the problem.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 788 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Problem with importing python-telegram library into the project gandonio 1 1,571 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Calculate the Euler Number with more decimal places Pedroski55 10 4,509 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  IndexError in Project Euler attempt CRISPRmetoopls 3 2,313 Aug-10-2020, 10:00 AM
Last Post: perfringo
  graphing euler method rondo 1 2,501 May-03-2019, 01:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020