Skip to content

263. Ugly Number

https://leetcode.com/problems/ugly-number/

js
/**
 * @param {number} num
 * @return {boolean}
 */
var isUgly = function(num) {
	if (num <= 0) {
		return false;
	}
	for (var i of [2, 3, 5]) {
		while (num % i === 0) {
			num = num / i;
		}
	}

	return num === 1;
};
py
class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 0:
            return False

        for i in [2, 3, 5]:
            while num % i == 0:
                num = num / i

        return num == 1