-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentedSieve.java
60 lines (48 loc) · 1.68 KB
/
SegmentedSieve.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
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class SegmentedSieve {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Start point: ");
int start = sc.nextInt();
System.out.print("Enter End point: ");
int end = sc.nextInt();
if (end < start) {
System.out.println("Invalid input. End point should be greater than or equal to start point.");
sc.close();
return;
}
int range = end - start + 1;
int[] primesInRange = new int[range];
for (int i = 0; i < range; i++) {
primesInRange[i] = i + start;
}
int sqrtEnd = (int) Math.sqrt(end);
int[] smallPrimes = new int[sqrtEnd - 1];
for (int i = 0; i < sqrtEnd - 1; i++) {
smallPrimes[i] = i + 2;
}
for (int i = 0; i < smallPrimes.length; i++) {
if (smallPrimes[i] != 0) {
for (int j = smallPrimes[i] * smallPrimes[i]; j <= sqrtEnd; j += smallPrimes[i]) {
smallPrimes[j - 2] = 0;
}
}
}
System.out.print("\nPrimes: ");
for (int i = 0; i < smallPrimes.length; i++) {
if (smallPrimes[i] != 0) {
int firstMultiple = Math.max(smallPrimes[i] * smallPrimes[i], start);
for (int j = firstMultiple; j <= end; j += smallPrimes[i]) {
primesInRange[j - start] = 0;
}
}
}
for (int prime : primesInRange) {
if (prime > 0) {
System.out.print(prime + " ");
}
}
sc.close();
}
}