Ugly Number
Ugly Number is LeetCode 263 (Easy). Given an integer n, return true if n is an ugly number and false otherwise.
- An ugly number is a positive integer whose only prime factors are 2, 3 and 5.
- 1 counts as ugly: it has no prime factors at all, so none of them is outside the allowed three.
ncan be any 32-bit integer, including 0 and negatives.
- -2³¹ <= n <= 2³¹ - 1
Intuition
Every positive integer splits into primes in exactly one way: 60 = 2 × 2 × 3 × 5, and 14 = 2 × 7. So n is ugly exactly when that split uses no prime other than 2, 3 and 5.
You don't need the full split to check that. Divide n by 2 as long as it divides evenly, then by 3, then by 5. That removes every allowed factor. If the number left is 1, nothing else was ever in it. If something bigger than 1 is left, like the 7 in 14, it is built from a forbidden prime.
"Is this number built only from these primes?" is answered by dividing those primes out until you can't. Power of Two and Power of Three are the same loop with one prime instead of three.
Approach
Before reading on, take 60 and 14 by hand. Divide by 2 while you can, then by 3, then by 5. What is left over in each case, and what does the leftover tell you?
Reject zero and negatives first
If n <= 0, return false straight away. Ugly numbers are positive by definition, and for n = 0 the division loop would never stop, because 0 divided by 2 is still 0 with no remainder.
Divide out each allowed prime
For each p in 2, 3, 5, keep doing n //= p while n % p == 0. Each pass of the inner loop removes one copy of p, so after the three loops no factor of 2, 3 or 5 remains in n.
Check what is left
Return n == 1. Whatever remains has no factor of 2, 3 or 5, so it is either 1 (the number was ugly) or a product of larger primes such as 7, 11 or 13 (it was not).
Code in Python, C++ and Java
Common pitfalls
Forgetting the n <= 0 check
for p in (2, 3, 5):
while n % p == 0:
n //= pif n <= 0:
return False
for p in (2, 3, 5):
while n % p == 0:
n //= pWith n = 0, 0 % 2 is 0 and 0 // 2 is 0 again, so the loop spins forever. Negatives are a second trap: -8 divides down to -1, which is not 1, so it happens to return false, but only by luck.
Dividing only once per prime
for p in (2, 3, 5):
if n % p == 0:
n //= pfor p in (2, 3, 5):
while n % p == 0:
n //= pAn if removes a single copy of each prime. For n = 8 it leaves 4, which is not 1, so 8 is wrongly called not ugly even though 8 = 2 × 2 × 2.
Edge cases
n = 1No loop runs, because 1 is not divisible by 2, 3 or 5, and 1 == 1 returns true. That matches the rule that 1 is ugly.
n = 7None of the three divisions apply, 7 is left, and the answer is false.
Complexity
n, so a 32-bit input takes at most about 31 divisions in total, plus a few failed checks when moving from one prime to the next.Ugly Number FAQ
Why is 1 an ugly number?
The rule only forbids prime factors other than 2, 3 and 5. The number 1 has no prime factors, so it breaks no rule. It is also the first term of the ugly number sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 …
Does the order 2, 3, 5 matter?
No. Each prime's copies are removed independently, so dividing by 5 first and 2 last leaves the same number. Starting with 2 just shrinks n fastest in the common case.
How do I find the nth one instead?
Testing 1, 2, 3, … one by one with this check is far too slow, because ugly numbers get rare. Ugly Number II builds the sequence directly, in order, from earlier terms times 2, 3 and 5.