I would like to decompose a number into a set of numbers as close as possible to each other in size, whose product is the starting number. The inputs are the number nwe want to determine and the number of mdesired factors.
For two factors ( m==2), it’s enough to find the largest coefficient less than the square root, so I can do something like this
def get_factors(n):
i = int(n**0.5 + 0.5)
while n % i != 0:
i -= 1
return i, n/i
So calling this using 120will result in 10,12.
I understand that there is some ambiguity about what it means that the numbers will be "close to each other in size." I do not mind if this is interpreted as minimizing Σ(x_i - x_avg)or Σ(x_i - x_avg)^2or something else in general along these lines.
In case m==3I would expect to 336create 6,7,8and 729to create 9,9,9.
Ideally, I would like to get a solution for the general m, but if someone has an idea even for m==3, it will be very appreciated. I welcome general heuristics.
EDIT: I would rather minimize the sum of the factors. Still interested in the above, but if someone has an idea for a way to determine the optimal value m, so that the sum of the factors is minimal, that would be great!
source
share