c++
class Solution {
public:
bool isUgly(int num) {
for (int i=2; i<=6 && num; i++)
while (num % i == 0)
num /= i;
return num == 1;
}
};
python
class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num<1:
return False;
for x in 8,6,5,4,3,2:
while num%x==0:
num /= x
return num==1
reference:
https://leetcode.com/discuss/52703/2-4-lines-every-language