Lehmann’s Primality Test
Last Updated :
13 Oct, 2022
An integer p greater than one is prime if the only divisors of p are 1 and p. First few prime numbers are 2, 3, 5, 7, 11, 13, …
The Lehmann’s test is a probabilistic primality test for an natural number n, it can test the primality of any kind of number(whether a large odd number is prime or not). The Lehmann’s test is a variation of Fermat’s Primality Test.
The approach used is as follows:
If ‘n’ is an odd number and ‘a’ is a random integer less than n but greater than 1, then
x = (a^((n-1)/2)) (mod n)
It is computed.
- If x is 1 or -1(or n-1), then n may be prime.
- If x is not 1 or -1(or n-1), then n is definitely composite.
The fact that any composite number can be turned out to be a prime, in this case, depends on the random value ‘a’. If all the values of a and n are co-prime, then n can be said as a prime number.
Example-1:
Input: n = 13
Output: 13 is Prime
Explanation:
Let a = 3, then,
3^((13-1)/2) % 13 = 729 % 13 = 1
Hence, 13 is Prime.
Example-2:
Input: n = 91
Output: 91 is Composite
Explanation:
Let a = 3, then,
3^((91-1)/2) % 91 = 27
Hence, 91 is Composite.
C++
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
#include<bits/stdc++.h>
using namespace std;
int lehmann( int n, int t)
{
int a = 2 + ( rand () % (n - 1));
int e = (n - 1) / 2;
while (t > 0)
{
int result =(( int )( pow (a, e)))% n;
if ((result % n) == 1 || (result % n) == (n - 1))
{
a = 2 + ( rand () % (n - 1));
t -= 1;
}
else
return -1;
}
return 1;
}
int main()
{
int n = 13 ;
int t = 10 ;
if (n == 2)
cout << "2 is Prime." ;
if (n % 2 == 0)
cout << n << " is Composite" ;
else
{
int flag = lehmann(n, t);
if (flag ==1)
cout << n << " may be Prime." ;
else
cout << n << " is Composite." ;
}
}
|
Java
import java.util.Random;
class GFG
{
static int lehmann( int n, int t)
{
Random rand = new Random();
int a = rand.nextInt(n - 3 ) + 2 ;
float e = (n - 1 ) / 2 ;
while (t > 0 )
{
int result = (( int )(Math.pow(a, e))) % n;
if ((result % n) == 1 || (result % n) == (n - 1 ))
{
a = rand.nextInt(n - 3 ) + 2 ;
t -= 1 ;
}
else
return - 1 ;
}
return 1 ;
}
public static void main (String[] args)
{
int n = 13 ;
int t = 10 ;
if (n == 2 )
System.out.println( " 2 is Prime." );
if (n % 2 == 0 )
System.out.println(n + " is Composite" );
else
{
long flag = lehmann(n, t);
if (flag == 1 )
System.out.println(n + " may be Prime." );
else
System.out.println(n + " is Composite." );
}
}
}
|
Python3
import random
def lehmann(n, t):
a = random.randint( 2 , n - 1 )
e = (n - 1 ) / 2
while (t> 0 ):
result = (( int )(a * * e)) % n
if ((result % n) = = 1 or (result % n) = = (n - 1 )):
a = random.randint( 2 , n - 1 )
t - = 1
else :
return - 1
return 1
n = 13
t = 10
if (n is 2 ):
print ( "2 is Prime." )
if (n % 2 = = 0 ):
print (n, "is Composite" )
else :
flag = lehmann(n, t)
if (flag is 1 ):
print (n, "may be Prime." )
else :
print (n, "is Composite." )
|
C#
using System;
class GFG
{
static int lehmann( int n, int t)
{
Random rand = new Random();
int a = rand.Next(n - 3) + 2;
float e = (n - 1) / 2;
while (t > 0)
{
int result = (( int )(Math.Pow(a, e))) % n;
if ((result % n) == 1 ||
(result % n) == (n - 1))
{
a = rand.Next(n - 3) + 2;
t -= 1;
}
else
return -1;
}
return 1;
}
public static void Main (String[] args)
{
int n = 13;
int t = 10;
if (n == 2)
Console.WriteLine( " 2 is Prime." );
if (n % 2 == 0)
Console.WriteLine(n + " is Composite" );
else
{
long flag = lehmann(n, t);
if (flag == 1)
Console.WriteLine(n + " may be Prime." );
else
Console.WriteLine(n + " is Composite." );
}
}
}
|
Share your thoughts in the comments
Please Login to comment...