在本文中,我们将讨论Lucas-Lehmer级数,该级数用于检查2 p – 1形式的质数的素数,其中p是整数。
首先,让我们看看什么是Lucas-Lehmer系列。
Lucas-Lehmer级数可以表示为:
因此,该系列为:
期限0:4
期限1:4 * 4 – 2 = 14
期限2:14 * 14 – 2 = 194,
期限3:194 * 194 – 2 = 37634,
第四学期:37634 * 37634 – 2 = 1416317954,等等。
以下是找出Lucas-Lehmer系列的前n个项的程序。
C++
// C++ program to find out Lucas-Lehmer series.
#include
#include
using namespace std;
// Function to find out first n terms
// (considering 4 as 0th term) of
// Lucas-Lehmer series.
void LucasLehmer(int n) {
// the 0th term of the series is 4.
unsigned long long current_val = 4;
// create an array to store the terms.
vector series;
// compute each term and add it to the array.
series.push_back(current_val);
for (int i = 0; i < n; i++) {
current_val = current_val * current_val - 2;
series.push_back(current_val);
}
// print out the terms one by one.
for (int i = 0; i <= n; i++)
cout << "Term " << i << ": "
<< series[i] << endl;
}
// Driver program
int main() {
int n = 5;
LucasLehmer(n);
return 0;
}
Java
// Java program to find out
// Lucas-Lehmer series.
import java.util.*;
class GFG
{
// Function to find out
// first n terms(considering
// 4 as 0th term) of Lucas-
// Lehmer series.
static void LucasLehmer(int n)
{
// the 0th term of
// the series is 4.
long current_val = 4;
// create an array
// to store the terms.
ArrayList series = new ArrayList<>();
// compute each term
// and add it to the array.
series.add(current_val);
for (int i = 0; i < n; i++)
{
current_val = current_val
* current_val - 2;
series.add(current_val);
}
// print out the
// terms one by one.
for (int i = 0; i <= n; i++)
{
System.out.println("Term " + i
+ ": " + series.get(i));
}
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
LucasLehmer(n);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to find out Lucas-Lehmer series.
# Function to find out first n terms
# (considering 4 as 0th term) of
# Lucas-Lehmer series.
def LucasLehmer(n):
# the 0th term of the series is 4.
current_val = 4;
# create an array to store the terms.
series = []
# compute each term and add it to the array.
series.append(current_val)
for i in range(n):
current_val = current_val * current_val - 2;
series.append(current_val);
# print out the terms one by one.
for i in range(n + 1):
print("Term", i, ":", series[i])
# Driver program
if __name__=='__main__':
n = 5;
LucasLehmer(n);
# This code is contributed by pratham76.
C#
// C# program to find out
// Lucas-Lehmer series.
using System;
using System.Collections.Generic;
class GFG
{
// Function to find out
// first n terms(considering
// 4 as 0th term) of Lucas-
// Lehmer series.
static void LucasLehmer(int n)
{
// the 0th term of
// the series is 4.
long current_val = 4;
// create an array
// to store the terms.
List series = new List();
// compute each term
// and add it to the array.
series.Add(current_val);
for (int i = 0; i < n; i++)
{
current_val = current_val *
current_val - 2;
series.Add(current_val);
}
// print out the
// terms one by one.
for (int i = 0; i <= n; i++)
Console.WriteLine("Term " + i +
": " + series[i]);
}
// Driver Code
static void Main()
{
int n = 5;
LucasLehmer(n);
}
}
// This code is contributed by
// ManishShaw(manishshaw1)
C++
// CPP program to check for primality using
// Lucas-Lehmer series.
#include
#include
using namespace std;
// Function to check whether (2^p - 1)
// is prime or not.
bool isPrime(int p) {
// generate the number
long long checkNumber = pow(2, p) - 1;
// First number of the series
long long nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
int main() {
// Check whether 2^p-1 is prime or not.
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
// Java program to check for primality using
// Lucas-Lehmer series.
class GFG{
// Function to check whether (2^p - 1)
// is prime or not.
static boolean isPrime(int p) {
// generate the number
double checkNumber = Math.pow(2, p) - 1;
// First number of the series
double nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
public static void main(String[] args) {
// Check whether 2^p-1 is prime or not.
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.");
}
}
// This code is contributed by mits
Python3
# Python3 Program to check for primality
# using Lucas-Lehmer series.
# Function to check whether (2^p - 1)
# is prime or not.
def isPrime(p):
# generate the number
checkNumber = 2 ** p - 1
# First number of the series
nextval = 4 % checkNumber
# Generate the rest (p-2) terms
# of the series
for i in range(1, p - 1):
nextval = (nextval * nextval - 2) % checkNumber
# now if the (p-1) the term is
# 0 return true else false.
if (nextval == 0): return True
else: return False
# Driver Code
# Check whetherr 2^(p-1)
# is prime or not.
p = 7
checkNumber = 2 ** p - 1
if isPrime(p):
print(checkNumber, 'is Prime.')
else:
print(checkNumber, 'is not Prime')
# This code is contributed by egoista.
C#
// C# program to check for primality using
// Lucas-Lehmer series.
using System;
class GFG{
// Function to check whether (2^p - 1)
// is prime or not.
static bool isPrime(int p) {
// generate the number
double checkNumber = Math.Pow(2, p) - 1;
// First number of the series
double nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
static void Main() {
// Check whether 2^p-1 is prime or not.
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.");
}
}
// This code is contributed by mits
PHP
输出:
Term 0: 4
Term 1: 14
Term 2: 194
Term 3: 37634
Term 4: 1416317954
Term 5: 2005956546822746114
我们可以使用字符串来存储系列的大量数字。
现在,这个卢卡斯-莱默级数与素数的关系是什么?
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.
例子:
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.
检查2 ^ p-1是否为质数的代码
C++
// CPP program to check for primality using
// Lucas-Lehmer series.
#include
#include
using namespace std;
// Function to check whether (2^p - 1)
// is prime or not.
bool isPrime(int p) {
// generate the number
long long checkNumber = pow(2, p) - 1;
// First number of the series
long long nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
int main() {
// Check whether 2^p-1 is prime or not.
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
// Java program to check for primality using
// Lucas-Lehmer series.
class GFG{
// Function to check whether (2^p - 1)
// is prime or not.
static boolean isPrime(int p) {
// generate the number
double checkNumber = Math.pow(2, p) - 1;
// First number of the series
double nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
public static void main(String[] args) {
// Check whether 2^p-1 is prime or not.
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.");
}
}
// This code is contributed by mits
Python3
# Python3 Program to check for primality
# using Lucas-Lehmer series.
# Function to check whether (2^p - 1)
# is prime or not.
def isPrime(p):
# generate the number
checkNumber = 2 ** p - 1
# First number of the series
nextval = 4 % checkNumber
# Generate the rest (p-2) terms
# of the series
for i in range(1, p - 1):
nextval = (nextval * nextval - 2) % checkNumber
# now if the (p-1) the term is
# 0 return true else false.
if (nextval == 0): return True
else: return False
# Driver Code
# Check whetherr 2^(p-1)
# is prime or not.
p = 7
checkNumber = 2 ** p - 1
if isPrime(p):
print(checkNumber, 'is Prime.')
else:
print(checkNumber, 'is not Prime')
# This code is contributed by egoista.
C#
// C# program to check for primality using
// Lucas-Lehmer series.
using System;
class GFG{
// Function to check whether (2^p - 1)
// is prime or not.
static bool isPrime(int p) {
// generate the number
double checkNumber = Math.Pow(2, p) - 1;
// First number of the series
double nextval = 4 % checkNumber;
// Generate the rest (p-2) terms
// of the series.
for (int i = 1; i < p - 1; i++)
nextval = (nextval * nextval - 2) % checkNumber;
// now if the (p-1)th term is
// 0 return true else false.
return (nextval == 0);
}
// Driver Program
static void Main() {
// Check whether 2^p-1 is prime or not.
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.");
}
}
// This code is contributed by mits
的PHP
输出:
127 is Prime.
在撰写本文时,最大质数为(2 ^(77232917)– 1)(发现于2017-12-26)。它具有23、249、425个数字。以与上述相同的方式找到该质数。为了找到这种大质数,需要巨大的计算能力和几个月的处理时间。
有趣的事实是,为了检查这么大的素数,p也被当作素数。在处理之后,如果发现数字x不是质数,则将p用作下一个质数,并运行相同的过程。