Check if a number is Euler Pseudoprime | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isComposite ( int n ) { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return true ; } return false ; } int Power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } bool isEulerPseudoprime ( int N , int A ) { if ( A <= 0 ) return false ; if ( N % 2 == 0 || ! isComposite ( N ) ) return false ; if ( __gcd ( A , N ) != 1 ) return false ; int mod = Power ( A , ( N - 1 ) / 2 , N ) ; if ( mod != 1 && mod != N - 1 ) return false ; return true ; } int main ( ) { int N = 121 , A = 3 ; if ( isEulerPseudoprime ( N , A ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }