I couldn’t sleep and was getting bored so I decided to write a function. I remember while reading a c++ book they showed you how to calculate the Fibonnaci number.
function fibonacci ($steps, $first = null, $second = 0, $step = 0)
{
if ($step<$steps)
{
if ($second==0)
{
$new_first = $second;
$new_second = 1;
}
else
{
$new_first = $second;
$new_second = $first+$second;
}
return fibonacci($steps, $new_first, $new_second, ++$step);
}
else
{
return $second;
}
}
A Fibonnaci sequence is a sequence such as 0 ,1 ,1 ,2 ,3 ,5 ,8 etc. Each number is the sum of its previous 2 numbers (except of course the 1st 2 numbers in the sequence). in my function I have done the same as wikipedia I have decided that it will start at f0 which denotes the 0 at the beginning of the sequence (f1 is 1, f2 is 1, f3 is 2 etc).
As you can see the function uses a recursive function. Be careful this can get out of hand very quickly and consume a lot of memory. The highest standard number my pc could handle was f68 which is 72723460248141 (f69 goes onto 1.1766903046099E+014 and don’t ask what f2000 showed… Ok you twisted my arm 1.#INF, i know i shouldn’t have but I had to try it hehe)
[...] time ago I wrote a quick article with an example script on how to obtain the Fibonnaci number using [...]