PHP | gmp_prob_prime() Function

The gmp_prob_prime() is a built-in function in PHP which is used to check how much is the possibility of given GMP number(GNU Multiple Precision : For large numbers) to be prime or not.
This function uses Miller-Rabin primality test to check if the given GMP number is prime or not.

Syntax:

gmp_prob_prime($num)

Parameters: The function accepts a GMP number $num as mandatory parameter as shown in the above syntax. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass numeric string such that it is possible to convert this string in number.

Return Value: This function returns value in the range 0-2, 0 if the number is definitely not prime, 1 if the number may be prime else 2 if the number is surely prime.

Examples:



Input : gmp_prob_prime("8")
Output : 0

Input : gmp_prob_prime("11111111111111")
Output : 1

Input: gmp_prob_prime("127")
Output:  2

Below programs illustrate the gmp_prob_prime() function in PHP:

Program 1: Program to find the prime probability of GMP number when numeric strings as GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the prime probability of
// GMP numbers passed as arguments 
  
// strings as GMP numbers
$num = "17";
  
// calculate the possibility
// of GMP number to be prime
$prob = gmp_prob_prime($num);
   
echo $prob;
?>

chevron_right


Output:

2

Program 2: Program to find the prime probability of GMP number when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the prime probability of
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num = gmp_init(8);
  
// calculate the possibility of
// GMP number to be prime
$prob = gmp_prob_prime($num);
  
echo $prob;
?>

chevron_right


Output:

0

Program 3: Program to find the prime probability of GMP number when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the prime probability of
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num = gmp_init(1111111111111111111);
  
// calculate the possibility of
// GMP number to be prime
$prob = gmp_prob_prime($num);
  
echo $prob;
?>

chevron_right


Output:

1

Reference:
php.net/manual/en/function.gmp-prob-prime.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.