Advertisement
makispaiktis

Project Euler 10 - Summation of primes

May 22nd, 2020 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. '''
  2. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  3. Find the sum of all the primes below two million.
  4. '''
  5.  
  6. def findPrimes(limit):
  7.     primes = [2]
  8.     for n in range(3, limit):
  9.         isPrime = True
  10.         for prime in primes:
  11.             if n % prime == 0:
  12.                 # This means that prime is divisor of n ----> n is not prime ----> break
  13.                 isPrime = False
  14.                 break
  15.         if isPrime:
  16.             primes.append(n)
  17.     return primes
  18.  
  19. # MAIN FUNCTION
  20. from timeit import default_timer as timer
  21. LIMITPOWER = 6
  22.  
  23. print("~~~~~~~~ EXECUTION TIME EXAMPLES ~~~~~~~~")
  24. for i in range(1, LIMITPOWER):
  25.     start = timer()
  26.     primes = findPrimes(10**i)
  27.     end = timer()
  28.     print("Time until finding primes below: " + str(10**i) + " ----> " + str(1000 * (end - start)) + " ms.")
  29. print("~~~~~~~~ END OF EXECUTION TIME EXAMPLES ~~~~~~~~")
  30. print()
  31. print()
  32.  
  33. # I suppose that all this process has finished by finding all the primes needed
  34. # I have to return the sum
  35. LIMITNUMBER = 2 * (10 ** 6)
  36. primes = findPrimes(LIMITNUMBER)
  37. s = sum(primes)
  38. print("The sum of the primes below " + str(LIMITNUMBER) + " is: " + str(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement