Check if a number is Euler Pseudoprime | Function that returns true if n is composite ; Check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that returns true if N is Euler Pseudoprime to the base A ; Invalid base ; N is not a composite odd number ; If A and N are not coprime ; All the conditions for Euler Pseudoprime are satisfied ; Driver code
function isComposite ( n ) { for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return true ; } return false ; } function Power ( x , y , p ) { let res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function isEulerPseudoprime ( N , A ) { if ( A <= 0 ) return false ; if ( N % 2 == 0 || ! isComposite ( N ) ) return false ; if ( __gcd ( A , N ) != 1 ) return false ; let mod = Power ( A , ( N - 1 ) / 2 , N ) ; if ( mod != 1 && mod != N - 1 ) return false ; return true ; } let N = 121 , A = 3 ; if ( isEulerPseudoprime ( N , A ) ) document . write ( " " ) ; else document . write ( " " ) ;