これにより、乗算して元の数を与えるすべての因数セットが生成されます。すべての製品セットを、並べ替えられたタプルの一意のリストとして返します。
s は、1
無限再帰を避けるために除外されます。
def prime_factors(n):
return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
def product_sets(n):
return set(products(1, [], n, prime_factors(n)))
def products(current_product, current_list, aim, factors):
if current_product == aim:
yield tuple(sorted(current_list))
elif 0 < current_product < aim:
for factor in factors:
if factor != 1:
for product in products(current_product * factor, current_list + [factor], aim, factors):
yield product
print list(product_sets(24))
出力:
[(4, 6), (3, 8), (2, 12), (2, 3, 4), (24,), (2, 2, 6), (2, 2, 2, 3)]