Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Ugly Number

C++, Python and TypeScript solutions for Ugly Number.

The Ugly Number solutions are below.

Solution files

Python•ugly-number/synced-solution.py
def isUgly(n: int) -> bool:
    if n <= 0:
        return False
    for factor in [2, 3, 5]:
        while n % factor == 0:
            n //= factor
    return n == 1
C++•ugly-number/synced-solution.cpp
class Solution {
public:
    bool isUgly(int n) {
        if (n <= 0) return false;
        for (int factor : {2, 3, 5}) {
            while (n % factor == 0) {
                n /= factor;
            }
        }
        return n == 1;
    }
};
TypeScript•ugly-number/synced-solution.ts
function isUgly(n: number): boolean {
    if (n <= 0) return false;
    for (const factor of [2, 3, 5]) {
        while (n % factor === 0) {
            n /= factor;
        }
    }
    return n === 1;
}