-
Notifications
You must be signed in to change notification settings - Fork 0
/
sieveoferathroetes.java
48 lines (43 loc) · 1.02 KB
/
sieveoferathroetes.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
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
static void sieve(int n,boolean primes[])
{
primes[0]=false;
primes[1]=false;;
for(int i=2;i<=n;i++)
{ if(primes[i])
{
for(int j=i*i;j<=n;j+=i)
{
primes[j]=false;
}
}
}
int count=0
for(int i=0;i<=n;i++)
{
if(primes[i])
{
System.out.print(i+" ");
}
}
System.out.println();
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
boolean primes[]=new boolean[n+1];
for(int i=0;i<=n;i++)
{
primes[i]=true;
}
sieve(n,primes);
}
}
}