Math Fundamentals18 sections · 814 units
Open in Course

Ugly Number - Insight

(Divide out allowed primes)

To check if n is ugly, divide n by 2, 3, and 5 repeatedly (as long as they divide evenly). If n becomes 1, then n had no other prime factors. Return true.

If n does not reduce to 1, then n has a prime factor other than 2, 3, or 5. Return false.

Edge cases: n ≤ 0 is not ugly. Handle those first.