A fairly acute Fibonacci sequence generator written in the PHP scripting language. I wrote this in April of 2005 to get some practice in PHP for University but never actually finished. As with my other versions of this program, this is really just a conversion of the original Ada version I had done in second year Computing Science.
PLEASE NOTE: I HAVE NOT YET FINISHED THIS SCRIPT. I still need to do all the actual clever stuff with 'Big Integers'. You will find that for large fib indices, the script will probably overflow and give you incorrect results.
The Fibonacci algorithm was invented by an Italian mathematician (Fibonacci) to predict rabbit growth. A lot of assumptions are made, some of which are a bit sillier than others:
Assume that you start with two rabbits.
Assume that once a rabbit is three years old, it is capable of mating.
Assume that all rabbits capable of mating will in fact mate.
Assume that every time two rabbits mate, one offspring is produced without failure; no more, no less.
Assume rabbits can only produce one 'litter' a year.
Assume that there is an almost equal number of male and female rabbits.
Assume rabbits never die.
Anyway, the interesting thing about the Fibonacci algorithm is that it can be implemented recursively, and the numbers get big VERY quickly. The most basic way of expressing the algorithm is shown below:
fib[n] = fib[n-1] + fib[n-2]
where fib[1] = 1, fib[2] = 1
You can view all the source code of the program below.
phpfib.php:
<?php /** * A script to generate the fibonacci number for any entered * Fibonacci index. * * PHP versions 4 and 5 * * LICENSE: This source file is subject to the GNU General * Public License version 2. * * @category Mathematics * @package Phpfib * @author Nexami Engeo <nex@n-e-x.co.uk> * @copyright 2005 John Urquhart Ferguson * @license http://www.gnu.org/licenses/gpl.html GNU GPL version 2 * @version CVS: $Id:$ * @link http://www.n-e-x.co.uk/doku.php?id=programming:php:fibonacci * @since File available since Release 0.1 */ // {{{ phpfib /** * This is the main class for the phpfib program. * * @category Mathematics * @package Phpfib * @author Nexami Engeo <nex@n-e-x.co.uk> * @copyright 2005 John Urquhart Ferguson * @license http://www.gnu.org/licenses/gpl.html GNU GPL version 2 * @version CVS: $Id:$ * @link http://www.n-e-x.co.uk/doku.php?id=programming:php:fibonacci * @since Class available since Release 0.1 */ //class Phpfib //{ //} // }}} // {{{ helpFib() /** * A helper function for fib(). Calculates Fibonacci numbers. * * helpFib() is used to speed up the fib() method by preventing repeated * calculations. In truth, I only ever briefly understood how this works. * I didn't come up with it, and I understood it just long enough to * program it in the original Ada version of this program. * * @param int $n current Fibonacci index being calculated. * @param int $k a starting count which should initialy be 1. * @param BigInt $fibk a starting count which should initialy be 1. * @param BigInt $fibk1 a starting count which should initialy be 0. * * @return BigInt the Fibonacci number of the entertered index. * * @static * @since Method available since Release 0.1 */ function helpFib($n, $k, $fibk, $fibk1) { if ($n == $k) { return $fibk; } else { //return (helpFib($n, $k+1, $fibk->add($fibk1), $fibk)); return (helpFib($n, ($k+1), ($fibk+$fibk1), $fibk)); } } // }}} ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>phpFib</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="robots" content="noindex, nofollow"> <style type="text/css"> <!-- html { background-color: #f0f0f0; } body { font-family: Verdana, Arial, sans-serif; margin: 10px; border: 2px solid #e0e0e0; background-color: #fcfcfc; padding: 5px; } h2 { margin: .1em 0 .2em .5em; border-bottom: 1px solid #b0b0b0; color: #b0b0b0; font-weight: normal; font-size: 150%; } h3 { margin: .1em 0 .2em .5em; color: #b0b0b0; font-weight: normal; font-size: 120%; } #footer { text-align: center; font-size: 80%; color: #a9a9a9; } #footer a { color: #9999ff; } textarea { border: 1px solid #b0b0b0; font-size: 90%; color: #333; margin-left: 20px; } select, input { margin-left: 20px; } p { font-size: 90%; margin-left: .5em; } --> </style> </head> <body> <h2>phpFib</h2> <?php if ( isset($_POST['submit']) ) { echo '<p>Fibonacci number for index '.$_POST['fibIndex'].': <br/>'; echo helpFib($_POST['fibIndex'], 1, 1, 0).'</p>'; } ?> <p> Enter the Fibonacci index you wish to see the Fibonacci number for. </p> <form action="phpfib.php" method="post"> <input rows="0" cols="60" name="fibIndex"></input> <input type="submit" name="submit" value="Calculate Fibonacci Number"> </form> </body> </html>
Just enter into the form the index of the Fibonacci number you want to know. Then click the calculate button.
None, but for large Fibonacci indices, the computer may crash from running out of memory. It is also assumed that you have a system capable of running PHP scripts. If not, you can probably find what you need here.
— Nexami Engeo 2007/07/06 10:36
Discussion