Sum of products of all combination taken ( 1 to n ) at a time | to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver Code ; storing numbers from 1 - N in array ; calling allCombination
def Combination ( a , combi , n , r , depth , index ) : NEW_LINE INDENT global Sum NEW_LINE if index == r : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( r ) : NEW_LINE INDENT product = product * combi [ i ] NEW_LINE DEDENT Sum += product NEW_LINE return NEW_LINE DEDENT for i in range ( depth , n ) : NEW_LINE INDENT combi [ index ] = a [ i ] NEW_LINE Combination ( a , combi , n , r , i + 1 , index + 1 ) NEW_LINE DEDENT DEDENT def allCombination ( a , n ) : NEW_LINE INDENT global Sum NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT combi = [ 0 ] * i NEW_LINE Combination ( a , combi , n , i , 0 , 0 ) NEW_LINE print ( " f ( " , i , " ) ▁ - - > ▁ " , Sum ) NEW_LINE Sum = 0 NEW_LINE DEDENT DEDENT Sum = 0 NEW_LINE n = 5 NEW_LINE a = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = i + 1 NEW_LINE DEDENT allCombination ( a , n ) NEW_LINE