Guest User

Untitled

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