Factoring a number into roughly equal factors

factor calculator
how to find factors of a large number
shortcut to find factors of a number
prime factorization
prime numbers
how to find factors of a number on a calculator
shortcut to find number of factors of a number
factors of 12

I would like to decompose a number into a tuple of numbers as close to each other in size as possible, whose product is the initial number. The inputs are the number n we want to factor and the number m of factors desired.

For the two factor situation (m==2), it is enough to look for the largest factor less than a 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 with 120 will result in 10,12.

I realize there is some ambiguity as to what it means for the numbers to be "close to each other in size". I don't mind if this is interpretted as minimizing Σ(x_i - x_avg) or Σ(x_i - x_avg)^2 or something else generally along those lines.

For the m==3 case, I would expect that 336 to produce 6,7,8 and 729 to produce 9,9,9.

Ideally, I would like a solution for general m, but if someone has an idea even for m==3 it would be much appreciated. I welcome general heuristics too.

EDIT: I would prefer to minimize the sum of the factors. Still interested in the above, but if someone has an idea for a way of also figuring out the optimal m value such that the sum of factors is minimal, it'd be great!

To answer your second question (which m minimizes the sum of factors), it will always be optimal to split number into its prime factors. Indeed, for any positive composite number except 4 sum of its prime factors is less that the number itself, so any split that has composite numbers can be improved by splitting that composite numbers into its prime factors.

To answer your first question, greedy approaches suggested by others will not work, as I pointed out in the comments 4104 breaks them, greedy will immediately extract 8 as the first factor, and then will be forced to split the remaining number into [3, 9, 19], failing to find a better solution [6, 6, 6, 19]. However, a simple DP can find the best solution. The state of the DP is the number we are trying to factor, and how many factors do we want to get, the value of the DP is the best sum possible. Something along the lines of the code below. It can be optimized by doing factorization smarter.

n = int(raw_input())
left = int(raw_input())


memo = {}
def dp(n, left): # returns tuple (cost, [factors])
    if (n, left) in memo: return memo[(n, left)]

    if left == 1:
        return (n, [n])

    i = 2
    best = n
    bestTuple = [n]
    while i * i <= n:
        if n % i == 0:
            rem = dp(n / i, left - 1)
            if rem[0] + i < best:
                best = rem[0] + i
                bestTuple = [i] + rem[1]
        i += 1

    memo[(n, left)] = (best, bestTuple)
    return memo[(n, left)]


print dp(n, left)[1]

For example

[In] 4104
[In] 4 
[Out] [6, 6, 6, 19]

Factoring Numbers, "Factors" are the numbers you multiply to get another number. You most often want to find the "prime factorization" of a number: the list of all the prime-number factors of a given number. That is because 8 does NOT equal 2�2�2�4! The nice thing about this upside-down division is that, when you're done, the prime� Enter any Number into this free calculator Our calculator will display all factors of any number. Note : If you are look for the prime factors of a number, use this calculator .

You can start with the same principle: look for numbers under or equal to the mth root that are factors. Then you can recurse to find the remaining factors.

def get_factors(n, m):
    factors = []
    factor = int(n**(1.0/m) + .1) # fudged to deal with precision problem with float roots
    while n % factor != 0:
        factor = factor - 1
    factors.append(factor)
    if m > 1:
        factors = factors + get_factors(n / factor, m - 1)
    return factors

print get_factors(729, 3)

Integer factorization, In number theory, integer factorization is the decomposition of a composite number into a product of smaller integers. If these factors are further restricted to prime numbers, the process is called Not all numbers of a given length are equally hard to factor. The hardest instances of these problems (for currently known� The quickest way to find the factors of a number is to divide it by the smallest prime number (bigger than 1) that goes into it evenly with no remainder. Continue this process with each number you get, until you reach 1.

How about this, for m=3 and some n:

  1. Get the largest factor of n smaller than the cube root of n, call it f1
  2. Divide n by f1, call it g
  3. Find the "roughly equal factors" of g as in the m=2 example.

For 336, the largest factor smaller than the cube root of 336 is 6 (I think). Dividing 336 by 6 gives 56 (another factor, go figure!) Performing the same math for 56 and looking for two factors, we get 7 and 8.

Note that doesn't work for any number with fewer than 3 factors. This method can be expanded for m > 3, maybe.

If this is right, and I'm not too crazy, the solution would be a recursive function:

factors=[]
n=336
m=3

def getFactors(howMany, value):
  if howMany < 2:
    return value

  root=getRoot(howMany, value) # get the root of value, eg square root, cube, etc.
  factor=getLargestFactor(value, root) # get the largest factor of value smaller than root
  otherFactors=getFactors(howMany-1, value / factor)
  otherFactors.insert(factor)
  return otherFactors
print getFactors(n, m)

I'm too lazy to code the rest, but that should do it.

Fermat's factorization method, Fermat's factorization method, named after Pierre de Fermat, is based on the representation of if neither factor equals one, it is a proper factorization of N. the first try for a is the square root of 5959 rounded up to the next integer, which is and the factors are roughly equal: Fermat's, applied to Nuv, will find them quickly. Find a pair of numbers that multiply to the top number and add to the bottom number, and place these in the two side quadrants (the order doesn’t matter). For the example, you want to find a pair of numbers that multiplies to 24 and adds up to 14. Begin by listing all the factor pairs of 24: 1×24, 2×12, 3×8, and 4×6.

m=5 n=4 then m^(1/n)

you get:

Answer=1.495

then

1.495*1.495*1.495*1.495 = 5

in C#

double Result = Math.Pow(m,1/(double)n);

Factoring Calculator, Factoring calculator to find the factors or divisors of a number. Factor calculator finds all factors and factor pairs of any positive non-zero integer. Factors You can also think about factors in terms of division: The factors of a number include all� Enter the expression you want to factor in the editor. The Factoring Calculator transforms complex expressions into a product of simpler factors. It can factor expressions with polynomials involving any number of vaiables as well as more complex functions. Difference of Squares: a2 – b2 = (a + b)(a – b) a 2 – b 2 = (a + b) (a – b)

Factoring - Factors and multiples - In Depth, A factor is simply a number that is multiplied to get a product. Factoring a For example, 4 equals 2 times 2, 6 equals 3 times 2, 8 equals 4 times 2, and so forth. I think that's all of them. Let's try out these numbers, and see if any of these add up to 15. So 1 plus 50 does not add up to 15. 2 plus 25 does not add up to 15. But 5 plus 10 does add up to 15. So this could be 5 plus 10, and this could be 5 times 10. So if we were to factor this, this would be equal to x plus 5, times x plus 10. And

All about Factors of a number, In this article, we will discuss the fundamentals of factors of a number. Almost every competitive examination has 2-3 medium to difficult level questions based on� They’re underlined here: x ( x – 2) + 5 ( x – 2). That’s a GCF because it appears in both terms (if you factor using this method, the last step should always look like this). Factor out the GCF from both terms (it’s always the expression inside the parentheses) to the front; you get ( x – 2) ( ).

How to Find All The Factors of a Number Quickly and Easily, Factor negative numbers in the same way as positive numbers, but make sure the factors multiply together to produce a About the Author Essentially, you want to figure out if you can multiply 2 by some integer to equal your constant. In the�

Comments
  • You could try generating all the prime factors, then generating every combination of m to find the minimum error.
  • For 4104 you will result with (3, 8, 9, 19), while (6, 6, 6, 19) is better with either loss function.
  • I like this greedy approach to the problem. It would come down to depth first search in the solution tree with exploring the largest possible factor first on each depth. Yet, in case there is no (howMany - x)'th factor, the algorithm should also be able to return "no solution". Alternatively, if a prime number is encounted before the desired depth is reached, it could simply return a "1" and solutions with "1" factors could be considered bad.
  • In other words: There is no need to call getFactors(howMany-1, value/factor) if value/factor is prime and the next depth of recursion is not the final depth.
  • Op is using integers, not doubles. Op is also using python, not C#.