What is the gmp_prob_prime function in PHP?

gmp_prob_prime is a function in PHP that checks whether or not the argument passed is probably prime.


This function uses Miller-Rabin’s probabilistic test to check the prime nature of numbers.

Syntax


gmp_prob_prime(GMP|int|string $num, int $repetitions = 10): int

Parameters

The function takes in two parameters:

  • num - the number that is going to be checked for prime number.

  • repetitions - this is an optional parameter. The value of repetition varies from 5 to 10. The default value is 10.


num and repetitions can be a numeric string, GMP object, or of the int type.

Return value

  • The function returns 0 if the function is not prime.

  • The function returns 1 if the function is prime.

  • The function returns 2 if the function is prime.

Code

<?php
// number is not prime
echo gmp_prob_prime("2") . "\n";
// number is probably prime
echo gmp_prob_prime("111111111") . "\n";
// number is surely prime
echo gmp_prob_prime("13") . "\n";
?>

Output


0
1
2

Free Resources