Today in the group has the Netizen question:
Ask for a 1000th prime number starting with Python 0?
In fact, the algorithm itself is not difficult to find the prime number from the definition of prime numbers.
Prime: A number that can only be divisible by 0 and itself. Well, let's start writing a program (a small algorithm).
#coding =utf-8
#定义求质数的函数
def getprim (n):
#我们从3开始, improve efficiency, oh, very little
p=3
x=0 while
(x<n):
result=true for
i in range (2,p-1):
if (p%i==0):
result=false
if result==true:
x=x+1
rst=p
#注意: This adds 2 to improve efficiency, since being an extra number is definitely not prime.
p=+2
Print (RST)
#调用函数
Getprim (1000)
The result: 7927
The results of the operation are shown below:
By the way: In fact all languages are figured out, important is the algorithm, so master the basis of the classical algorithm is still very necessary drop ~ ~
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/