PHP | gmp_prob_prime()函数

gmp_prob_prime()是PHP中的一个内置函数,用于检查给定gmp编号的可能性有多大( GNU多重精度:适用于大数字 )无论是不是最好的。 此函数使用 米勒-拉宾素性检验 检查给定的GMP编号是否为素数。

null

语法:

gmp_prob_prime($num)

参数: 该函数接受GMP编号 $num 作为强制参数,如上述语法所示。这个参数可以是PHP 5.6版及更高版本中的GMP对象,也可以传递数字字符串,这样就可以将这个字符串转换为数字。

返回值: 此函数返回0-2范围内的值, 0 如果这个数绝对不是素数, 1. 如果这个数可能是素数,那么是素数还是素数 2. 如果这个数肯定是素数。

例如:

Input : gmp_prob_prime("8")
Output : 0

Input : gmp_prob_prime("11111111111111")
Output : 1

Input: gmp_prob_prime("127")
Output:  2

下面的程序演示了PHP中的gmp_prob_prime()函数:

项目1: 当作为GMP数的数字字符串作为参数传递时,该程序用于查找GMP数的素数概率。

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


输出:

2

项目2: 当GMP数作为参数传递时,求GMP数的素数概率的程序。

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


输出:

0

方案3: 当GMP数作为参数传递时,求GMP数的素数概率的程序。

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


输出:

1

参考: php。net/manual/en/function。gmp prob prime。php

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享