Skip to content

Latest commit

 

History

History
33 lines (28 loc) · 716 Bytes

7. 10001st prime.md

File metadata and controls

33 lines (28 loc) · 716 Bytes

Question

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?

Solution

def isprime(x):
    if x <= 1: return False
    if x <= 3: return True
    if x % 2 == 0 or x % 3 == 0: return False
    i = 5
    while i * i <= x:
        if x % i == 0 or x % (i+2) == 0:
            return False
        i += 6
    return True

def p7():
    count = 1 # count the unique even prime number 2
    p = 3
    while True:
        if isprime(p):
            count += 1
            if count == 10001:
                return p
        p += 2

Answer

104743