Thứ Bảy, 16 tháng 4, 2016

[#7] 10001st prime

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?

def isPrime(n):
        if n == 2 or n == 3:
            return True
        if n < 2 or n % 2 == 0:
            return False
        if n < 9:
            return True
        if n % 3 == 0:
            return False
        r = int(n**0.5)
        f = 5
        while f <= r:
            if n % f == 0:
                return False
            if n % (f + 2) == 0:
                return False
            f += 6
        return True


def problem7(n):
        '''
        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 n'st prime number?
        '''
        if n == 1:
            return 2
        m = 1
        prime = 3
        while m != n:
            if isPrime(prime):
                m += 1
                result = prime
            prime += 2
        return result

ChiefdaThief

Author & Editor

Keep It Simple Stupid.

0 nhận xét:

Đăng nhận xét

Manual Categories