Check if a number is Euler Pseudoprime
Given an integer N and a base number A, the task is to check whether N is a Euler Pseudoprime to the given base A.
An integer N is called Euler Pseudoprime to the base A, if
- A > 0 and N is an odd composite number.
- A and N are co-prime i.e. GCD(A, N) = 1.
- A(N – 1) / 2 % N is either 1 or N – 1.
Examples:
Input: N = 121, A = 3
Output: Yes
Input: N = 343, A = 2
Output: No
Approach: Check all the given conditions for Euler Pseudoprime. If any one of the conditions is not true then print No else print Yes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
y = y >> 1;
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;
}
|
Java
class GFG
{
static boolean isComposite( int n)
{
for ( int i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
return true ;
}
return false ;
}
static int Power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
{
res = (res * x) % p;
}
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static boolean 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 ;
}
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
public static void main(String []args)
{
int N = 121 , A = 3 ;
if (isEulerPseudoprime(N, A))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import gcd, sqrt
def isComposite(n) :
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
return True ;
return False ;
def Power(x, y, p) :
res = 1 ;
x = x % p;
while (y > 0 ) :
if (y & 1 ) :
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
return res;
def isEulerPseudoprime(N, A) :
if (A < = 0 ) :
return False ;
if (N % 2 = = 0 or not isComposite(N)) :
return False ;
if (gcd(A, N) ! = 1 ) :
return false;
mod = Power(A, (N - 1 ) / / 2 , N);
if (mod ! = 1 and mod ! = N - 1 ) :
return False ;
return True ;
if __name__ = = "__main__" :
N = 121 ; A = 3 ;
if (isEulerPseudoprime(N, A)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool isComposite( int n)
{
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
return true ;
}
return false ;
}
static int Power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static 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 ;
}
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
public static void Main(String []args)
{
int N = 121, A = 3;
if (isEulerPseudoprime(N, A))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
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;
}
y = y >> 1;
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( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...