Find remainder when a number A raised to N factorial is divided by P | Function to calculate factorial of a Number ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
def fact ( n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT ans *= i NEW_LINE DEDENT return ans NEW_LINE DEDENT def remainder ( n , a , p ) : NEW_LINE INDENT len1 = fact ( n ) NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , len1 + 1 , 1 ) : NEW_LINE INDENT ans = ( ans * a ) % p NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 NEW_LINE N = 1 NEW_LINE P = 2 NEW_LINE print ( remainder ( N , A , P ) ) NEW_LINE DEDENT