Ugly numbers are positive numbers whose prime factors only include 2, 3, 5
. For example, 6, 8
are ugly while 14
is not ugly since it includes another prime factor 7
class Solution {
public:
bool isUgly(int num) {
//比较直接的方法就是对一个数重复除以2,3,5直到这个数不存在2,3,5这三个因子为止,若此时得到的结果不为1,则说明该数不是丑数。
if(num<=0) return false;
while(num%2==0)num=num/2;
while(num%3==0)num=num/3;
while(num%5==0)num=num/5;
if (num==1) return true;
else return false;
}
};