论坛里的算法暂时还并没有看到比我更简便的了(不要拿那些在if里面与10以内的素数比较再输出的比)
java
public class 素数 {
public static void main(String[] args) {
out: for (int i=2 ; i<=100 ; i++ ){
for (int j = 2 ; j<i ; j++){
if (i%j==0){
//但凡有一个值为false则直接退到外层循环,不再向下执行
continue out;
}
}
System.out.println(i);
}
}
}
python
while
i = 2
while i <= 100:
b = True
j = 2
while j < i:
if i % j == 0:
b = False
break
j += 1
if b:
print(i)
i += 1
for
for i in range(2, 101):
judge = True
for j in range(2, i):
if i % j == 0:
judge = False
break
if judge:
print(i)
结果:
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
进程已结束,退出代码为 0