使用while循环和break:
from math import sqrt
j =2
whilej <=100:
i =2
k = sqrt(j)
while(i<=k):
ifj%i ==0:break
i = i+1
if(i > k):
printj,
j+=1
使用for循环和break:
frommathimportsqrt
foriinrange(2,101):
flag =True
k =int(sqrt(i))
forjinrange(2,k+1):
ifi % j ==0:
flag =0
break
if(flag):
printi,
定义函数:
frommathimportsqrt
defisprime(x):
ifx ==1:
returnFalse
k =int(sqrt(x))
forjinrange(2,k+1):
ifi % j ==0:
returnFalse
returnTrue
foriinrange(1,101):
ifisprime(i):
printi,
网友评论