Given an integer $n\in\Bbb N$, what's the best (known) way to decompose it as the product of two integers in such a way that the factors are the sides of a rectangle that's as close as possible to a square? The full factorization of $n$ into prime factors can be assumed to be known if that's helpful.
More formally:
Given $n\in \Bbb N$, find a divisor $d|n$ such that $ |d-n/d| \stackrel{!}= \min$.
or
Given $n\in \Bbb N$, find $a, b\in\Bbb N$ such that $a\cdot b=n$ and $a+b \stackrel{!}= \min$.
Using the factorization $n = \displaystyle \prod_{j=1}^k p_j$ where primes might occur more than once, this can be restated using logarithms:
Find $k$ values $b_j\in\{-1,1\}$ such that $\displaystyle \Bigg|\sum_{j=1}^k b_j\log p_j \Bigg|\stackrel{!}= \min$.
This looks like a knapsack problem which are known to be hard in general. For $n > 1$ there are $2^{k-1}$ possible decompositions into different pairs of factors, and checking all of them is expensive when $n$ has many factors. Moreover:
If someone comes up with a specific decomposition asserting it's the most square-ish one: Is it easier to check whether the solution is correct, or is it as expensive as determining the best solution in the first place?
As an example, let $H_D(x)\in \Bbb Z[x]$ be the Hilbert class polynomial for discriminant $D$ and let $n=H_{-71}(0)$: $$n = 737707086760731113357714241006081263 = 11^9 \cdot 17^6 \cdot 23^3 \cdot 41^3 \cdot 47^3 \cdot 53^3$$
See [1]. This $n$ has $2^{26} = 67\,108\,864$ different decompositions, which is doable with brute force on a modern computer. But is there a smarter approach?
Two more demanding test cases are
$H_{-119}(0) = (11^4 \cdot 17 \cdot 23^2 \cdot 29^2 \cdot 47 \cdot 59 \cdot 83 \cdot 89)^3$ with $2^{38}\approx 2.7\cdot 10^{11}$ decompositions and
$H_{-907}(0) = (2^{19} \cdot 3^3 \cdot 5^3 \cdot 131 \cdot 137 \cdot 167)^3$ with $2^{83}\approx 9.7\cdot 10^{24}$ decompositions.
Note 1
As Jukka Kohonen mentions in a comment, the upper bound for the complexity of a brute-force approach can be reduced: Let $n=\displaystyle\prod_{i=1}^m p_i^{e_i}$ with pairwise different primes $p_i$ be the factorization of $n$. Then the task is:
Find $m$ integers $a_i$ with $0\leqslant a_i \leqslant e_i$ such that $\Bigg|\log n - 2\displaystyle \sum_{i=1}^m a_i\log p_i \Bigg| \stackrel!= \min$.
If all $a_i$ are replaced by $e_i-a_i$ then the value to be minimized stays the same.
- If all $e_i$ are even, then $n$ is a perfect square and the solution is clear.
- If at least one $e_i$ is odd, then a brute-force approach has to test $\dfrac12\displaystyle\prod_{i=1}^m (1+e_i)$ combinations of the $a_i$.
The examples above are all in case 2., and the corrected number of tries are:
$H_{-71}(0)$: $\frac12\cdot 10 \cdot 7 \cdot 4 \cdot 4 \cdot 4 \cdot 4 = 8960$ possibilities.
$H_{-119}(0)$: $\frac12\cdot 13 \cdot 4 \cdot 7 \cdot 7 \cdot 4 \cdot 4 \cdot 4 \cdot 4 = 326144$ possibilities.
$H_{-907}(0)$: $\frac12\cdot 58 \cdot 10 \cdot 10 \cdot 4 \cdot 4 \cdot 4 = 185600$ possibilities.
Conclusion:
The work that's needed to get the complete factorization of $n$ is in general much higher than brute-force checking which pair of divisors is the best choice.
Related:
- How to get Most square rectagle: No answers, asked for explicit formula.
- Is there a name for the "most square" factorization of an integer?: Asks for nomenclature.