Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
- Find the sum of all the primes below two million.
- '''
- def findPrimes(limit):
- primes = [2]
- for n in range(3, limit):
- isPrime = True
- for prime in primes:
- if n % prime == 0:
- # This means that prime is divisor of n ----> n is not prime ----> break
- isPrime = False
- break
- if isPrime:
- primes.append(n)
- return primes
- # MAIN FUNCTION
- from timeit import default_timer as timer
- LIMITPOWER = 6
- print("~~~~~~~~ EXECUTION TIME EXAMPLES ~~~~~~~~")
- for i in range(1, LIMITPOWER):
- start = timer()
- primes = findPrimes(10**i)
- end = timer()
- print("Time until finding primes below: " + str(10**i) + " ----> " + str(1000 * (end - start)) + " ms.")
- print("~~~~~~~~ END OF EXECUTION TIME EXAMPLES ~~~~~~~~")
- print()
- print()
- # I suppose that all this process has finished by finding all the primes needed
- # I have to return the sum
- LIMITNUMBER = 2 * (10 ** 6)
- primes = findPrimes(LIMITNUMBER)
- s = sum(primes)
- print("The sum of the primes below " + str(LIMITNUMBER) + " is: " + str(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement