- //第三种解法(筛选法)
- public class Ancheng {
- public static void main(String[] args) {
- boolean[] arr = new boolean[1000];
- arr[2] = true;
- for (int i = 2; i < arr.length; i++) {
- if (i % 2 == 1) {
- arr[i] = true;
- }
- }
- for (int i = 3; i < 1000 / 2; i += 2) {
- if (arr[i]) {
- for (int j = 2 * i; j < 1000; j += i) {
- arr[j] = false;
- }
- }
- }
- for (int i = 101; i < 1000; i++) {
- if (arr[i]) {
- System.out.println(i);
- }
- }
- }
- }
复制代码 |