Ugly Number (LeetCode 263): Solution and Explanation
Decide whether a given integer is positive and has no prime factors other than 2, 3 and 5. Divide those three primes out as many times as they go in, and the number is ugly exactly when 1 is left.
Intuition
Every positive integer splits into prime factors in exactly one way. The question is whether that split uses only 2, 3 and 5. You do not need to find the whole factorization to answer it; you only need to know whether anything else is in there.
Dividing n by 2 when 2 divides it removes one allowed factor and keeps every other factor in place. Repeat that for 2, then 3, then 5, and the number left over is the product of all the other prime factors. If that leftover is 1, there were none.
Zero and negative numbers need a separate check. Zero is divisible by everything, so the loop would spin forever on it, and negatives are excluded by definition.
Approach: Divide out the allowed primes
- If
n <= 0, return false. - For each prime p in 2, 3, 5, divide n by p while
n % p == 0. - Return whether n is now 1.
Step-by-step example
n = 6
- n = 6 is positive, so continue.
- 6 % 2 = 0, divide: n = 3. 3 % 2 = 1, so no more factors of 2.
- 3 % 3 = 0, divide: n = 1. 1 % 3 = 1, stop.
- 1 % 5 = 1, nothing to remove.
- n is 1, so 6 = 2 x 3 used only allowed primes. Return true.
Code: Python, Java, C++
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 == 11class Solution {
2 public boolean isUgly(int n) {
3 if (n <= 0) return false;
4 for (int p : new int[]{2, 3, 5}) {
5 while (n % p == 0) {
6 n /= p;
7 }
8 }
9 return n == 1;
10 }
11}1class Solution {
2public:
3 bool isUgly(int n) {
4 if (n <= 0) return false;
5 for (int p : {2, 3, 5}) {
6 while (n % p == 0) {
7 n /= p;
8 }
9 }
10 return n == 1;
11 }
12};Time and space complexity
O(log n)
Each successful division shrinks n by at least half, so there are at most about 31 divisions for a 32-bit input, plus three failed tests.
O(1)
Only the shrinking value of n is stored.
Other approaches
Trial division over all primes (time O(sqrt n), space O(1))
Factor n completely by trying every divisor up to its square root and check that only 2, 3 and 5 appear. It answers the question but does far more work than needed.
Recursive division (time O(log n), space O(log n))
Return true for 1, false for n <= 0, and otherwise recurse on n / p for the first p in 2, 3, 5 that divides n, returning false if none does. Same idea as the loop, written as recursion with O(log n) stack depth.
Common mistakes
- Forgetting the
n <= 0check. With n = 0 the loopwhile n % 2 == 0never ends. - Treating 1 as not ugly. 1 has no prime factors, so it qualifies, and the problem says so.
- Dividing by each prime only once instead of in a loop. 8 needs three divisions by 2.
Frequently asked questions
Why is 1 an ugly number?
It has no prime factors, so it has no factor outside 2, 3 and 5. The problem statement counts it as ugly.
Does the order 2, 3, 5 matter?
No. Division by one prime never changes how many times another prime divides n, so any order leaves the same remainder.
Is there a faster test?
Not meaningfully. The loop is already O(log n) with tiny constants, and it is at most about 34 modulo operations for any 32-bit input.