prime.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from itertools import count, islice


def is_prime(n):
    """Is n prime? """
    if n == 1:
        return False

    for i in count(2):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return True


def primes():
    """Prime number generator"""
    return filter(is_prime, count(2))


def nth_prime(n):
    """Return the nth prime"""
    return next(islice(primes(), n - 1, None))

Comments


You're not logged in right now. Please login via GitHub to comment