Number Theory
Mathematics
Algebra
Problem Solving
Mathematical Puzzles

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:

text
a * b * c = N

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:

  1. enumerate divisors a up to the cube root of N
  2. for each valid a, reduce the problem to factoring N / a into two numbers
  3. 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.

A brute-force search over every triple is wasteful. A better exact method only checks divisors.

python
1from math import isqrt
2
3
4def closest_triple(n: int):
5    best = None
6    best_spread = None
7
8    a = 1
9    while a * a * a <= n:
10        if n % a == 0:
11            q = n // a
12            b = 1
13            while b * b <= q:
14                if q % b == 0:
15                    c = q // b
16                    triple = tuple(sorted((a, b, c)))
17                    spread = triple[2] - triple[0]
18                    if best is None or spread < best_spread:
19                        best = triple
20                        best_spread = spread
21                b += 1
22        a += 1
23
24    return best
25
26
27print(closest_triple(216))
28print(closest_triple(540))
29print(closest_triple(1000))

Example output:

text
(6, 6, 6)
(6, 9, 10)
(10, 10, 10)

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.

python
1n = 500
2x = n ** (1 / 3)
3print(x, x, x)
4print(x * x * x)

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 N gives 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).

Course illustration
Course illustration