-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segmented_Sieve.java
52 lines (48 loc) · 1.46 KB
/
Segmented_Sieve.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
class Segmented_Sieve {
static int N = 1000000;
static boolean arr[] = new boolean[N + 1];
static void simpleSieve() {
Arrays.fill(arr, true);
arr[0] = arr[1] = false;
for (int i = 2; i * i <= N; i++) {
if (arr[i]) {
for (int j = i * i; j <= N; j += i) {
arr[j] = false;
}
}
}
}
static ArrayList<Integer> generatePrimes(int n) {
ArrayList<Integer> al = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (arr[i]) {
al.add(i);
}
}
return al;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int low = sc.nextInt();
int high = sc.nextInt();
simpleSieve();
ArrayList<Integer> primes = generatePrimes(high);
boolean[] dummy = new boolean[high - low + 1];
Arrays.fill(dummy, true);
for (int prime : primes) {
int firstMultiple = (low / prime) * prime;
if (firstMultiple < low) {
firstMultiple += prime;
}
for (int j = Math.max(firstMultiple, prime * prime); j <= high; j += prime) {
dummy[j - low] = false;
}
}
for (int i = low; i <= high; i++) {
if (dummy[i - low] && i != 1) {
System.out.print(i + " ");
}
}
}
}