Advertisement
Guest User

Subset Product

a guest
Aug 22nd, 2021
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import itertools
  2. from itertools import combinations
  3. from functools import reduce
  4. from operator import mul
  5. import math
  6.  
  7.  
  8. # Subset Product: Find all divisors of TARGET, output the SIZE of ALL K-sets that have product
  9. # equal to TARGET)
  10.  
  11. TARGET = math.factorial(12)
  12. SET = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
  13. SET = set(SET)
  14.  
  15. divisors = []
  16. for i in SET:
  17.     if TARGET % i == 0:
  18.         divisors.append(i)
  19.  
  20.  
  21. divisors = sorted(divisors)
  22.  
  23. max_combo = []
  24. for a in divisors:
  25.     max_combo.append(a)
  26.     if reduce(mul,max_combo) >= TARGET:
  27.         if reduce(mul,max_combo) > TARGET:
  28.             max_combo.pop()
  29.             break
  30.         else:
  31.             break
  32. count = 0
  33. for A in range(len(max_combo), 0, -1):
  34.     if A <= len(divisors):
  35.         for X in combinations(divisors, A):
  36.             count = count + 1
  37.             if reduce(mul, X) == TARGET:
  38.                 print(X, 'K-Size: ',len(X))
  39.                 quit()
  40.             if A == len(max_combo):
  41.                 break
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement