Is there an algorithm to find ALL factorizations of an integer, preferably in Python/Java but any feedback is welcome.
I have an algorithm to calculate the prime factors. For instance [2,2,5]
are the prime factors of 20
.
def prime_factorization(n):
primfac = []
d = 2
while d*d <= n:
while (n % d) == 0:
primfac.append(d)
n /= d
d += 1
if n > 1:
primfac.append(n)
return primfac
I also have an algorithm to compute all factors (prime and non-prime) of an algorithm. For instance, the factors of 20
are [1, 2, 4, 5, 10, 20]
.
def factors(n):
a, r = 1, [1]
while a * a < n:
a += 1
if n % a: continue
b, f = 1, []
while n % a == 0:
n //= a
b *= a
f += [i * b for i in r]
r += f
if n > 1: r += [i * n for i in r]
return sorted(r)
What I'm searching for is an algorithm to return all factorizations (not factors) of a given integer. For the integer 20
, the algorithm would produce the following:
[1,20]
[2,10]
[4,5]
[2,2,5]
Thanks!