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
def seriesSumUtil ( k , n , prevSum , multi , add ) : NEW_LINE INDENT if ( k == n + 1 ) : NEW_LINE INDENT return prevSum ; NEW_LINE DEDENT multi = multi * k ; NEW_LINE add = add + k ; NEW_LINE prevSum = prevSum + multi - add ; NEW_LINE return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; NEW_LINE DEDENT def seriesSum ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT prevSum = 0 ; NEW_LINE multi = 1 ; NEW_LINE add = 1 ; NEW_LINE return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE print ( seriesSum ( N ) ) ; NEW_LINE DEDENT