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 ; Given Input ; Function Call
function fact ( n ) { let ans = 1 ; for ( let i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } function remainder ( n , a , p ) { let len = fact ( n ) ; let ans = 1 ; for ( let i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } let A = 2 , N = 1 , P = 2 ; document . write ( remainder ( N , A , P ) ) ;