LeetCode Entry

Ugly Number

18.11.2022 easy 2022

fun isUgly(n: Int): Boolean {

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


    fun isUgly(n: Int): Boolean {
        if (n <= 0) return false
        var x = n
        while(x%2==0) x = x/2
        while(x%3==0) x = x/3
        while(x%5==0) x = x/5
        return x == 1
    }

There is also a clever math solution, but I don’t understand it yet.

Complexity: O(log(n)) Memory: O(1)