From a given number, determine three close numbers whose product is the original number
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If the goal is to find three integers whose product is a given number and whose values are as close to each other as possible, the problem is really a balanced factorization problem. The ideal target is the cube root of the number, because three equal factors would all be exactly N^(1/3) if that value were an integer and if an exact triple existed.
Start Near the Cube Root
Suppose you want integers a, b, and c such that:
If the numbers are supposed to be close, then each one should be near the cube root of N. That gives you a search strategy:
- enumerate divisors
aup to the cube root ofN - for each valid
a, reduce the problem to factoringN / ainto two numbers - choose the triple with the smallest spread, such as
max - min
This works because once a is fixed, the remaining two factors must multiply to N / a, and the closest pair for that quotient will be near its square root.
Exact Integer Solution by Search
A brute-force search over every triple is wasteful. A better exact method only checks divisors.
Example output:
The 540 case is a good example. Many factorizations exist, but (6, 9, 10) is more balanced than something like (3, 6, 30).
Why This Works
Balanced factors minimize the ratio and the difference between the smallest and largest factor. If one factor is extremely small, one of the others must be extremely large to keep the product fixed. That immediately makes the triple less “close.”
The cube root gives the theoretical center. For perfect cubes such as 216 or 1000, the answer is especially clean:
- '
216 = 6 * 6 * 6' - '
1000 = 10 * 10 * 10'
For numbers that are not perfect cubes, you usually look for the nearest exact triple of integer divisors.
Picking a Closeness Metric
You should decide what “close” means before writing the algorithm. Common choices include:
- minimizing
max(a, b, c) - min(a, b, c) - minimizing the sum of pairwise differences
- minimizing the largest ratio between factors
For many puzzle and interview-style versions of the problem, minimizing the range is enough and is easy to reason about.
If Exact Integers Do Not Matter
If the problem allows real numbers instead of integers, the answer is trivial: the three closest numbers are all the same, namely the cube root of N.
But most practical versions of this question mean integer factors, so exact divisibility is the real challenge.
Common Pitfalls
The most common mistake is searching around the cube root and accepting nearby integers without checking that their exact product equals N. Near the cube root is a heuristic starting point, not a proof.
Another mistake is forgetting that there can be many valid triples. You need a consistent closeness metric to decide which one is best.
It is also easy to use a full triple nested loop, which becomes much slower than necessary. Restricting the search to divisors up to the cube root and square root is a much better exact approach.
Summary
- The problem is best treated as balanced integer factorization.
- The cube root of
Ngives the natural center for the search. - Enumerate divisors instead of trying every triple.
- Choose a clear closeness metric such as minimizing the spread.
- If real numbers are allowed, the exact balanced answer is simply three copies of
N^(1/3).

