Home >Backend Development >PHP Tutorial >How to use PHP and GMP to perform Lucas-Lehmer primality test of large integers

How to use PHP and GMP to perform Lucas-Lehmer primality test of large integers

PHPz
PHPzOriginal
2023-07-28 16:05:201118browse

How to use PHP and GMP to perform the Lucas-Lehmer primality test of large integers

Introduction:
In number theory, the Lucas-Lehmer primality test is a method used to test the Mernessen number (Mersenne number) The method of determining whether a number is prime is widely used in the judgment of large integers. In this article, we will use the PHP language and GMP extension (GNU Multiple Precision Arithmetic Library, GNU Multiple Precision Math Library) to implement the Lucas-Lehmer primality test and provide corresponding code examples.

What is the Lucas-Lehmer Sexuality Test?
The Lucas-Lehmer primality test is an efficient algorithm used to determine whether a Monessen number of the form M = 2^n − 1 is a prime number. Among them, n is a positive integer greater than 1. This testing method is based on the properties of the Lucas-Lehmer sequence. It determines the primality of the Monessen number by iteratively calculating the next element of the sequence, and finally judging whether the last element of the sequence is zero.

Steps to perform Lucas-Lehmer primality test using PHP and GMP:
Step 1: Install GMP extension
When performing large integer operations, PHP's built-in functions cannot handle larger values. So, we need to use GMP extensions to solve this problem. When installing PHP, you can choose to install a version with GMP extensions enabled, or enable GMP extensions in an existing PHP environment.

Step 2: Write the Lucas-Lehmer primality test function
The following is an example of a function used to perform the Lucas-Lehmer primality test:

function lucasLehmerTest($n)
{
    $s = '4';
    $m = gmp_pow('2', $n) - '1';
    
    for ($i = 1; $i < $n - 1; $i++) {
        $s = gmp_mod(gmp_pow($s, 2) - 2, $m);
    }
    
    if ($s == '0') {
        return true;
    }
    
    return false;
}

Analysis:

  • $n: The exponent part of the Mounissen number.
  • $s: Initial value of Lucas-Lehmer sequence.
  • $m: Monessen number.

In the function, we use the gmp_pow function to calculate 2 to the power of $n$, and then subtract 1 to get $m$. Then, we perform $n-1$ loop iterations to calculate each element of the Lucas-Lehmer sequence. Finally, determine whether the last element of the sequence is zero, thereby determining the primality of the Monessen number.

Step 3: Call the Lucas-Lehmer primality test function for testing
The following is an example of calling the Lucas-Lehmer primality test function:

$exponents = [2, 3, 5, 7, 13, 17];
foreach ($exponents as $exponent) {
    $result = lucasLehmerTest($exponent);
    
    if ($result) {
        echo "2^$exponent - 1 is a prime number.
";
    } else {
        echo "2^$exponent - 1 is not a prime number.
";
    }
}

Analysis:
We define an array $exponents, contains some exponent values. Then use a foreach loop to call the Lucas-Lehmer primality test function in sequence, and output the corresponding judgment information based on the test results.

Summary:
By using PHP and GMP extensions, we can easily implement the Lucas-Lehmer primality test and determine whether a large integer is prime. For large-scale primality tests, the Lucas-Lehmer algorithm is very efficient and can quickly determine the primality of Mönisen numbers. This article provides corresponding code examples, hoping to help readers conduct large integer primality testing in practice.

Reference:

  • "Lucas–Lehmer primality test." Wikipedia, The Free Encyclopedia. URL: https://en.wikipedia.org/wiki/Lucas–Lehmer_primality_test
  • "GMP Manual." PHP. URL: https://www.php.net/manual/en/book.gmp.php

The above is about how to use PHP and GMP An article on performing the Lucas-Lehmer primality test of large integers, and corresponding code examples.

The above is the detailed content of How to use PHP and GMP to perform Lucas-Lehmer primality test of large integers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn