File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments