Java程序输出低于100的素数
任何大于1且只有两个因数和1本身的整数都称为质数。除了这两个数字外,它没有正除数。例如-
7 = 1 × 7
很少素数是− 1,2,3,5,7,11等。
算法
1.取整数A
2.将变量A除以(A-1至2)
3.如果A不能被任何值(A-1至2)整除,除了它本身就是质数。
4.对从2到所需限制的所有数字重复此操作。
示例
public class First100Primes {public static void main(String args[]){
for(int i = 2; i<=100;){
int prime = 1;
for(int loop = 2; loop <=i; loop++) {
if((i % loop) == 0 && loop!=i) {
prime = 0;
}
}
if (prime != 0){
System.out.println(i+" is a prime number");
i++;
}
else
i ++;
}
}
}
输出结果
2 is a prime number3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
53 is a prime number
59 is a prime number
61 is a prime number
67 is a prime number
71 is a prime number
73 is a prime number
79 is a prime number
83 is a prime number
89 is a prime number
97 is a prime number
以上是 Java程序输出低于100的素数 的全部内容, 来源链接: utcz.com/z/322175.html