Skip to content

Instantly share code, notes, and snippets.

@Bojne
Last active June 11, 2016 12:58
Show Gist options
  • Save Bojne/961a12a315d41a27f17b659bcad8bf71 to your computer and use it in GitHub Desktop.
Save Bojne/961a12a315d41a27f17b659bcad8bf71 to your computer and use it in GitHub Desktop.
#找到兩百萬以下所有質數的和
def prime_num_list(max_num):
x = 2 #最小的質數
prime = [2]
checkNum = None #用於檢測是不是質數,如果是 = True
while x < max_num:
checkNum = True
#檢查每個質數表裡的數,如果都不是表裡的倍數,x就是質數
for prime_num in prime:
if x % prime_num == 0: #發現x可被某數整除,不是質數
checkNum = False
break
if prime_num **2 > x: #如果x的平方根以下都沒有因數,x就是質數
break
if checkNum: #如果沒有找到因數
prime.append(x) #就把x加入質數表
x +=1
return prime
print (prime_num_list(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment