Find the smallest regular number that is not less than N
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A regular number, also called a Hamming number, has no prime factors other than 2, 3, or 5. The problem asks for the smallest such number that is greater than or equal to a given N. The naive solution is to test numbers one by one, but the more useful approach is to generate regular numbers in sorted order and stop as soon as you reach the first value that is not less than N.
What Makes a Number Regular
A regular number can always be written as:
- '
2^a * 3^b * 5^c'
where a, b, and c are non-negative integers.
Examples:
- '
1is regular' - '
12 = 2^2 * 3is regular' - '
45 = 3^2 * 5is regular' - '
14 = 2 * 7is not regular because of the factor7'
This structure means the set of regular numbers can be generated systematically instead of discovered by repeated trial division over all integers.
A Simple Baseline Check
For small inputs, the most direct approach is to test each candidate until a regular number appears.
This works, but it becomes less attractive when N grows and the gaps between regular numbers widen.
Generate Regular Numbers in Sorted Order
A better algorithm generates the sequence in order using three pointers, similar to the classic ugly-number problem.
This avoids scanning non-regular integers entirely. It only constructs valid regular numbers and keeps them ordered.
Why the Pointer Method Works
Every regular number after 1 is obtained by multiplying an earlier regular number by 2, 3, or 5. The three pointers track the smallest unseen multiples of 2, 3, and 5. Taking the minimum of those candidates gives the next regular number in sorted order.
Handling duplicate candidates matters. For example, 6 can be reached as both 2 * 3 and 3 * 2. That is why the algorithm increments every pointer whose candidate equals the chosen next value.
Example Results
A few values make the behavior concrete:
- '
N = 23gives24' - '
N = 24gives24' - '
N = 26gives27' - '
N = 100gives100' - '
N = 101gives108'
Those examples also show why brute force becomes wasteful as N grows. Many non-regular values must be skipped, but the generated-sequence method never touches them.
Common Pitfalls
- Confusing regular numbers with powers of two only.
- Forgetting that
1counts as a regular number. - Using brute force for very large inputs when direct sequence generation is much cleaner.
- Failing to handle duplicate candidates such as
6,10, or15in the generation algorithm. - Assuming factorization of
Nalone is enough to jump directly to the next regular number.
Summary
- Regular numbers have no prime factors except 2, 3, and 5.
- The smallest regular number not less than
Ncan be found by brute force, but that is rarely the best algorithm. - Generating regular numbers in sorted order with three pointers is cleaner and more efficient.
- Duplicate candidate handling is essential in the generation approach.
- For algorithmic work, think in terms of constructing the valid sequence rather than filtering the whole integer line.

