Skip to content

Instantly share code, notes, and snippets.

@raybsmith
Last active July 14, 2016 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raybsmith/32465f2881305fbdcc0f7f01a5abe7f1 to your computer and use it in GitHub Desktop.
Save raybsmith/32465f2881305fbdcc0f7f01a5abe7f1 to your computer and use it in GitHub Desktop.
Project Euler p10
import time
def is_prime(val, primes_smaller):
"""Determine if a number is prime given a list of all primes less than it."""
cutoff = int(val**(0.5))
val_primeness = True
for testval in primes_smaller:
if val - (val // testval)*testval == 0:
val_primeness = False
break
if testval >= cutoff:
break
return val_primeness
def primes_less_than(n):
"""Find all the primes less than the input."""
primes = [2]
for val in range(3,n+1):
if is_prime(val, primes):
primes.append(val)
return primes
def main():
"""Find the sum of all primes less than 2e6."""
print(sum(primes_less_than(int(2e6))))
# print(sum(primes_less_than(int(1e5))))
if __name__ == "__main__":
tstart = time.time()
main()
tend = time.time()
print("total time:", tend - tstart, "s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment