Created
March 3, 2013 15:48
-
-
Save omz/5076605 to your computer and use it in GitHub Desktop.
Primes
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
# Demo of how to generate a list of prime numbers and cache them | |
# in a data file for fast access (using the marshal module). | |
def gen_primes(n): | |
# Source: http://code.activestate.com/recipes/366178-a-fast-prime-number-list-generator/#c19 | |
s = range(0, n+1) | |
s[1] = 0 | |
bottom = 2 | |
top = n // bottom | |
while (bottom * bottom <= n): | |
while (bottom <= top): | |
if s[top]: | |
s[top * bottom] = 0 | |
top -= 1 | |
bottom += 1 | |
top = n // bottom | |
return [x for x in s if x] | |
def load_primes(n): | |
import marshal | |
filename = 'primes_' + str(n) | |
try: | |
# Load cached primes file: | |
with open(filename, 'r') as f: | |
return marshal.load(f) | |
except: | |
# Generate primes and save to file: | |
primes_list = gen_primes(n) | |
with open(filename, 'w') as f: | |
marshal.dump(primes_list, f) | |
return primes_list | |
def main(): | |
primes = load_primes(104729) #Happens to be the 10,000th prime number | |
print primes | |
print len(primes), 'prime numbers loaded.' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment