Python Forum
3rd problem from project Euler
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3rd problem from project Euler
#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


Messages In This Thread
3rd problem from project Euler - by Richard_SS - Jun-05-2019, 09:57 AM
RE: 3rd problem from project Euler - by DeaD_EyE - Jun-05-2019, 10:06 AM
RE: 3rd problem from project Euler - by Richard_SS - Jun-05-2019, 10:51 AM
RE: 3rd problem from project Euler - by perfringo - Jun-05-2019, 10:56 AM
RE: 3rd problem from project Euler - by Richard_SS - Jun-05-2019, 12:13 PM
RE: 3rd problem from project Euler - by heiner55 - Jun-05-2019, 01:55 PM
RE: 3rd problem from project Euler - by DeaD_EyE - Jun-05-2019, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 888 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Problem with importing python-telegram library into the project gandonio 1 1,647 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Calculate the Euler Number with more decimal places Pedroski55 10 4,672 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  IndexError in Project Euler attempt CRISPRmetoopls 3 2,380 Aug-10-2020, 10:00 AM
Last Post: perfringo
  graphing euler method rondo 1 2,545 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