-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sieve of Eratosthenes
48 lines (45 loc) · 1 KB
/
Sieve of Eratosthenes
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
//https://practice.geeksforgeeks.org/problems/sieve-of-eratosthenes/0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll const N = 1e4 + 1 ;
ll arr [ N ] ;
void cal ( )
{
for ( ll i = 1 ; i <= N ; i ++ )
arr [ i ] = 1 ;
for ( int i = 2 ; i * i <= N ; i ++ )
{
if ( arr [ i ] )
{
// cout << i << " " ;
for ( ll j = i * i ; j <= N ; j += i )
{
arr [ j ] = 0 ;
}
}
}
}
int main() {
//ios_base::sync_with_stdc; cin.tie(0);cout.tie (0) ;
ll T ;
cin >> T ;
// cout << T << " ***" << endl ;
cal () ;
//cout << T << endl ;
while ( T-- )
{
ll n ;
cin >> n ;
for ( int i = 2 ; i <= n ; i ++ )
{
if ( arr [ i ] )
{
cout << i << " " ;
}
}
cout << endl ;
}
//code
return 0;
}