Java求100以内的素数
时间: 2023-09-03 16:11:55 浏览: 87
java代码-100里的素数
以下是求100以内的素数的Java代码:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int n = 100;
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
System.out.println("The prime numbers less than or equal to " + n + " are:");
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
System.out.print(i + " ");
}
}
}
}
```
运行结果:
```
The prime numbers less than or equal to 100 are:
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
```
阅读全文