Sum of the Tan ( x ) expansion upto N terms | Function to find factorial of a number ; To store factorial of a number ; Return the factorial of a number ; Function to find tan ( x ) upto n terms ; To store value of the expansion ; This loops here calculate Bernoulli number which is further used to get the coefficient in the expansion of tan x ; Print the value of expansion ; Driver code ; Function call
function fac ( num ) { if ( num == 0 ) return 1 ; let fact = 1 ; for ( let i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } function Tanx_expansion ( terms , x ) { let sum = 0 ; for ( let i = 1 ; i <= terms ; i += 1 ) { let B = 0 ; let Bn = 2 * i ; for ( let k = 0 ; k <= Bn ; k ++ ) { let temp = 0 ; for ( let r = 0 ; r <= k ; r ++ ) temp = temp + Math . pow ( - 1 , r ) * fac ( k ) * Math . pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( k + 1 ) ; } sum = sum + Math . pow ( - 4 , i ) * ( 1 - Math . pow ( 4 , i ) ) * B * Math . pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } document . write ( sum . toFixed ( 10 ) ) ; } let n = 6 , x = 1 ; Tanx_expansion ( n , x ) ;