生成n個質(zhì)數(shù)的程序:
def getprim(n):
p = 2 #質(zhì)數(shù)從二開始
x = 0
while(x < n):
result = True
for i in range(2, p // 2 + 1):
if(p % i == 0):
result = False # 如果P能被任意一個小于n的數(shù)整除拉岁,則非質(zhì)數(shù)
if result == True:
print(p) # 如果是質(zhì)數(shù)资锰,則打印
x = x + 1 # 計數(shù)+1
p += 1 # P+1
getprim(20)