思路:定义一个变量index来追n,从2开始判断,若是素数index就+1,直到判断到第n个
import math
def nthPrime(n):
index = 1
result = 2
while index <= n:
flag = 1 #先假定是质数
#判断是否为素数
for i in range(2,int(math.sqrt(result))+1):
if result % i == 0:
flag = 0
break
if flag:
index += 1
result += 1
return result - 1