j ava求200内的素数
时间: 2024-05-14 11:18:07 浏览: 96
java求100之内的素数(质数)简单示例
以下是Java代码实现:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int n = 200;
System.out.println("Prime numbers up to " + n + " are:");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
// Check if a number is prime or not
public static boolean isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
```
输出结果:
```
Prime numbers up to 200 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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
```
阅读全文