我创建了一个以下程序来制作php中的fibanocci系列
function find_max_paths_fib($spaces) {
$c = array();
$c[0] = 1;
$c[1] = 1;
for ($i = 2; $i <= $spaces; $i++) {
if ($i >= 2) {
echo $c[$i] = $c[$i-2] + $c[$i-1];
}
}
return $c[$spaces];
}
Run Code Online (Sandbox Code Playgroud)
$ spaces表示我需要多少生成系列的数字,但是find_max_paths_fib(8000)或者对于一些大数字返回INF,我已经在c ++中尝试过并得到了相同的结果.有没有办法计算它?还是我的功能错了?
小智 9
必须使用BC Math Functions或GNU Multiple Precision来处理大量数字
安装此模块使用终端中的flow命令:
sudo apt-get install php7.0-bcmath // set your php version
#or sudo apt-get install php7.2-bcmath
#or sudo apt-get install php7.1-bcmath
#or sudo apt-get install php-bcmath
Run Code Online (Sandbox Code Playgroud)
或者如果使用GNU Multiple Precision:
sudo apt-get install php70-gmp// set your php version
#or sudo apt-get install php7.2-gmp
#or sudo apt-get install php7.1-gmp
#or sudo apt-get install php-gmp
Run Code Online (Sandbox Code Playgroud)
如果使用microsoft windows:link
安装完成后重启apache
使用BCMath:
$sum = bcadd('1234567812345678', '8765432187654321');
// $sum is now the string '9999999999999999'
print $sum;
Run Code Online (Sandbox Code Playgroud)
使用GMP:
$sum = gmp_add('1234567812345678', '8765432187654321');
// $sum is now a GMP resource, not a string; use gmp_strval( ) to convert
print gmp_strval($sum);
Run Code Online (Sandbox Code Playgroud)
最终代码:
function find_max_paths_fib2($spaces) {
$c = array();
$c[1] = 1;
$c[2] = 1;
for ($i = 3; $i <= $spaces; $i++) {
if ($i >= 3) {
echo $c[$i] = bcadd($c[$i-2] , $c[$i-1]);
}
}
return $c[$spaces];
}
Run Code Online (Sandbox Code Playgroud)