Tutorialspoint.dev

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.

<?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;
?>

Output:

2

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

<?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;
?>

Output:

0

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

<?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;
?>

Output:

1

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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter