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
let sum = 0 ; function Combination ( a , combi , n , r , depth , index ) { if ( index == r ) { let product = 1 ; for ( let i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( let i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } function allCombination ( a , n ) { for ( let i = 1 ; i <= n ; i ++ ) { let combi = [ ] ; Combination ( a , combi , n , i , 0 , 0 ) ; document . write ( " " + i + " " + sum + " " ) ; sum = 0 ; } } let n = 5 ; let a = [ ] ; for ( let i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ;