Skip to content

Instantly share code, notes, and snippets.

@mhtsai1010
Last active August 22, 2016 04:03
Show Gist options
  • Save mhtsai1010/c5f7e51c6095f0a33fcf to your computer and use it in GitHub Desktop.
Save mhtsai1010/c5f7e51c6095f0a33fcf to your computer and use it in GitHub Desktop.
Python: generate a prime list < n
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
def primes(n):
""" Returns a list of primes < n """
sieve = [True] * n
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment