fork download
  1.  
  2. from itertools import accumulate
  3. from functools import reduce
  4. from operator import mul
  5.  
  6. def Partition(n, m, L=[]):
  7. if m == 1 and n:
  8. yield [n]+L
  9. else:
  10. for i in range(1, n):
  11. yield from Partition(n-i, m-1, [i]+L)
  12.  
  13. def MAX_product(N, K):
  14. N_str = str(N)
  15. n = len(N_str)
  16. if n < K - 1:
  17. print('K too large.')
  18. return (-1, [])
  19.  
  20. R = []
  21. for p in Partition(n, K+1):
  22. L = [ 0 ] + list(p)
  23. pa = list(accumulate(L))
  24. Numbers = list(map(int, (N_str[a:b] for a, b in zip(pa, pa[1:]))))
  25. Product = reduce(mul, Numbers, 1)
  26. R.append((Product, Numbers))
  27.  
  28. return sorted(R, key=lambda t: t[0])[-1]
  29.  
  30. P1, L1 = MAX_product( 746589, 2)
  31. print(P1, '=', ' x '.join(map(str, L1)))
  32.  
  33. P2, L2 = MAX_product(1111114, 3)
  34. print(P2, '=', ' x '.join(map(str, L2)))
  35.  
Success #stdin #stdout 0.02s 10336KB
stdin
Standard input is empty
stdout
537480 = 7465 x 8 x 9
5324 = 11 x 11 x 11 x 4