# My Ugly Solution of Ugly Number

• This is a slow solution but different from what I have seen.
The if statements are really unnecessary, however it could save some time.
It also can be recursive.
again, it is SLOW but another solution.

``````public class Solution {
public boolean isUgly(int num) {
if(num<1)
return false;
String s="";
if(num>1&&num%2==0) {
s=Integer.toBinaryString(num).replaceAll("0+\$", "");
num=Integer.valueOf(s, 2);
// 		return isUgly(num);
}
if(num>1&&num%3==0) {
s=Integer.toString(num, 3).replaceAll("0+\$", "");
num=Integer.valueOf(s, 3);
// 		return isUgly(num);
}
if(num>1&&num%5==0) {
s=Integer.toString(num, 5).replaceAll("0+\$", "");
num=Integer.valueOf(s, 5);
// 		return isUgly(num);
}
return num==1;
}
}``````

• I think it is ugly too, ^_^, here is my code, not so fast but comprehensible:

``````public boolean isUgly (int num) {
if (num == 0) {
return false;
}
while (num % 2 == 0) {
num /= 2;
}
while (num % 3 == 0) {
num /= 3;
}
while (num % 5 == 0) {
num /= 5;
}
return num == 1;
}``````

• yours are good enough, my code is actually following the though of bit manipulation.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.