Skip to content

Commit 3ba0195

Browse files
few ready
1 parent ee82551 commit 3ba0195

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

326 Power of Three/solution.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Given an integer n, return true if it is a power of three. Otherwise, return false.
2+
#
3+
# An integer n is a power of three, if there exists an integer x such that n == 3x.
4+
#
5+
#
6+
#
7+
# Example 1:
8+
#
9+
# Input: n = 27
10+
# Output: true
11+
# Explanation: 27 = 33
12+
# Example 2:
13+
#
14+
# Input: n = 0
15+
# Output: false
16+
# Explanation: There is no x where 3x = 0.
17+
# Example 3:
18+
#
19+
# Input: n = -1
20+
# Output: false
21+
# Explanation: There is no x where 3x = (-1).
22+
23+
24+
n = 27
25+
26+
def power(n) -> bool:
27+
28+
if n <= 0:
29+
return False
30+
31+
while n != 1:
32+
if n % 3 == 0:
33+
n = n // 3
34+
elif n % 3 != 0:
35+
return False
36+
37+
return True
38+
39+
40+
41+
42+
print(power(n))

0 commit comments

Comments
 (0)