## Nearly Square Divisors, Revisited

### August 23, 2016

In a recent exercise, we discussed the problem of computing the nearly square divisors of a composite number *n* = *a* · *b*, with *a* ≥ b, such that the difference *a* − *b* is as small as possible. We gave two solutions: The first solution enumerated the divisors one-by-one, by trial division counting from 1, until reaching the square root, which is fast if *n* is small but slow in general. The second solution factored *n*, computed the divisors, the picked the two divisors in the middle of the list, which is fast in general but slow if *n* is highly composite and thus has a large number of divisors.

In a comment on that exercise, Matthew Arcus, who is a regular reader and commentor at Programming Praxis, gave a splendid answer to the problem; it relies on factoring *n*, but is fast even if *n* has a large number of divisors. His algorithm reduces the multiplication of the factors to addition of their logarithms, which means that the knapsack algorithm can be used to find the greatest sum less than the logarithm of the square root.

Your task is to implement Matthew’s algorithm to find the nearly-square divisors of a number. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

How do this work? With logarithm?

You can use the knapsack algorithm with products, as well as with sums.

On a 64-bit machine all numbers are fixnums, so things go even a bit faster:

> (time (nsd-new 224403121196654400))

(time (nsd-new 224403121196654400))

63 ms real time

64 ms cpu time (64 user, 0 system)

no collections

3280 bytes allocated

no minor faults

no major faults

(473753280 473670855)

> (time (nsd 224403121196654400))

(time (nsd 224403121196654400))

148 ms real time

148 ms cpu time (148 user, 0 system)

77 collections accounting for 62 ms real time (56 user, 0 system)

165921824 bytes allocated

no minor faults

no major faults

(473753280 473670855)

And here’s some code (without sum or factors):

Here a description for a version of nsd, which is superfast. It solves Euler 266 in Python in 7 seconds! I implemented a C version of an improved “Matthew” algo, which solved it in about 100 minutes (a comparable Python version would have taken 100 hours).

Split the factors of number n in 2 equal parts, calculate all divisors for the 2 parts and sort both sets of divisors. Loop over both sets of divisors (one ascending and the other descending) and make sure the product is less equal isqrt(n). If the product is too high advance the descending, otherwise the ascending. Keep track of the highest prod below isqrt(n).

@Paul .. Could you give sample snippet or pseudo code of your idea? Especially about the splitting of divisors?

@Rudolf-san. Here is Python code. You need routines for integer square root (isqrt) and factoring (rho_factors).

In fact you do not split the divisors, but you split the factors and then calculate all divisors for them. Every divisor for n is a product of two divisors from either part. You are only interested in these products close to isqrt(n).