Last active
July 14, 2016 04:28
-
-
Save raybsmith/32465f2881305fbdcc0f7f01a5abe7f1 to your computer and use it in GitHub Desktop.
Project Euler p10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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