Problem 7:10001st prime

原题地址:http://projecteuler.net/problem=7

10001st prime

Problem 7

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?


大意是:

前六个质数是2,3,5,7,11和13,其中第6个是13.

第10001个质数是多少?



解法1:

序号初始为1,从2开始递增,判断它是否是质数,如果是则序号加1,直到序号达到10001

python代码如下所示:

import math

def isPrime(num):
    num = int(num) 
    temp = math.floor(math.sqrt(num))+1
    if num == 2:
        return True
    while temp > 1:
         if num%temp == 0:
             return False
         temp -= 1
    else:
         return True

def calculate(num):
    order = 1
    number = 2
    while order < num:
        while not isPrime(number):
            number += 1
        order += 1
        number +=1
    return number-1
        
print calculate(10002)




注:题目的中文翻译源自http://pe.spiritzhang.com

你可能感兴趣的:(python,projecteuler)