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
def subfactorial ( N ) : NEW_LINE INDENT res = 0 NEW_LINE fact = 1 NEW_LINE count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE if ( count % 2 == 0 ) : NEW_LINE INDENT res = res - ( 1 / fact ) NEW_LINE DEDENT else : NEW_LINE INDENT res = res + ( 1 / fact ) NEW_LINE DEDENT count += 1 NEW_LINE DEDENT return fact * ( 1 + res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( subfactorial ( N ) ) NEW_LINE DEDENT