Check if a number is Fermat Pseudoprime
Last Updated :
07 Jun, 2022
Given a number N and a base number A. The task is to check whether the number is a Fermat Pseudoprime to the base.
The number N is called as Fermat Pseudoprime to the base A, if
1. A > 1
2. N is a composite number
3. N divides AN-1 – 1.
Examples:
Input : N = 645, a = 2
Output :1
645 = 3*5*43, Hence it is a composite number
Also 645 divides 2^(644)-1
Hence it is a Fermat Pseudoprime.
Input : N = 6, a = 2
Output :0
Approach: The approach is to check the below conditions:
If all of the above conditions satisfy then N is a fermat pseudoprime to base A.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkcomposite( int n)
{
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0)
return 1;
}
return 0;
}
int power( int x, int y, int mod)
{
int res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
bool Check( int n, int a)
{
if (a>1 && checkcomposite(n) && power(a, n - 1, n) == 1)
return 1;
return 0;
}
int main()
{
int N = 645;
int a = 2;
cout << Check(N, a);
return 0;
}
|
Java
class GFG
{
static boolean checkcomposite( 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 mod)
{
int res = 1 ;
while (y != 0 )
{
if ((y & 1 ) == 1 )
{
res = (res * x) % mod;
}
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
static int Check( int n, int a)
{
if (a > 1 && checkcomposite(n)
&& power(a, n - 1 , n) == 1 )
{
return 1 ;
}
return 0 ;
}
public static void main(String[] args)
{
int N = 645 ;
int a = 2 ;
System.out.println(Check(N, a));
}
}
|
Python3
from math import sqrt
def checkcomposite(n):
for i in range ( 2 , int (sqrt(n)) + 1 , 1 ):
if (n % i = = 0 ):
return 1
return 0
def power(x, y, mod):
res = 1
while (y):
if (y & 1 ):
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
def Check(n,a):
if (a> 1 and checkcomposite(n) and power(a, n - 1 , n) = = 1 ):
return 1
return 0
if __name__ = = '__main__' :
N = 645
a = 2
print (Check(N, a))
|
C#
using System;
class GFG
{
static bool checkcomposite( 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 mod)
{
int res = 1;
while (y != 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int Check( int n, int a)
{
if (a > 1 && checkcomposite(n) &&
power(a, n - 1, n) == 1)
return 1;
return 0;
}
static public void Main ()
{
int N = 645;
int a = 2;
Console.WriteLine(Check(N, a));
}
}
|
Javascript
<script>
function checkcomposite(n)
{
for (let i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
return 1;
}
return 0;
}
function power(x, y, mod)
{
let res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function Check(n, a)
{
if (a>1 && checkcomposite(n) &&
power(a, n - 1, n) == 1)
return 1;
return 0;
}
let N = 645;
let a = 2;
document.write(Check(N, a));
</script>
|
Time Complexity : O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...