Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <bitset>
  4.  
  5. int main()
  6. {
  7. const int n = 2000000;
  8. clock_t cstart = clock();
  9. std::bitset<n + 1> b;
  10. int count = 0;
  11. int i;
  12. for (i = 2; i <= n; ++i) {
  13. b.set(i);
  14. }
  15. i = 2;
  16. while (i * i <= n) {
  17. if (b.test(i)) {
  18. ++count;
  19. int k = 2 * i;
  20. while (k <= n) {
  21. b.reset(k);
  22. k += i;
  23. }
  24. }
  25. ++i;
  26. }
  27. while (i <= n) {
  28. if (b.test(i)) {
  29. ++count;
  30. }
  31. ++i;
  32. }
  33. clock_t cend = clock();
  34. double ms = 1000.0 * (cend - cstart) / CLOCKS_PER_SEC;
  35. std::cout << count << " total " << ms << " ms";
  36. system("pause");
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement