质数问题

生成n个质数的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
def (n):
p = 2
x = 0
while(x < n):
result = True
for i in range(2, p // 2 + 1):
if(p % i == 0):
result = False # 如果P能被任意一个小于n的数整除,则非质数
if result == True:
print(p) # 如果是质数,则打印
x = x + 1 # 计数+1
p += 1 # P+1
getprim(20)