'''
Created on 2014年8月30日
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?
@author: wxp2971
'''
from numpy import *
'''
Some useful facts:
1 is not a prime.
All primes except 2 are odd.
All primes greater than 3 can be written in the form 6k+/-1.
Any number n can have only one primefactor greater than n .
The consequence for primality testing of a number n is: if we cannot find a number f less than
or equal n that divides n then n is prime: the only primefactor of n is n itself
'''
# 以解答思路做的
def isPrime(n):
if n == 1:
return False
if n < 4 :
return True # 2 and 3 are prime
if n % 2 ==0:
return False
if n < 9 :
return True # we have already excluded 4,6 and 8.
if n % 3 ==0:
return False
r = floor(sqrt(n))
f = 5
while f <= r:
if n % f == 0:
return False
break
if n % (f + 2) == 0:
return False
break
f += 6
return True
limit = 10001
count = 1
candidate = 1
while count != limit:
candidate += 2
if isPrime(candidate):
count += 1
# print(candidate)
print(candidate)
Project Euler 7
最新推荐文章于 2022-01-29 12:04:12 发布