Commit af633381 authored by Taddeus Kroes's avatar Taddeus Kroes

Added some prime number util functions

parent 04b49530
def is_prime(n):
if n == 2:
return True
if n < 2 or not n & 1:
return False
for i in xrange(3, int(n ** .5) + 1, 2):
if not n % i:
return False
return True
def primes_until(n):
""" Sieve of Eratosthenes """
lst = [False] * n
i = 2
while i < n:
if not lst[i]:
yield i
for j in xrange(i, n, i):
lst[j] = True
i += 1
def nprimes(n):
a = 2
while n:
if is_prime(a):
yield a
n -= 1
a += 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment