Skip to content

Instantly share code, notes, and snippets.

@SylvainDe SylvainDe/generate_primes.py Secret
Created Sep 2, 2015

Embed
What would you like to do?
import math
import itertools
def is_prime_alpha(n):
if n < 2:
return False
i = 2
while (i * i <= n):
if n % i == 0:
return False
i = i + 1
return True
def is_prime_beta(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def is_prime_gamma(n):
if n < 2:
return False
return not any(n % i == 0 for i in range(2, int(math.sqrt(n)) + 1))
def is_prime_delta(n):
if n < 2:
return False
return all(n % i for i in range(2, int(math.sqrt(n)) + 1))
def is_prime(n):
return is_prime_delta(n)
def n_prime_alpha(n):
i = 2
while n > 0:
if is_prime(i):
n = n - 1
if n == 0:
return i
i = i + 1
return -1
def n_prime_beta(n):
cand = 1
for i in range(n):
cand += 1
while not is_prime(cand):
cand += 1
return cand
def n_prime_gamma(n):
return nth(yield_primes(), n - 1)
def yield_primes(beg=0):
"""Yields prime number by checking them individually - not efficient."""
for i in itertools.count(beg):
if is_prime(i):
yield i
def nth(iterable, n, default=None):
"""Returns the nth item or a default value.
From http://stackoverflow.com/questions/12007820/better-ways-to-get-nth-element-from-an-unsubscriptable-iterable ."""
return next(itertools.islice(iterable, n, None), default)
def n_prime(n):
return n_prime_gamma(n)
print(n_prime(10001))
@adamop8

This comment has been minimized.

Copy link

commented Jan 7, 2019

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.