Guest User

Untitled

a guest
Nov 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int countPrimes(int n) {
  4. if (n == 0) {
  5. return 0;
  6. }
  7.  
  8. bool notPrime[n] = {false};
  9. int cnt = 0;
  10. const int limit = sqrt(n);
  11.  
  12. for(int i = 2; i < n; ++i) {
  13. if (!notPrime[i]) {
  14. ++cnt;
  15.  
  16. if(i <= limit) {
  17. for (int j = 2; j * i < n; ++j) {
  18. notPrime[j * i] = true;
  19. }
  20. }
  21. }
  22. }
  23.  
  24. return cnt;
  25. }
  26. };
Add Comment
Please, Sign In to add comment