# Simple C solution(4ms)

• ``````bool isUgly(int num) {
if(num == 1){
return true;
}
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;
}``````

• Could you pass?
My code is similar but Time Limit Exceeded

bool isUgly(int num) {
if(num <= 0) return false;

``````while((num % 2) == 0){
num >> 1;
}
while((num % 3) == 0){
num  /= 3;
}
while((num % 5) == 0){
num /= 5;
}

return (num == 1);
``````

}

• The answer would suggest that every non-prime number can be decomposed into the combination of 2 3 5. I was wondering if that is true? Many thanks!

• Nope,it's only the ugly numbers can be decomposed into the combination of 2 3 5. Other non-prime number can't be converted into 1 by this program :)

• me either. My code also comes across the same problem. And I am confused.

• @starwbx Me too, my code:

``````bool isUgly(int num)
{
while (num % 5 == 0) num /= 5;
while (num % 3 == 0) num /= 3;
while (num % 2 == 0) num /= 2;

return (num == 1);
}
``````

But this will pass:

``````bool isUgly(int num)
{
for (int i=2; i<6 && num; i++)
while (num % i == 0)
num /= i;
return num == 1;
}
``````

I don't understand why.

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