Skip to content

Instantly share code, notes, and snippets.

@PythonCoderAS
Created May 5, 2018 01:47
Show Gist options
  • Select an option

  • Save PythonCoderAS/8806aa79a4422f700216bd5b061d982c to your computer and use it in GitHub Desktop.

Select an option

Save PythonCoderAS/8806aa79a4422f700216bd5b061d982c to your computer and use it in GitHub Desktop.
Easy way to find (and gen) prime numbers
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
f = 5
while f <= r:
if n%f == 0: return False
if n%(f+2) == 0: return False
f +=6
return True
def prime_gen(amt):
n = 1
cur = 0
while True:
if not is_prime(n):
n += 1
else:
yield n
n += 1
cur += 1
if cur == amt:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment