Factoring factorials
Jelly, 17 18 bytes
!P
ÇṗLÇ⁼¥ÐfÇḢḟ1ȯ0F
A monadic link taking and returning a list of the numbers (sticks to the one factorial per number option)
Try it online!
How?
A golfed (although independently written) version of Pietu1998's solution.
!P - Link 1, product of factorials: list
! - factorial (vectorises)
P - product
ÇṗLÇ⁼¥ÐfÇḢḟ1ȯ0F - Main link: list e.g. [3,2,2]
Ç - call the last link (1) as a monad 24
L - length 3
ṗ - Cartesian power [[1,1,1],[1,1,2],...,[1,1,24],...,[24,24,24]]
Ç - call the last link (1) as a monad 24
Ðf - filter keep if:
¥ - last two links as a dyad:
Ç - call the last link (1) as a monad [1,2,...,24!,...,24!^3]
⁼ - equal?
Ḣ - head
ḟ1 - filter out any ones
ȯ0 - or with zero (for the empty list case)
F - flatten (to cater for the fact that zero is not yet a list)
Jelly, 19 bytes
,!P€E
SṗLçÐfµḢḟ1ȯ1F
Try it online!
Quick and dirty. Very slow, even the 23!2!3!2!
test case is a stretch. I/O as lists of integers.
Explanation
,!P€E Helper link. Arguments: attempt, original
, Make the array [attempt, original].
Example: [[1,1,1,4], [2,3,2,0]]
! Take the factorial of each item.
Example: [[1,1,1,24], [2,6,2,1]]
P€ Take the product of each sublist.
Example: [24, 24]
E Check if the values are equal.
SṗLçÐfµḢḟ1ȯ1F Main link. Arguments: original
S Find the sum S of the integers in the input.
L Find the number N of integers in the input.
ṗ Generate all lists containing N integers from 1 to S.
çÐf Take the lists whose factorial-product is the same as the original.
Ḣ Take the first match. This is the one with the most ones.
ḟ1 Remove any ones.
ȯ1 If there were only ones, return a one instead.
F Turn into a list if needed.
Clean, 397 ... 317 bytes
import StdEnv,StdLib
c=length
f c m=sortBy c o flatten o map m
%n=f(>)@[2..n]
@1=[]
@n#f=[i\\i<-[2..n]|n/i*i==n&&and[i/j*j<i\\j<-[2..i-1]]]
=f++ @(n/prod f)
?l=group(f(>)%l)
$l=hd(f(\a b=c a<c b)(~(?l))[0..sum l])
~[]_=[[]]
~i n=[[m:k]\\m<-take n[hd(i!!0++[0])..],k<- ~[drop(c a)b\\a<-group(%m)&b<-i|b>a]n|i== ?[m:k]]
Try it online!
This takes an [Int]
, determines the prime factors of the result, and reduces over the factors to find the smallest representation, using the largest factor at any stage as a baseline value for the next factorial term. It won't complete some test cases on TIO, but it is fairly* fast, and can run them all in under 3 minutes on a midrange laptop.
* for an O((prod(N)!)^sum(N))
complexity algorithm