無聊之作
求素數(shù)的方法有很多
這篇文章帶來一個很無聊的寫法
只使用while去求素數(shù)
直接上代碼:
import math
i = 2
while i < 101:
j = 2
flag_1 = True
flag_2 = 1
while j < int(math.sqrt(i)+1):
flag = False
flag_2 = 1
while i%j==0 and flag_2:
flag = True
flag_2 = 0
flag_2 = 1
while flag and flag_2:
flag_1 = False
flag_2 = 0
j += 1
while flag_1 and flag_2:
print(i,end=' ')
flag_2 = 0
i += 1
常規(guī)for求素數(shù)
i = 2
for i in range(2,101):
j = 2
for j in range(2,int(math.sqrt(i)+1)):
if (i%j==0):
break
else:
print(i,end=' ')
一起學(xué)習(xí)python稼钩,小白指導(dǎo)割粮,教學(xué)分享記得私信我