Sum of the series Kn + ( K ( n | Recursive Javascript program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
function exponent ( A , B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; let y ; if ( B % 2 == 0 ) { y = exponent ( A , parseInt ( B / 2 , 10 ) ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } function sum ( k , n ) { let sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } let n = 3 ; let K = 3 ; document . write ( sum ( K , n ) ) ;