Skip to content

Commit 1c05d2a

Browse files
author
Jaskaran Singh
authored
Create count_primes.js
1 parent ea7b695 commit 1c05d2a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

count_primes.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// /**
2+
// * @param {number} n
3+
// * @return {number}
4+
// */
5+
6+
var countPrimes = function(n) {
7+
if (n == 0 || n == 1) return 0
8+
9+
let count = 0
10+
for (let i = 2; i < n; i++) if(isPrime(i)) count++
11+
return count
12+
};
13+
14+
const isPrime = n => {
15+
if (n == 2) return true
16+
for (let i = 2; i <= Math.sqrt(n); i++)
17+
if (n % i == 0) return false
18+
19+
return true
20+
}

0 commit comments

Comments
 (0)