Minimum number of Factorials whose sum is equal to N | Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Prmin count ; Prresult ; Driver program
fact = [ 0 ] * 14 NEW_LINE size = 1 NEW_LINE def preCompute ( N ) : NEW_LINE INDENT global size NEW_LINE fact [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE while fact [ i - 1 ] <= N : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE size += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def findMin ( N ) : NEW_LINE INDENT preCompute ( N ) NEW_LINE originalN = N NEW_LINE ans = [ ] NEW_LINE for i in range ( size - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( N >= fact [ i ] ) : NEW_LINE INDENT N -= fact [ i ] NEW_LINE ans . append ( fact [ i ] ) NEW_LINE DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 27 NEW_LINE findMin ( n ) NEW_LINE