Sum of series formed by difference between product and sum of N natural numbers | Recursive Function to calculate the sum upto Nth term ; If N - th term is calculated ; Update multi to store product upto K ; Update add to store sum upto K ; Update prevSum to store sum upto K ; Proceed to next K ; Function to calculate and return the Sum upto Nth term ; Recursive Function ; Driver code
function seriesSumUtil ( k , n , prevSum , multi , add ) { if ( k == n + 1 ) { return prevSum ; } multi = multi * k ; add = add + k ; prevSum = prevSum + multi - add ; return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; } function seriesSum ( n ) { if ( n == 1 ) return 0 ; let prevSum = 0 ; let multi = 1 ; let add = 1 ; return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; } let N = 5 ; document . write ( seriesSum ( N ) ) ;