We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ee82551 commit 3ba0195Copy full SHA for 3ba0195
326 Power of Three/solution.py
@@ -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
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
36
37
+ return True
38
39
40
41
42
+print(power(n))
0 commit comments