LeetCode #263 Easy

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.
  • n can be any 32-bit integer, including 0 and negatives.
Constraints
  • -2³¹ <= n <= 2³¹ - 1
math
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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).

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def isUgly(self, n: int) -> bool:
▶3 if n <= 0:
▶4 return False
▶5 for p in (2, 3, 5):
▶6 while n % p == 0:
▶7 n //= p
▶8 return n == 1
primes to remove235number left60this check÷=factors removedstart with 60
n60
The plan: strip every factor of 2, then of 3, then of 5, and see what is left. Every positive whole number is a product of primes in one way only, so whatever survives is made of primes other than 2, 3 and 5.
primes to remove235number left30this check60÷2=30factors removed260 splits by 2 → 30 left
n30
prime2
60 ÷ 2 = 30 with nothing left over, so one copy of 2 is removed. The loop tries 2 again, because a number can hold the same prime more than once.
primes to remove235number left15this check30÷2=15factors removed2×230 splits by 2 → 15 left
n15
prime2
30 ÷ 2 = 15 with nothing left over, so one copy of 2 is removed. The loop tries 2 again, because a number can hold the same prime more than once.
primes to remove235number left15this check15÷2→remainder 1factors removed2×215 does not split by 2 → try 3
n15
prime2
15 ÷ 2 leaves remainder 1, so no copy of 2 is left in it. Move on to the next prime.
primes to remove2done35number left5this check15÷3=5factors removed2×2×315 splits by 3 → 5 left
n5
prime3
15 ÷ 3 = 5 with nothing left over, so one copy of 3 is removed. The loop tries 3 again, because a number can hold the same prime more than once.
primes to remove2done35number left5this check5÷3→remainder 2factors removed2×2×35 does not split by 3 → try 5
n5
prime3
5 ÷ 3 leaves remainder 2, so no copy of 3 is left in it. Move on to the next prime.
primes to remove2done3done5number left1this check5÷5=1factors removed2×2×3×55 splits by 5 → 1 left
n1
prime5
5 ÷ 5 = 1 with nothing left over, so one copy of 5 is removed. The loop tries 5 again, because a number can hold the same prime more than once.
primes to remove2done3done5number left1this check1÷5→remainder 1factors removed2×2×3×51 does not split by 5 → stop
n1
prime5
1 ÷ 5 leaves remainder 1, so no copy of 5 is left in it. All three allowed primes are now gone.
primes to remove2done3done5donenumber left1this check÷=factors removed2×2×3×51 is left → ugly
answertrue
Only 1 is left, so 60 is made of nothing but 2 × 2 × 3 × 5. Every prime factor was 2, 3 or 5, so 60 is ugly.
05

Common pitfalls

Forgetting the n <= 0 check

✗ Wrong
for p in (2, 3, 5):
    while n % p == 0:
        n //= p
✓ Right
if n <= 0:
    return False
for p in (2, 3, 5):
    while n % p == 0:
        n //= p

With 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

✗ Wrong
for p in (2, 3, 5):
    if n % p == 0:
        n //= p
✓ Right
for p in (2, 3, 5):
    while n % p == 0:
        n //= p

An 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.

06

Edge cases

n = 1

No 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.

A prime above 5, such as n = 7

None of the three divisions apply, 7 is left, and the answer is false.

07

Complexity

Time
O(log n)
Space
O(1)
Every division at least halves 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.
08

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.