Primality Test using Lucas-Lehmer Series
Last Updated :
11 Jan, 2023
In this article, we will discuss the Lucas-Lehmer series which is used to check the primality of prime numbers of the form 2p – 1 where p is an integer.
First, let’s see what is Lucas-Lehmer series. The Lucas-Lehmer series can be expressed as :
Hence the series is:
Term 0: 4,
Term 1: 4*4 - 2 = 14,
Term 2: 14*14 - 2 = 194,
Term 3: 194*194 - 2 = 37634,
Term 4: 37634*37634 - 2 = 1416317954, ... and so on.
Below is the program to find out first n terms of the Lucas-Lehmer series.
C++
#include <iostream>
#include <vector>
using namespace std;
void LucasLehmer( int n) {
unsigned long long current_val = 4;
vector<unsigned long long > series;
series.push_back(current_val);
for ( int i = 0; i < n; i++) {
current_val = current_val * current_val - 2;
series.push_back(current_val);
}
for ( int i = 0; i <= n; i++)
cout << "Term " << i << ": "
<< series[i] << endl;
}
int main() {
int n = 5;
LucasLehmer(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void LucasLehmer( int n)
{
long current_val = 4 ;
ArrayList<Long> series = new ArrayList<>();
series.add(current_val);
for ( int i = 0 ; i < n; i++)
{
current_val = current_val
* current_val - 2 ;
series.add(current_val);
}
for ( int i = 0 ; i <= n; i++)
{
System.out.println( "Term " + i
+ ": " + series.get(i));
}
}
public static void main(String[] args)
{
int n = 5 ;
LucasLehmer(n);
}
}
|
Python3
def LucasLehmer(n):
current_val = 4 ;
series = []
series.append(current_val)
for i in range (n):
current_val = current_val * current_val - 2 ;
series.append(current_val);
for i in range (n + 1 ):
print ( "Term" , i, ":" , series[i])
if __name__ = = '__main__' :
n = 5 ;
LucasLehmer(n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void LucasLehmer( int n)
{
long current_val = 4;
List< long > series = new List< long >();
series.Add(current_val);
for ( int i = 0; i < n; i++)
{
current_val = current_val *
current_val - 2;
series.Add(current_val);
}
for ( int i = 0; i <= n; i++)
Console.WriteLine( "Term " + i +
": " + series[i]);
}
static void Main()
{
int n = 5;
LucasLehmer(n);
}
}
|
Javascript
<script>
function LucasLehmer(n)
{
let current_val = 4;
let series = [];
series.push(current_val);
for (let i = 0; i < n; i++)
{
current_val = (current_val
* current_val) - 2;
series.push(current_val);
}
for (let i = 0; i <= n; i++)
{
document.write( "Term " + i
+ ": " + series[i]+ "<br>" );
}
}
let n = 5;
LucasLehmer(n);
</script>
|
OutputTerm 0: 4
Term 1: 14
Term 2: 194
Term 3: 37634
Term 4: 1416317954
Term 5: 2005956546822746114
Time Complexity: O(n)
Auxiliary Space: O(n)
We can use string to store the big numbers of the series.
Now, what is the relation with prime numbers of this Lucas-Lehmer series?
1. First thing is that we can only check the primality of those numbers which we can represent as, x = (2p – 1) where p is an integer.
2. Now we have to find out the (p-1)th term of Lucas-Lehmer series.
3. If this term is a multiple of x, then x is a prime number.
4. When x is large, i.e. p is large then we may find difficulties to find out the (p-1)th term of the series.
Rather what we can do:
1. Start calculating Lucas-Lehmer series from 0th term and rather storing the whole term only store the s[i]%x (i.e. term modulo x).
2. Compute the next number of this modified series using the previous term. s[i] = (s[i-1]2 – 2)%x.
3. Compute up to (p-1)th term.
4. If the (p-1)th term is 0 then x is prime, otherwise not. Hence, s[p-1] has to be 0 to be x = (2p – 1) prime.
Examples:
Is 2^7 - 1 = 127 is a prime?
so here x = 127, p = 7-1 = 6.
Hence the modified Lucas-Lehmer series is:
term 1: 4,
term 2: (4*4 - 2) % 127 = 14,
term 3: (14*14 - 2) % 127 = 67,
term 4: (67*67 - 2) % 127 = 42,
term 5: (42*42 - 2) % 127 = 111,
term 6: (111*111) % 127 = 0.
Here the 6th term is 0 so 127 is a prime number.
Code to check whether 2^p-1 is prime or not
C++
#include <cmath>
#include <iostream>
using namespace std;
bool isPrime( int p) {
long long checkNumber = pow (2, p) - 1;
long long nextval = 4 % checkNumber;
for ( int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
return (nextval == 0);
}
int main() {
int p = 7;
long long checkNumber = pow (2, p) - 1;
if (isPrime(p))
cout << checkNumber << " is Prime." ;
else
cout << checkNumber << " is not Prime." ;
return 0;
}
|
Java
class GFG{
static boolean isPrime( int p) {
double checkNumber = Math.pow( 2 , p) - 1 ;
double nextval = 4 % checkNumber;
for ( int i = 1 ; i < p - 1 ; i++)
nextval = (nextval * nextval - 2 ) % checkNumber;
return (nextval == 0 );
}
public static void main(String[] args) {
int p = 7 ;
double checkNumber = Math.pow( 2 , p) - 1 ;
if (isPrime(p))
System.out.println(( int )checkNumber+ " is Prime." );
else
System.out.println(( int )checkNumber+ " is not Prime." );
}
}
|
Python3
def isPrime(p):
checkNumber = 2 * * p - 1
nextval = 4 % checkNumber
for i in range ( 1 , p - 1 ):
nextval = (nextval * nextval - 2 ) % checkNumber
if (nextval = = 0 ): return True
else : return False
p = 7
checkNumber = 2 * * p - 1
if isPrime(p):
print (checkNumber, 'is Prime.' )
else :
print (checkNumber, 'is not Prime' )
|
C#
using System;
class GFG{
static bool isPrime( int p) {
double checkNumber = Math.Pow(2, p) - 1;
double nextval = 4 % checkNumber;
for ( int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
return (nextval == 0);
}
static void Main() {
int p = 7;
double checkNumber = Math.Pow(2, p) - 1;
if (isPrime(p))
Console.WriteLine(( int )checkNumber+ " is Prime." );
else
Console.WriteLine(( int )checkNumber+ " is not Prime." );
}
}
|
PHP
<?php
function isPrime( $p )
{
$checkNumber = pow(2, $p ) - 1;
$nextval = 4 % $checkNumber ;
for ( $i = 1; $i < $p - 1; $i ++)
$nextval = ( $nextval * $nextval - 2) %
$checkNumber ;
return ( $nextval == 0);
}
$p = 7;
$checkNumber = pow(2, $p ) - 1;
if (isPrime( $p ))
echo $checkNumber , " is Prime." ;
else
echo $checkNumber , " is not Prime." ;
?>
|
Javascript
<script>
function isPrime(p) {
let checkNumber = Math.pow(2, p) - 1;
let nextval = 4 % checkNumber;
for (let i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
return (nextval == 0);
}
let p = 7;
let checkNumber = Math.pow(2, p) - 1;
if (isPrime(p))
document.write(checkNumber+ " is Prime." );
else
document.write(checkNumber+ " is not Prime." );
</script>
|
The largest prime number at the time of writing this article is (2^(77232917) – 1) (discovered 2017-12-26). It has 23, 249, 425 digits. These prime numbers are found in the same way discussed above. Huge computational power and several months of processing are required to find out this kind of large prime number.
Time Complexity: O(p)
Auxiliary Space: O(1)
An interesting fact is that for checking this many big prime numbers, p is also taken prime. After processing, if it finds that the number x is not prime then p is taken as the next prime number and the same process is run.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...