Skip to content

Commit 0b0d20d

Browse files
authored
Adding PowerOfThree
1 parent 2281403 commit 0b0d20d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

PowerOfThree.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given an integer n, return true if it is a power of three. Otherwise, return false.
3+
An integer n is a power of three, if there exists an integer x such that n == 3x.
4+
5+
Example 1:
6+
Input: n = 27
7+
Output: true
8+
Explanation: 27 = 33
9+
*/
10+
public class PowerOfThree {
11+
public static void main(String[] args) {
12+
int n=27;
13+
boolean ans=isPowerOfThree(n);
14+
System.out.println(ans);
15+
}
16+
public static boolean isPowerOfThree(int n) {
17+
if(n==0){
18+
return false;
19+
}
20+
int num=n;
21+
boolean ans=true;
22+
while (num>0){
23+
if(num==1){
24+
return true;
25+
}
26+
if (num%3==0){
27+
num=num/3;
28+
}
29+
else {
30+
return false;
31+
}
32+
}
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)