Check if a number is Euler Pseudoprime | Python3 program for nth FussCatalan Number ; 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
from math import gcd , sqrt NEW_LINE def isComposite ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT def Power ( x , y , p ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % p ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def isEulerPseudoprime ( N , A ) : NEW_LINE INDENT if ( A <= 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( N % 2 == 0 or not isComposite ( N ) ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( gcd ( A , N ) != 1 ) : NEW_LINE INDENT return false ; NEW_LINE DEDENT mod = Power ( A , ( N - 1 ) // 2 , N ) ; NEW_LINE if ( mod != 1 and mod != N - 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 121 ; A = 3 ; NEW_LINE if ( isEulerPseudoprime ( N , A ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT