Sum of series formed by difference between product and sum of N natural numbers | Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto current term ; Generate the remaining terms and calculate sum ; Update the sum ; Return the sum ; Driver Code
def seriesSum ( n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE currProd = 1 ; NEW_LINE currSum = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT currProd *= i ; NEW_LINE currSum += i ; NEW_LINE sum1 += currProd - currSum ; NEW_LINE DEDENT return sum1 ; NEW_LINE DEDENT N = 5 ; NEW_LINE print ( seriesSum ( N ) , end = " ▁ " ) ; NEW_LINE