我試過使用該代碼這個問題的工作:找到第1000黃金(蟒蛇)
def nthprime(n):
cnt=1
count=0
while(cnt==n):
for i in range(3,):
for j in range(2, i):
if i % j != 0:
count = count + 1
if count == (i - 2):
cnt = cnt + 1
return I
print(nthprime(1000))
誰能告訴我什麼是錯我的代碼?它只會一直返回「無」。
什麼是'I'在'回報我'?它已經初始化了嗎? – arif
你正在通過我假設「xth」素數。但是,除非n = 1,否則while循環將始終失敗。 – gutelfuldead
由於函數永遠不會進入最外層循環('while'),它將返回'None'。根據傳遞給函數的參數,進入該循環的條件是'cnt == n',但'cnt'被初始化爲'0','n'爲'1000'。所以條件永遠不會滿足,函數返回'None'。更好的算法是[Eratosthenes的篩選](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) – arif