Putting Primes to Work, is_prime Extension


#1

def is_prime(x):
    if x < 2 or x % 2 == 0 and x != 2: return False
    if x > 2:
        for i in range(3, int(x ** 0.5) + 1, 2):
            if x % i == 0: return False
    return True

def nth_prime(n):
    n = abs(n) or 1
    i = 0
    while True:
        i += 1 if i == 2 else 2
        if is_prime(i):
            x = i
            n -= 1
            if n: continue
            else: break        
    return x

In other words, now that we know how to validate a prime it is time to put that skill to work.