fork(5) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define N 51000000
  5. unsigned int prime[N / 64];
  6. #define gP(n) (prime[n>>6]&(1<<((n>>1)&31)))
  7. #define rP(n) (prime[n>>6]&=~(1<<((n>>1)&31)))
  8. void sieve()
  9. {
  10. memset( prime, -1, sizeof( prime ) );
  11.  
  12. unsigned int i;
  13. unsigned int sqrtN = ( unsigned int )sqrt( ( double )N ) + 1;
  14. for( i = 3; i < sqrtN; i += 2 ) if( gP( i ) )
  15. {
  16. unsigned int i2 = i + i;
  17. for( unsigned int j = i * i; j < N; j += i2 ) rP( j );
  18. }
  19. }
  20.  
  21. bool checkPrime (int x) {return (x&1)&&gP(x); }
  22.  
  23. int main () {
  24. ios_base::sync_with_stdio(false);
  25. cin.tie(0);
  26. sieve();
  27. for (int i=1; i<30; ++i)
  28. cout <<i<<' '<<checkPrime(i)<<endl;
  29. return 0;
  30. }
Success #stdin #stdout 0.27s 6344KB
stdin
Standard input is empty
stdout
1 1
2 0
3 1
4 0
5 1
6 0
7 1
8 0
9 0
10 0
11 1
12 0
13 1
14 0
15 0
16 0
17 1
18 0
19 1
20 0
21 0
22 0
23 1
24 0
25 0
26 0
27 0
28 0
29 1