Problem statement

https://binarysearch.com/problems/Ugly-Number/

Solution

Equal to Leetcode 0263 Ugly Number.

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def solve(self, n):
        if n <= 0: return False
        for num in [2, 3, 5]:
            while n % num == 0: n//=num
        return n == 1