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
//it turns out they're using a trick to solve this question without using a loop: find the max possible integer that is a power of 3, then do modulor with this number
21
-
publicbooleanisPowerOfThree_without_loop(intn) {
22
-
return (n > 0 && 1162261467 % n == 0);
23
-
}
24
-
25
-
//I'm not able to think of a method that has no loop to do it, use regular method to solve it first
26
-
publicbooleanisPowerOfThree(intn) {
27
-
if (n < 3 && n != 1) {
28
-
returnfalse;
29
-
}
30
-
while (n != 1) {
31
-
if (n % 3 != 0) {
13
+
publicstaticclassSolution1 {
14
+
//regular method that has a loop
15
+
publicbooleanisPowerOfThree(intn) {
16
+
if (n < 3 && n != 1) {
32
17
returnfalse;
33
18
}
34
-
n /= 3;
19
+
while (n != 1) {
20
+
if (n % 3 != 0) {
21
+
returnfalse;
22
+
}
23
+
n /= 3;
24
+
}
25
+
returntrue;
35
26
}
36
-
returntrue;
37
27
}
38
28
39
-
publicstaticvoidmain(String... strings) {
40
-
_326test = new_326();
41
-
System.out.println(test.isPowerOfThree(12));
29
+
publicstaticclassSolution2 {
30
+
//find the max possible integer that is a power of 3, then do modulor with this number
0 commit comments