Last active
September 14, 2018 13:55
-
-
Save awwsmm/536c81fbfd2c86c9e6b750855f074201 to your computer and use it in GitHub Desktop.
Get first N primes in a list
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 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