Skip to content

Instantly share code, notes, and snippets.

@awwsmm
Last active September 14, 2018 13:55
Show Gist options
  • Save awwsmm/536c81fbfd2c86c9e6b750855f074201 to your computer and use it in GitHub Desktop.
Save awwsmm/536c81fbfd2c86c9e6b750855f074201 to your computer and use it in GitHub Desktop.
Get first N primes in a list
import math
import itertools
# number of primes to generate, beginning with 2, 3, 5, ...
N = 100
def firstNPrimes(N):
primes = [2]
prime = (x for x in itertools.count(2) if not any ([x%y==0 for y in range(2, int(math.ceil(math.sqrt(x)))+1)]))
for n in range(1, int(math.ceil(N))):
primes.append(next(prime))
if N > 0:
return primes
else:
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment