-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathPrimeSieve.java
More file actions
33 lines (28 loc) · 760 Bytes
/
PrimeSieve.java
File metadata and controls
33 lines (28 loc) · 760 Bytes
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
import java.util.*;
public class PrimeSieve {
public static void main(String[] args)
{
// Scanner sc=new Scanner(System.in);
int n=40;
boolean[] primes=new boolean[n+1];
sieve(n,primes);
}
// false means number is prime
static void sieve(int n,boolean[] primes)
{
for (int i=2;(i*i)<n+1;i++)
{
if(primes[i]==false)
{
for (int j = (2*i); j < n+1; j=j+i) {
// make all the multiples true i.e. they are composite
primes[j]=true;
}
}
}
for (int i = 2; i < n+1; i++) {
if(primes[i]==false)
System.out.print(i+" ");
}
}
}