Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0263 - Ugly Number
    0263 - Ugly Number
    0263 - Ugly Number
    0263 - Ugly Number

    0263 - Ugly Number

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/ugly-number/

    Solution 1 - Iterative Division

    An ugly number is a positive integer whose only prime factors are 2, 3, and 5. Repeatedly divide n by 2, 3, and 5 as long as it is divisible. If the result is 1, the number is ugly. Handle edge case where n <= 0 by returning False.

    TimeComplexity:O(log⁡n)TimeComplexity: O(\log n)TimeComplexity:O(logn)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Recursive Division

    Same logic as Solution 1 but implemented recursively. If n is divisible by 2, 3, or 5, recursively call with the divided value. Base cases: return False if n <= 0, return True if n == 1.

    TimeComplexity:O(log⁡n)TimeComplexity: O(\log n)TimeComplexity:O(logn)
    SpaceComplexity:O(log⁡n)SpaceComplexity: O(\log n)SpaceComplexity:O(logn)
    """ 0263.1 - Ugly Number - Solution 1 - Iterative Division """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isUgly(self, n: int) -> bool:
            """Ugly Number Function"""
            if n <= 0:
                return False
    
            for p in [2, 3, 5]:
                while n % p == 0:
                    n //= p
    
            return n == 1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isUgly(6))    # True
        print(Solution().isUgly(1))    # True
        print(Solution().isUgly(14))   # False
        print(Solution().isUgly(0))    # False
        print(Solution().isUgly(-6))   # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0263.2 - Ugly Number - Solution 2 - Recursive Division """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isUgly(self, n: int) -> bool:
            """Ugly Number Function"""
            if n <= 0:
                return False
            if n == 1:
                return True
    
            for p in [2, 3, 5]:
                if n % p == 0:
                    return self.isUgly(n // p)
    
            return False
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isUgly(6))    # True
        print(Solution().isUgly(1))    # True
        print(Solution().isUgly(14))   # False
        print(Solution().isUgly(0))    # False
        print(Solution().isUgly(-6))   # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()