fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define ISBITSET(x, i) (( x[i>>3] & (1<<(i&7)) ) != 0)
  5. #define SETBIT(x, i) x[i>>3] |= (1<<(i&7));
  6. #define CLEARBIT(x, i) x[i>>3] &= (1<<(i&7)) ^ 0xFF;
  7.  
  8. long long sumPrimes(int n) {
  9. int m = (n-1) / 2;
  10. char b[m/8+1];
  11. int i = 0;
  12. int p = 3;
  13. long long s = 2;
  14. int j;
  15.  
  16. memset(b, 255, sizeof(b));
  17.  
  18. while (p*p < n) {
  19. if (ISBITSET(b,i)) {
  20. s += p;
  21. j = (p*p - 3) / 2;
  22. while (j < m) {
  23. CLEARBIT(b, j);
  24. j += p; } }
  25. i += 1; p += 2; }
  26.  
  27. while (i < m) {
  28. if (ISBITSET(b,i)) {
  29. s += p; }
  30. i += 1; p += 2; }
  31.  
  32. return s; }
  33.  
  34. int main(void) {
  35. printf("%lld\n", sumPrimes(2000000));
  36. return 0; }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
142913828922