Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int countPrimes(int n) {
- if (n == 0) {
- return 0;
- }
- boolean[] notPrime = new boolean[n];
- int cnt = 0;
- final int limit = (int)Math.sqrt(n);
- for(int i = 2; i < n; ++i) {
- if(!notPrime[i]) {
- ++cnt;
- if (i <= limit) {
- for(int j = 2; j * i < n; ++j) {
- notPrime[i*j] = true;
- }
- }
- }
- }
- return cnt;
- }
- }
Add Comment
Please, Sign In to add comment