
PHP gmp_prob_prime() function
As of my last update in September 2021, the gmp_prob_prime()
function is available in PHP’s GMP (GNU Multiple Precision) extension. The gmp_prob_prime()
function is used to check whether a given GMP number is probably prime or not.
Here’s the syntax of the gmp_prob_prime()
function:
int gmp_prob_prime ( GMP $num [, int $reps = 10] )
Parameters:
$num
: The GMP number to be tested for primality.$reps
: (Optional) The number of iterations used in the Miller-Rabin probabilistic primality test. The default value is 10.
Return value:
- The
gmp_prob_prime()
function returns an integer that indicates the probability that$num
is prime. The possible return values are: 0
:$num
is definitely not prime.1
:$num
is probably prime.2
:$num
is definitely prime.
Example:
$num = gmp_init('31');
$probability = gmp_prob_prime($num);
if ($probability == 2) {
echo "31 is definitely prime.";
} elseif ($probability == 1) {
echo "31 is probably prime.";
} else {
echo "31 is not prime.";
}
In this example, the gmp_prob_prime()
function is used to check whether the GMP number 31
is prime. The function returns 2
, indicating that 31
is definitely prime.
The gmp_prob_prime()
function uses the Miller-Rabin primality test, which is a probabilistic algorithm. It performs several iterations (controlled by the $reps
parameter) to test the primality of the number. The higher the value of $reps
, the more iterations are performed, resulting in a more accurate primality test.
For cryptographic applications or when absolute certainty is required, you should consider using other primality testing algorithms like the deterministic AKS primality test or strong pseudoprime tests.
Please note that the GMP extension needs to be enabled in your PHP configuration to use this function. Additionally, the availability of functions may change in newer PHP versions, so always refer to the PHP manual for the specific version you are using.