Find subfactorial of a number | Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code
function subfactorial ( N ) { let res = 0 , fact = 1 ; let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { fact = fact * i ; if ( count % 2 == 0 ) res = res - 1 / fact ; else res = res + 1 / fact ; count ++ ; } return fact * ( 1 + res ) ; } let N = 4 ; document . write ( subfactorial ( N ) ) ;