Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | Utility function to find ; A Simple Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Driver Code
function factorial ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } function sum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += 1.0 / factorial ( i ) ; return sum ; } let n = 5 ; document . write ( sum ( n ) . toFixed ( 5 ) ) ;