Ugly Number Calculator
Number Theory
- Ugly numbers (also called Hamming numbers) are the positive integers whose only prime factors are 2, 3 and 5. Every term of the ugly sequence is built from 2, 3 and 5 alone, and this page finds the term at the index you give.
- Each new ugly term is the smallest unused multiple of an earlier term by 2, 3 and 5. Generating the ugly numbers in order up to index 10 gives the term 12, between the terms 10 and 15.
- The term factorises as 12 = 2 x 2 x 3, which uses only the primes 2, 3 and 5 - confirming it is an ugly term. The first 10 ugly terms add to 60.
- So the ugly number at index 10 is 12: an ugly term built only from 2, 3 and 5, with the neighbouring terms 10 and 15.
Ugly numbers - also called Hamming numbers, and known to the Babylonians as regular numbers - are the positive integers whose prime factorisation uses only 2, 3 and 5. The sequence begins 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24: every integer that avoids a prime factor of 7 or larger. Seven itself is the first positive integer that is not ugly, followed by 11, 13 and 14 (which smuggles in a 7).
The name is a joke that stuck - there is nothing unattractive about the numbers, and the rival name Hamming numbers honours Richard Hamming, who posed their efficient generation as a programming problem. Edsger Dijkstra took the problem up in his writings, and the classic challenge is famous: produce the 1500th ugly number, which is 859963392, without testing every integer one by one.
Enter an index n and the calculator generates the sequence in order by the three-pointer method, reads off the nth term, and shows the neighbouring terms, the running total and the term's own factorisation over 2, 3 and 5 - so every result carries its proof of membership with it.
Worked examples
The tenth ugly number
(10)
| Ugly number U(n) | 12 |
| Previous term | 10 |
| Next term | 15 |
| Sum of the first n terms | 60 |
The fifteenth ugly number
(15)
| Ugly number U(n) | 24 |
| Previous term | 20 |
| Next term | 25 |
| Sum of the first n terms | 153 |
The twentieth ugly number
(20)
| Ugly number U(n) | 36 |
| Previous term | 32 |
| Next term | 40 |
| Sum of the first n terms | 303 |
What makes a number ugly
The rule is absolute: factorise the number completely, and if any prime other than 2, 3 or 5 appears, the number is out. 60 = 2 x 2 x 3 x 5 is ugly; 84 = 2 x 2 x 3 x 7 is not, despite sharing most of its factorisation. Powers are welcome in any amount - 2^10 = 1024 is ugly, as is 3^5 = 243 and 5^4 = 625 - because the rule restricts which primes, not how many.
One is included by convention, as the empty product: it has no prime factors at all, so it vacuously satisfies the rule, and it seeds the generation. Every ugly number is therefore 2^a x 3^b x 5^c for some exponents a, b, c of zero or more - a three-parameter family pretending to be a one-parameter sequence.
The calculator's index runs to 1500, the classic Hamming bound, so the famous final value of the traditional problem - the 1500th term, 859963392 - is itself one click away, generated in a blink by the pointer method rather than by sifting the hundreds of millions of integers below it.
Generating the sequence in order
The three-pointer method is the elegant heart of the topic. Keep three fingers on the sequence: one on the smallest term not yet doubled, one on the smallest not yet tripled, one on the smallest not yet multiplied by 5. The next term is the minimum of the three candidate products; append it, and advance every finger whose candidate was used (several can tie - 6 is both 2 x 3 and 3 x 2, and both fingers must move).
The method works because every ugly number beyond 1 is 2, 3 or 5 times a smaller ugly number, so the ordered sequence generates itself from its own history. It runs in time proportional to the number of terms, which is how the 1500th term is reachable at all: brute-force testing would have to sift through 859963392 integers to find it.
The first terms and a worked example
The sequence opens 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36. At index 10 the term is 12, factorising as 2 x 2 x 3, and the first ten terms add to 60. At index 15 the term is 24 = 2 x 2 x 2 x 3; at index 20 it is 36 = 2 x 2 x 3 x 3, and the first twenty terms add to 303.
The calculator's examples use exactly these indices, so the printed steps can be checked against the sequence by eye. The neighbours printed with each term - 10 and 15 around the term 12, for instance - are the surest quick audit: an alleged term that does not sit between its claimed neighbours in the generated order is wrong.
A practical way to feel the generation: the three pointers move at very different speeds. The 2-pointer advances almost every step, the 3-pointer regularly, and the 5-pointer only occasionally - which is why the multiples of 5 arrive at widening intervals in the sequence: 5, 10, 15, 20, 25, 30, then 40, 45, 50, each one resetting the local pattern of doubles and triples around it. Watching which pointer produced a given term is a complete second description of the sequence, and the factorisation printed with each result in the calculator is exactly that record, term by term.
How sparse the ugly numbers are
Ugly numbers thin out fast. There are exactly 34 of them up to 100, and exactly 1530 up to a billion - against a billion integers competing for those places. The count up to x grows like (log x)^3, a consequence of the three free exponents: the number of triples (a, b, c) with 2^a 3^b 5^c at most x is the volume of a tetrahedron in log space.
That sparsity is the practical moral of the page: membership is precious and generation must be clever. It is also why the sequence appears in algorithm courses - it is the canonical example of a problem where the naive method is hopeless and the right observation makes it trivial.
Ugly versus smooth numbers
An ugly number is exactly a 5-smooth number: B-smooth means no prime factor exceeds B, and the ugly numbers are the case B = 5, listed in order. This site's smooth-number calculator tests B-smoothness for a general B and counts smooth numbers in ranges; this page does the complementary job for the most famous case - it generates the 5-smooth sequence term by term and certifies each term by factorisation.
The two viewpoints meet in the count: the 1530 ugly numbers up to a billion are the 5-smooth count at that bound. If a problem asks for the sequence, come here; if it asks whether a given large integer is B-smooth for some other B, the smooth-number page is the tool.
Where ugly numbers show up
Beyond the algorithms classroom, the same numbers are the regular numbers of Babylonian mathematics, used because their reciprocals have terminating sexagesimal expansions - the ancient version of preferring denominators built from 2, 3 and 5. In music theory they bound the intervals of 5-limit just intonation, whose ratios use only the primes 2, 3 and 5.
They also appear wherever merge-style generation is taught: the three-pointer method is the template for merging sorted streams, and the ugly numbers are its standard demonstration data - small enough to check by hand, rich enough to expose every bug in a careless implementation.
The Babylonian inheritance
Long before Hamming, Babylonian scribes built their arithmetic on these numbers. In base 60, a fraction terminates exactly when its denominator is regular - built from 2, 3 and 5, the prime factors of 60 itself - so the scribes tabulated regular numbers and their reciprocals and simply declined to divide by anything else. The sequence on this page is, quite literally, a table of the denominators Babylonian mathematics was willing to touch.
That history explains the otherwise odd convention of including 1 and the reverence for the count 1530: in a sexagesimal world the regular numbers up to a bound were a working tool, not a curiosity. The modern algorithmic fame is a second career for a sequence that had already served as the backbone of one civilisation's arithmetic - few sequences can claim both.
Common mistakes
The commonest slip is admitting a number with a hidden large prime: 42 looks tame but is 2 x 3 x 7, and is not ugly. The second is a generation bug at ties - failing to advance all three pointers when candidates coincide produces duplicates, classically a repeated 6.
A third is an indexing slip: this calculator counts from n = 1 with the term 1, so the tenth term is 12. Tables that start from the Babylonian convention or drop the 1 shift every index; the factorisation check printed with each term settles any dispute immediately.
FAQs
What is an ugly number?
A positive integer whose only prime factors are 2, 3 and 5 - also called a Hamming number, and exactly the 5-smooth numbers in order.
What is the 10th ugly number?
12.
What is the 20th ugly number?
36, factorising as 2 x 2 x 3 x 3.
How many ugly numbers are there up to 100?
34.
What is the 1500th ugly number?
859963392 - the classic Hamming problem value.
How are ugly numbers generated?
In order, by always taking the smallest unused multiple of an earlier term by 2, 3 or 5 - the three-pointer method this calculator's steps describe.
Is 42 an ugly number?
No - 42 = 2 x 3 x 7, and the factor 7 disqualifies it.
Why are they called Hamming numbers?
For Richard Hamming, who proposed generating the sequence efficiently as a programming problem; Dijkstra's treatment made it famous.