Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit ded94ed

Browse files
committed
342. Power of Four
1 parent 08e5a47 commit ded94ed

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

342. Power of Four.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 342. Power of Four
2+
3+
### 2017-03-30
4+
5+
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
6+
7+
**Example:**
8+
Given num = 16, return true. Given num = 5, return false.
9+
10+
**Follow up**: Could you solve it without loops/recursion?
11+
12+
13+
14+
# Solution
15+
16+
```swift
17+
class Solution {
18+
func isPowerOfX(x: Int, num: Int) -> Bool {
19+
guard num > 0, x > 0 else { return false }
20+
let n = log10(Double(num)) / log10(Double(x))
21+
return (ceil(n) - n).isZero
22+
}
23+
func isPowerOfFour(_ num: Int) -> Bool {
24+
return isPowerOfX(x: 4, num: num)
25+
}
26+
}
27+
```
28+

0 commit comments

Comments
 (0)