① 用python找質數
n=10000
numbers=list(range(2,n+1))
i=2
whilei<10001:
forminnumbers:
ifi<m:
whilem%i==0:#這邊你為什麼要用while呢,這邊如果m=3,i=2的話是會進入死循環的,改成if語句試試
k=m#這個沒必要存在,只接用numbers.remove(m)
numbers.remove(k)
i=i+1
print(numbers)
print(" Therewere",len(numbers),"primenumbersupto",n)
#下面是修改後的
n=10000
numbers=list(range(2,n+1))
i=2
whilei<10001:
forminnumbers:
ifi<mandm%i==0:
numbers.remove(m)
i=i+1
print(numbers)
print(" Therewere",len(numbers),"primenumbersupto",n)