You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0300-0399/0326.Power of Three/README_EN.md
+70-4Lines changed: 70 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,11 @@ tags:
62
62
63
63
<!-- solution:start -->
64
64
65
-
### Solution 1
65
+
### Solution 1: Trial Division
66
+
67
+
If $n \gt 2,ドル we can continuously divide $n$ by 3ドル$. If it's not divisible, it means $n$ is not a power of 3ドル,ドル otherwise we continue dividing by 3ドル$ until $n$ is less than or equal to 2ドル$. If $n$ equals 1ドル,ドル it means $n$ is a power of 3ドル,ドル otherwise it's not a power of 3ドル$.
68
+
69
+
Time complexity $O(\log_3n),ドル space complexity $O(1)$.
@@ -141,7 +167,13 @@ function isPowerOfThree(n: number): boolean {
141
167
* @return{boolean}
142
168
*/
143
169
varisPowerOfThree=function (n) {
144
-
return n >0&&1162261467% n ==0;
170
+
while (n >2) {
171
+
if (n %3!==0) {
172
+
returnfalse;
173
+
}
174
+
n =Math.floor(n /3);
175
+
}
176
+
return n ===1;
145
177
};
146
178
```
147
179
@@ -151,7 +183,11 @@ var isPowerOfThree = function (n) {
151
183
152
184
<!-- solution:start -->
153
185
154
-
### Solution 2
186
+
### Solution 2: Mathematics
187
+
188
+
If $n$ is a power of 3ドル,ドル then the maximum value of $n$ is 3ドル^{19} = 1162261467$. Therefore, we only need to check if $n$ is a divisor of 3ドル^{19}$.
0 commit comments