Repeated sum of first N natural numbers | Function to return the sum of the first n natural numbers ; Function to return the repeated sum ; Perform the operation exactly k times ; Update n with the sum of first n natural numbers ; Driver code
function sum ( n ) { var sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } function repeatedSum ( n , k ) { for ( i = 0 ; i < k ; i ++ ) { n = sum ( n ) ; } return n ; } var n = 2 , k = 2 ; document . write ( repeatedSum ( n , k ) ) ;