天天看点

求素数(范围自改)

方法一

def prime(x):
    for i in range(2,x):
        if x%i==0:
            return False
        if i==x-1:
            return True
output = filter(prime, range(100, 200))
print(list(output))      

方法二

for a in range(101, 201):
    k = 0
    for i in range(2, a):
        if a % i == 0:
            k += 1
    if k == 0:
        print(a, end=' ')      

方法三

i = 100
while i <= 200:
    j = 2
    k = 0
    while j < i:
        if i % j == 0:
            k += 1
        j += 1
    if k == 0:
        print(i, end=' ')
    i += 1      

方法四

for i in range(100, 201):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        print("%d是素数" % i, end=' ')