Strict Standards: Declaration of action_plugin_importoldindex::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/importoldindex/action.php on line 8

Strict Standards: Declaration of action_plugin_include::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/include/action.php on line 140

Strict Standards: Declaration of action_plugin_discussion::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/discussion/action.php on line 955

Strict Standards: Declaration of action_plugin_blog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/blog/action.php on line 154

Strict Standards: Declaration of action_plugin_importoldchangelog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/importoldchangelog/action.php on line 157

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 205

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 208

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 389

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 530

Strict Standards: Declaration of cache_instructions::retrieveCache() should be compatible with cache::retrieveCache($clean = true) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/cache.php on line 291

Deprecated: Function split() is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/auth.php on line 154

Strict Standards: Only variables should be passed by reference in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/doku.php on line 71
nex code:php:fibonacci
 
Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/parser.php on line 66

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/lexer.php on line 292

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 22

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 49

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 213

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 241

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 295

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 328

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/handler.php on line 575

Deprecated: Function split() is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/lexer.php on line 510

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 939

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Doku_Renderer_xhtml::_tocitem() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/html.php on line 741

Strict Standards: Non-static method Doku_Renderer_xhtml::_xmlEntities() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 119

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Doku_Renderer_xhtml::_tocitem() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/html.php on line 741

Strict Standards: Non-static method Doku_Renderer_xhtml::_xmlEntities() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 119

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Doku_Renderer_xhtml::_tocitem() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/html.php on line 741

Strict Standards: Non-static method Doku_Renderer_xhtml::_xmlEntities() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 119

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Doku_Renderer_xhtml::_tocitem() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/html.php on line 741

Strict Standards: Non-static method Doku_Renderer_xhtml::_xmlEntities() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 119

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method Doku_Renderer_xhtml::_tocitem() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/html.php on line 741

Strict Standards: Non-static method Doku_Renderer_xhtml::_xmlEntities() should not be called statically in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parser/xhtml.php on line 119

Fibonacci Generator (phpfib)

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.

Description

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

The Source Code

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>

Example of Use

Just enter into the form the index of the Fibonacci number you want to know. Then click the calculate button.

Assumptions

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


Strict Standards: Only variables should be passed by reference in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/doku.php on line 79