from __future__ import print_function
import itertools
import operator
def partition(iterable, chain=itertools.chain, map=map):
# http://code.activestate.com/recipes/576795/
# In [1]: list(partition('abcd'))
# Out[1]:
# [['abcd'],
# ['a', 'bcd'],
# ['ab', 'cd'],
# ['abc', 'd'],
# ['a', 'b', 'cd'],
# ['a', 'bc', 'd'],
# ['ab', 'c', 'd'],
# ['a', 'b', 'c', 'd']]
s = iterable if hasattr(iterable, '__getslice__') else tuple(iterable)
n = len(s)
first, middle, last = [0], range(1, n), [n]
getslice = s.__getslice__
return [map(getslice, chain(first, div), chain(div, last))
for i in range(n) for div in itertools.combinations(middle, i)]
def product(factors,mul=operator.mul):
return reduce(mul,factors,1)
def factorings(factors,product=product,
permutations=itertools.permutations,
imap=itertools.imap,
chain_from_iterable=itertools.chain.from_iterable,
):
seen=set()
seen.add(tuple([product(factors)]))
for grouping in chain_from_iterable(
imap(
partition,
set(permutations(factors,len(factors)))
)):
result=tuple(sorted(product(group) for group in grouping))
if result in seen:
continue
else:
seen.add(result)
yield result
if __name__=='__main__':
for f in factorings([2,2,3,3,5,7]):
print(f,end=' ')
урожайность
(3, 420) (9, 140) (28, 45) (14, 90) (2, 630) (3, 3, 140) (3, 15, 28) (3, 14, 30) (2, 3, 210) (5, 9, 28) (9, 10, 14) (2, 9, 70) (2, 14, 45) (2, 7, 90) (3, 3, 5, 28) (3, 3, 10, 14) (2, 3, 3, 70) (2, 3, 14, 15) (2, 3, 7, 30) (2, 5, 9, 14) (2, 7, 9, 10) (2, 2, 7, 45) (2, 3, 3, 5, 14) (2, 3, 3, 7, 10) (2, 2, 3, 7, 15) (2, 2, 5, 7, 9) (2, 2, 3, 3, 5, 7) (5, 252) (10, 126) (18, 70) (6, 210) (2, 5, 126) (5, 14, 18) (5, 6, 42) (7, 10, 18) (6, 10, 21) (2, 10, 63) (3, 6, 70) (2, 5, 7, 18) (2, 5, 6, 21) (2, 2, 5, 63) (3, 5, 6, 14) (2, 3, 5, 42) (3, 6, 7, 10) (2, 3, 10, 21) (2, 3, 5, 6, 7) (2, 2, 3, 5, 21) (4, 315) (20, 63) (2, 2, 315) (4, 5, 63) (4, 9, 35) (3, 4, 105) (7, 9, 20) (3, 20, 21) (2, 2, 9, 35) (2, 2, 3, 105) (4, 5, 7, 9) (3, 4, 5, 21) (3, 3, 4, 35) (3, 3, 7, 20) (2, 2, 3, 3, 35) (3, 3, 4, 5, 7) (7, 180) (3, 7, 60) (2, 18, 35) (2, 6, 105) (3, 10, 42) (2, 3, 6, 35) (15, 84) (12, 105) (3, 5, 84) (5, 12, 21) (7, 12, 15) (4, 15, 21) (2, 15, 42) (3, 5, 7, 12) (3, 4, 7, 15) (2, 6, 7, 15) (2, 2, 15, 21) (21, 60) (30, 42) (6, 7, 30) (5, 7, 36) (2, 21, 30) (5, 6, 6, 7) (3, 12, 35) (6, 14, 15) (4, 7, 45) (35, 36) (6, 6, 35)