马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#输出100到200之间的所有素数。 提示:不能被2到自身平方根范围内的任一整数整除的数为素数,math.sqrt(x)可用于求平方根,round()函数可用于取整。
import math
L1=[]
for i in range(100,201):
a=round(math.sqrt(i))
for n in range(2,a):
if i%n==0:
break
else:
L1.append(i)
print(L1)
输出结果:
[101, 101, 101, 101, 101, 101, 101, 101, 103, 103, 103, 103, 103, 103, 103, 103, 105, 107, 107, 107, 107, 107, 107, 107, 107, 109, 109, 109, 109, 109, 109, 109, 109, 111, 113, 113, 113, 113, 113, 113, 113, 113, 113, 115, 115, 115, 117, 119, 119, 119, 119, 119, 121, 121, 121, 121, 121, 121, 121, 121, 121, 123, 125, 125, 125, 127, 127, 127, 127, 127, 127, 127, 127, 127, 129, 131, 131, 131, 131, 131, 131, 131, 131, 131, 133, 133, 133, 133, 133, 135, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 141, 143, 143, 143, 143, 143, 143, 143, 143, 143, 145, 145, 145, 147, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 153, 155, 155, 155, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 159, 161, 161, 161, 161, 161, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 165, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 171, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 175, 175, 175, 177, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 183, 185, 185, 185, 187, 187, 187, 187, 187, 187, 187, 187, 187, 189, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 195, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199]
怎么让输出的每个数字只有一个啊
else 放错位置了吧
import math
L1 = []
for i in range(100, 201):
a = round(math.sqrt(i))
for n in range(2, a):
if i % n == 0:
break
else:
L1.append(i)
print(L1)
|