-2
我想寫一個不斷生成和素數的程序。所以你會開始它,然後素數被保存到一個文本文件或作爲一個字符串。到目前爲止,我已經得到了這個代碼,但不知道如何使它繼續工作。下一個素數生成
def primenumbers():
j = 2
chk = 1
f = open("primes.txt", "w")
primes = []
notprimes = []
ask = input("how many primes? ")
while len(primes) < int(ask):
k = 2
while not(k==j) and not(j%k==0):
k = k + 1
if k == j:
primes.append(j)
f.write(str(j)+"\n")
else:
notprimes.append(j)
if len(primes) >= 1000*chk:
chk = chk + 1
print("There have been " + str(len(primes)) + " primes counted so far")
j = j + 1
print("Primes written to file 'primes.txt', " + str(len(primes)) + " written")
f.close
return(" ")