Ugly Number
Decide whether a number has no prime factors other than 2, 3 and 5.
Call a positive whole number ugly when the only primes that divide it are 2, 3 and 5. Given an integer ntruefalse
Example 1
Output: true
Explanation: 6 = 2 × 3.
Example 2
Output: false
Explanation: 14 = 2 × 7, and 7 is not allowed.
Example 3
Output: true
The Idea
Take the number 60 and keep splitting it into smaller pieces that multiply back to it. 60 is 2 × 30, and 30 is 2 × 15, and 15 is 3 × 5. So 60 = 2 × 2 × 3 × 5. None of 2, 3 and 5 can be split again, since only 1 and the number itself divide them. Such numbers are called primes, and 2, 2, 3 and 5 are the prime factors of 60.
All the prime factors of 60 are 2, 3 or 5, so 60 is ugly. Example 2 splits the same way into 14 = 2 × 7, and 7 is a prime outside the allowed three, so 14 is not ugly. Example 3, the number 1, has no prime factors at all, so nothing in it breaks the rule.
That raises the question: do we have to find every prime factor of n
So take those three out and see what remains. In the picture, each arrow divides by one allowed prime, and the coloured box at the end of a row is what remains.
The row labelled n = 60 divides by 2 twice, then by 3, then by 5, and ends in the teal box at 1. The row labelled n = 14 divides by 2 once and reaches 7. Neither 2, 3 nor 5 divides 7, so the row stops in the orange box at 7.
What remains is nn
Solution Approach
Three choices shape the code: how to take out the allowed primes instead of factoring n
Taking out 2, 3 and 5
Why not list all the prime factors of nnInteger.MAX_VALUEint
Instead, divide by 2 for as long as the remainder is 0, then do the same with 3, then with 5. The order does not change what remains: starting with 5, 60 becomes 12, the 2s turn 12 into 3, and the 3 turns that into 1, the same end as in the picture.
Reading what is left
Once no 2, 3 or 5 divides it any more, is the remaining number enough to decide? Yes. Every allowed prime has been taken out as often as it appeared, so the remaining number is exactly the product of the other primes. That product is 1 when there are none, and at least 7 when there is one.
Zero, negative numbers and overflow
What happens with 0? Its remainder after dividing by 2 is 0, and 0 / 2 is 0 again, so dividing out the 2s would never finish. Ugly numbers are positive anyway, so the method returns falsen
Can the arithmetic go past the range of an intnInteger.MAX_VALUElong
Java Solution
The method isUgly
- First, return
whenfalse is 0 or less, since ugly numbers are positive and 0 would never stop dividing.n - Put the allowed primes in a small array,
, and loop over it, withallowed = {2, 3, 5} taking each prime in turn.p - Inside, a
loop divideswhile byn as long asp , that is, as long asn % p == 0 dividesp with no remainder.n - After all three primes, return
: true when nothing but 2, 3 and 5 was inn == 1 .n
The mainInteger.MAX_VALUEInteger.MIN_VALUE
public class UglyNumber {
public static boolean isUgly(int n) {
if (n <= 0) {
return false; // ugly numbers are positive; also stops 0
}
int[] allowed = {2, 3, 5};
for (int p : allowed) {
while (n % p == 0) {
n /= p; // take out one factor p
}
}
return n == 1; // anything else has another prime factor
}
public static void main(String[] args) {
int[] tests = {6, 14, 1, 60, 49, 0, -6, 1 << 30, Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int n : tests) {
System.out.println("isUgly(" + n + ") = " + isUgly(n));
}
}
}
Running it prints:
isUgly(6) = true
isUgly(14) = false
isUgly(1) = true
isUgly(60) = true
isUgly(49) = false
isUgly(0) = false
isUgly(-6) = false
isUgly(1073741824) = true
isUgly(2147483647) = false
isUgly(-2147483648) = false
Look at isUgly(49) = falseisUgly(1073741824) = truep = 2
Next comes isUgly(2147483647) = falseInteger.MAX_VALUEfalseInteger.MIN_VALUE
That first check matters most for 0. A copy of the method without it never returned from isUgly(0)
Complexity
The cost is the number of divisions, plus at most one failed remainder check for each of the three primes.
- Time: O(log n), because every division at least halves
, so a positiven takes at most 30 divisions to reach 1, the count that 230 needs.int - Space: O(1), because the method changes
itself and keeps only the three-elementn array, whatever the size ofallowed .n
Listing the ugly numbers in increasing order is a different task, solved in