37,720
社区成员
发帖
与我相关
我的任务
分享
#100以内质数的while循环
i = 2
while i < 101:
j = 2
while j < i:
if i % j == 0:
break
else:
if j == i-1:
print(i)
j += 1
i += 1
h = 0
leap = 1
from math import sqrt
for m in range(1,100):
k = int(sqrt(m + 1))
for i in range(2,k + 1):
if m % i == 0:
leap = 0
break
if leap == 1:
print ('%-4d' % m, end = '')
h += 1
if h % 10 == 0:
print ('')
leap = 1
print('')
print ('The total is %d' % h)
result = [ x for x in range(2,101) if 0 not in [ x%y for y in range(2,x)]]
print (result)
def printPrime(maxNumber):
"""
打印小于maxNumber的质数
"""
for n in primes():
if n < maxNumber:
print(n,end=",")
else:
break
def _odd_iter():
n=1
while True:
n=n+2
yield n
def _not_divisible(n):
return lambda x:x % n>0
def primes():
#素数不包括1,从2开始
yield 2
it=_odd_iter()
while True:
n=next(it)
yield n
it=filter(_not_divisible(n),it)
# encoding=utf-8
for x in range(2,101): # 依次拿出2-100的每个数字
y = 1
while y <= x: # 从1除到自己如果发现了除1和自己以外的除数立刻退出循环,如果循环正常结束则执行else语句
if x%y == 0 and y != 1 and y != x:
break
y += 1
else: # 循环没有break执行else
print(x)
x += 1
while True:
print('2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97')
break
from math import sqrt
n = 0
prime_list = [] # 质数列表
while n < 100:
n += 1
flag = True
if n == 1:
continue
else:
# 对正整数n,如果用2到根号n之间的所有整数去除,均无法整除,则n为质数
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
flag = False
break
if flag:
prime_list.append(n)
print(prime_list)
for i in range(2,101):
ii=1
while ii<i:
if i%ii==0 and ii!=1 and ii!=i:
break
ii+=1
if ii==i:
print ("prime number:{}".format(i))
zhishu = []
n = 100
x = 2
while x in range(2,n):
y = 0
result = True
while y < len(zhishu):
s = x % zhishu[y]
if s == 0:
result = False
break
y += 1
if result:
zhishu.append(x)
x += 1
print(zhishu)
i = 1
while True:
if i > 100: break
x = 0 # 记录被整除的次数
for j in range(1, i):
if i % j == 0:
x += 1
if x <= 1: #i=1的话range(1, i)是空的,所以x=0, i大于1时,因为range(1, i)不包含i,只能被1整除,所以x=1。
print(i)
i += 1