I am writing a program that processes data based on an integer factor. The process can either be done at once, or in multiple stages. For example, one stage with factor 500 could be replaced by three stages, the first one with factor 50, the second with factor 5, the third with factor 2. Because, 50*5*2 = 500. The reason for this is that the run-time complexity of the code is directly proportional to this integer.
It is best to order these sub-factors by size, largest first. Like in the example above: [50,5,2]. But, it is also best to make sure that the sum of these sub-factors is as small as possible. This means for example that [10,10,5] is better than [50,5,2], because the sum of the former is 25, while the sum of the latter is 57. This sum defines the run-time complexity, as mentioned above. The lower the number, the less work needs to be done, and the faster the code is.
It is of course possible that a factor simply cannot be decomposed into exactly the given number of sub-factors. One example is 25: Here, only [5,5] is possible. For this reason, I want something that decomposes an integer number into up to N numbers (N being a user specified parameter), with the constraint above about the sum of these numbers being as small as possible.
So far, I've had no luck finding anything that solves this. Does anybody know more?
The following algorithm gives an approximate solution, which is exact if $N=2$.
Let $a$ be the integer to be decomposed as a product of $N$ factors.
If $N=2$, let $d_1=\max\{k\in\Bbb N:k\text{ divides }a,k\le\sqrt a\} $ and $d_2=a/d_1$. Then $a=d_1\times d_2$ is the optimal solution. For instance, if $a=112476$, then $\sqrt a=335.374$, $d_1=309$, $d_2=364$ and $d_1+d_2=673$.
Suppose now that $N=3$. Choose $d_1=\max\{k\in\Bbb N:k\text{ divides }a,k\le a^{1/3}\}$ and apply the method for $N=2$ to $a/d_1$. As an example, take $a=112476$ again. Then $a^{1/3}=48.271$ and $d_1=42$; applying the method for $N=2$ to $a/d_1=2678$ we find $a/d_1=26\times103$, giving the decomposition $$ 112476=42\times26\times103,\quad42+26+103=171. $$ The actual minimum is given by $$ 112476=28\times39\times103,\quad28+39+103=170. $$ Not bad.
The process can be iterated to any value of $N$. Choose $d_1=\max\{k\in\Bbb N:k\text{ divides }a,k\le a^{1/N}\}$ and apply the method for $N-1$ to $a/d_1$.
For large $a$, the most time consuming part will be the computation of the divisors of $a$. However, not all of them are needed. Start from $\lfloor a^{1/N}\rfloor$ and go down by one until you find a divisor of $a$.