Skip to content

Commit 36f2f83

Browse files
committed
solutions
1 parent 91f8133 commit 36f2f83

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// description: https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/25/math/61/
2+
// 计数质数
3+
4+
function isPrime(n) {
5+
if (n <= 1) {
6+
return false;
7+
}
8+
for (var i = 2; i * i <= n; i++) {
9+
if (n % i === 0) return false;
10+
}
11+
return true;
12+
}
13+
14+
/**
15+
* @param {number} n
16+
* @return {number}
17+
*/
18+
var countPrimes = function(n) {
19+
var count = 0;
20+
for (var i = 1; i < n; i++) {
21+
if (isPrime(i)) {
22+
count = count + 1;
23+
}
24+
}
25+
return count;
26+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// description: https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/25/math/62/
2+
// 3的幂
3+
4+
/**
5+
* @param {number} n
6+
* @return {boolean}
7+
*/
8+
var isPowerOfThree = function(n) {
9+
if (n === 1) {
10+
return true;
11+
}
12+
if (n % 3 !== 0 || n === 0) {
13+
return false;
14+
}
15+
16+
return isPowerOfThree(n / 3);
17+
};

0 commit comments

Comments
 (0)