forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sieve_of_Eratosthenes.java
60 lines (40 loc) · 1.04 KB
/
Sieve_of_Eratosthenes.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
/**
The Sieve of Eratosthenes is one of the most
efficient ways to find all primes smaller than n.
*/
import java.util.Arrays;
import java.util.Scanner;
public class Sieve_of_Eratosthenes {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print ("Enter value of n - ");
int n=sc.nextInt();
boolean[] prime=new boolean[n+1];
for (int i=2; i<=n; i++) {
prime[i]=true;
}
for (int i=2; i<=Math.sqrt(n); i++) {
int x = i;
if (prime[i]) {
for (int j=2; j*x<=n; j++) {
prime[j*x]=false;
}
}
}
System.out.print ("Prime numbers upto "+n+" - ");
for (int i=2; i<=n; i++) {
if (prime[i]) {
System.out.print (i+", ");
}
}
System.out.println ("End.");
}
}
/**
Time Complexity : O(N(log(log(N))))
Space Complexity : O(N)
Sample Test case 1 :
Enter value of n - 13
Output :
Prime numbers upto 13 - 2, 3, 5, 7, 11, 13, End.
*/