fork(9) download
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("avx")
  3. #include <bits/stdc++.h>
  4. const int NMAX = (int)1e8;
  5. std::bitset<NMAX> bits;
  6. int main() {
  7. bits.set();
  8. bits[0] = bits[1] = 0;
  9. for (int i = 2; i * i <= NMAX; i++) {
  10. if (bits[i]) {
  11. for (int j = i * i; j <= NMAX; j += i) bits[j] = 0;
  12. }
  13. }
  14. //iterate over primes
  15. int64_t sum(0);
  16. int cnt(0);
  17. for (int p = bits._Find_first(); p < bits.size(); p = bits._Find_next(p)) {
  18. sum += p;
  19. cnt++;
  20. }
  21. assert(bits.count() == cnt);
  22. std::cout << "sum = " << sum << std::endl;
  23. std::cout << "cnt = " << cnt << std::endl;
  24. return 0;
  25. }
Success #stdin #stdout 0.72s 15752KB
stdin
Standard input is empty
stdout
sum = 279209790387276
cnt = 5761455