Skip to content

Instantly share code, notes, and snippets.

@omz
Created March 3, 2013 15:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omz/5076605 to your computer and use it in GitHub Desktop.
Save omz/5076605 to your computer and use it in GitHub Desktop.
Primes
# 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