fibonacci.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 490 行 · 第 1/2 页
PHP
490 行
<?php//// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2002 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/2_02.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |// +----------------------------------------------------------------------+//// $Id: Fibonacci.php,v 1.1 2003/01/02 01:56:59 jmcastagnetto Exp $//include_once 'PEAR.php';include_once 'Math/IntegerOp.php';include_once 'Math/Fibonacci/_fibonacciTable.php';// PHI and phi are the roots of: x^2 - x - 1 = 0// PHI is called the golden ratio. Also phi = -1/PHI = 1 - PHI/** * MATH_PHI: The golden ratio = (1 + sqrt(5))/2 */define ('MATH_PHI', (1 + sqrt(5))/2);/** * MATH_PHI_x100000: (int) (MATH_PHI * 100000) */define ('MATH_PHI_x100000', intval(MATH_PHI * 100000));/** * MATH_phi: The reciprocal of PHI = (1 - sqrt(5))/2 */define ('MATH_phi', (1 - sqrt(5))/2);/** * MATH_phi_x100000: (int) (MATH_phi * 100000) */define ('MATH_phi_x100000', intval(MATH_phi * 100000));/** * MATH_SQRT5_x100000: (int) (sqrt(5) * 100000) */define ('MATH_SQRT5_x100000', intval(sqrt(5) * 100000));/** * MATH_LNSQRT5: ln(sqrt(5)) */define ('MATH_LNSQRT5', log(sqrt(5)));/** * MATH_LNPHI: ln(PHI) */define ('MATH_LNPHI', log(MATH_PHI));/** * Math_Fibonacci: class to calculate, validate, and decompose into Fibonacci numbers * * Examples: * <pre> * include_once 'Math/Fibonacci.php'; * * $idx = 20; * echo "Calculate F($idx), fast equation = "; * $fib =& Math_Fibonacci::term($idx); * echo $fib->toString()."\n"; * // Calculate F(20), fast equation = 6765 * * $idx = 55; * echo "Calculate F($idx), lookup table = "; * $fib =& Math_Fibonacci::term($idx); * echo $fib->toString()."\n"; * // Calculate F(55), lookup table = 139583862445 * * $idx = 502; * echo "Calculate F($idx), addition loop = "; * $fib = Math_Fibonacci::term($idx); * echo $fib->toString()."\n"; * // Calculate F(502), addition loop = 365014740723634211012237077906479355996081581501455497852747829366800199361550174096573645929019489792751 * * echo "\nSeries from F(0) to F(10):\n"; * $series = Math_Fibonacci::series(10); * foreach ($series as $n=>$fib) { * echo "n = $n, F(n) = ".$fib->toString()."\n"; * } * // Series from F(0) to F(10): * // n = 0, F(n) = 0 * // n = 1, F(n) = 1 * // n = 2, F(n) = 1 * // n = 3, F(n) = 2 * // n = 4, F(n) = 3 * // n = 5, F(n) = 5 * // n = 6, F(n) = 8 * // n = 7, F(n) = 13 * // n = 8, F(n) = 21 * // n = 9, F(n) = 34 * // n = 10, F(n) = 55 * * echo "\nand now from F(11) to F(19):\n"; * $series = Math_Fibonacci::series(11, 19); * foreach ($series as $n=>$fib) { * echo "n = $n, F(n) = ".$fib->toString()."\n"; * } * // and now from F(11) to F(19): * // n = 11, F(n) = 89 * // n = 12, F(n) = 144 * // n = 13, F(n) = 233 * // n = 14, F(n) = 377 * // n = 15, F(n) = 610 * // n = 16, F(n) = 987 * // n = 17, F(n) = 1597 * // n = 18, F(n) = 2584 * // n = 19, F(n) = 4181 * * echo "\nChecking if 26 and 4181 are Fibonacci numbers\n"; * $verb = Math_Fibonacci::isFibonacci(new Math_Integer(26)) ? 'is' : 'is not'; * echo "26 $verb a Fibonacci number\n"; * // 26 is not a Fibonacci number * $verb = Math_Fibonacci::isFibonacci(new Math_Integer(4181)) ? 'is' : 'is not'; * echo "4181 $verb a Fibonacci number\n"; * // 4181 is a Fibonacci number * * echo "\nDecompose 34512\n"; * $decarr = Math_Fibonacci::decompose(new Math_Integer(34512)); * foreach ($decarr as $fib) { * $index = Math_Fibonacci::getIndexOf($fib); * echo "F(".$index->toString().") = ".$fib->toString()."\n"; * } * // Decompose 34512 * // F(23) = 28657 * // F(19) = 4181 * // F(17) = 1597 * // F(10) = 55 * // F(8) = 21 * // F(2) = 1 * * echo "\nF(n) closest to 314156 is: "; * $fib = Math_Fibonacci::closestTo(new Math_Integer(314156)); * echo $fib->toString()."\n\n"; * // F(n) closest to 314156 is: 317811 * * echo 'The index for 1597 is : '; * $idx = Math_Fibonacci::getIndexOf(new Math_Integer(1597)); * echo $idx->toString()."\n\n"; * // The index for 1597 is : 17 * * $bigint = '3141579834521345220291'; * echo "Finding the Fibonacci numbers that add up to $bigint\n"; * $series = Math_Fibonacci::decompose(new Math_Integer($bigint)); * foreach ($series as $fib) { * $index = Math_Fibonacci::getIndexOf($fib); * echo "F(".$index->toString().") = ".$fib->toString()."\n"; * } * // Finding the Fibonacci numbers that add up to 3141579834521345220291 * // F(104) = 2427893228399975082453 * // F(101) = 573147844013817084101 * // F(98) = 135301852344706746049 * // F(91) = 4660046610375530309 * // F(86) = 420196140727489673 * // F(83) = 99194853094755497 * // F(81) = 37889062373143906 * // F(79) = 14472334024676221 * // F(76) = 3416454622906707 * // F(74) = 1304969544928657 * // F(71) = 308061521170129 * // F(68) = 72723460248141 * // F(63) = 6557470319842 * // F(60) = 1548008755920 * // F(57) = 365435296162 * // F(53) = 53316291173 * // F(51) = 20365011074 * // F(49) = 7778742049 * // F(44) = 701408733 * // F(37) = 24157817 * // F(31) = 1346269 * // F(26) = 121393 * // F(20) = 6765 * // F(16) = 987 * // F(13) = 233 * // F(8) = 21 * // F(6) = 8 * // F(3) = 2 * * </pre> * * @author Jesus M. Castagnetto <jmcastagnetto@php.net> * @version 0.8 * @access public * @package Math_Fibonacci */class Math_Fibonacci {/*{{{*/ /** * Calculates a Fibonacci number using the (exact) golden ratio equation: * F(n) = (PHI^n - phi^n)/sqrt(5) [Lucas formula] * for terms from [0,46] * from [47,500] it uses a lookup table * from then on uses the recursive addition * * @param mixed $n the index of the Fibonacci number to calculate, as an integer or a Math_Integer number * @return mixed numeric on success, PEAR_Error otherwise * @access public */ function &term($n) {/*{{{*/ if (Math_IntegerOp::isMath_Integer($n)) { $idx =& $n; } elseif (is_numeric($n) && $n >= 0) { $idx =& new Math_Integer($n); } else { return PEAR::raiseError("The parameter $n is not a Math_Integer object"); } $table =& $GLOBALS['_fibonacciTable']; // shortcut and check if it is already a cached value if (isset($table[$idx->toString()])) { return new Math_Integer($table[$idx->toString()]); } // from index [0,46] use the fast algorithm $cmp_0 = Math_IntegerOp::compare($idx, new Math_Integer(0)); $cmp_46 = Math_IntegerOp::compare($idx, new Math_Integer(46)); if ( ($cmp_0 >= 0) && ($cmp_46 <= 0) ) { $val = intval($idx->toString()); $fn = (pow(MATH_PHI, $val) - pow(MATH_phi, $val))/sqrt(5); // add to lookup table $table[$val] = $fn; return new Math_Integer(strval($fn)); } // from [47,500] use the lookup table $cmp_500 = Math_IntegerOp::compare($idx, new Math_Integer(500)); if ( ($cmp_46 > 0) && ($cmp_500 <= 0) ) { return new Math_Integer($table[$idx->toString()]); } else { // calculate and cache the values $a = new Math_Integer($table['499']); $b = new Math_Integer($table['500']); $pos = new Math_Integer('501'); $one = new Math_Integer('1'); while (Math_IntegerOp::compare($pos,$idx) <= 0) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?