Skip to content

Commit cd9a33e

Browse files
committed
Count Primes
1 parent 7c08b64 commit cd9a33e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Count Primes.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*Count the number of prime numbers less than a non-negative number, n.*/
2+
3+
/**
4+
* @param {number} n
5+
* @return {number}
6+
*/
7+
var countPrimes = function(n) {
8+
count = 0
9+
isNotPrime = []
10+
for(i=2; i<Math.sqrt(n); i++){
11+
if(isNotPrime[i] == null){
12+
for(j=i; i*j<n; j++)
13+
isNotPrime[i*j] = true
14+
}
15+
}
16+
for(i=2; i<n; i++){
17+
if(isNotPrime[i] == null)
18+
count++
19+
}
20+
return count
21+
};

0 commit comments

Comments
 (0)