Math_Fibonacci
[ class tree: Math_Fibonacci ] [ index: Math_Fibonacci ] [ all elements ]

Class: Math_Fibonacci

Source Location: /Math_Fibonacci-0.8/Fibonacci.php

Class Overview


Math_Fibonacci: class to calculate, validate, and decompose into Fibonacci numbers


Author(s):

Version:

  • 0.8

Methods


Inherited Variables

Inherited Methods


Class Details

[line 196]
Math_Fibonacci: class to calculate, validate, and decompose into Fibonacci numbers

Examples:

 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

 



[ Top ]


Method Detail

closestTo   [line 353]

mixed closestTo( integer $num)

Finds the Fibonacci number closest to an given integer
  • Return: a Fibonacci number (integer) on success, PEAR_Error otherwise
  • Access: public

Parameters:

integer   $num     

[ Top ]

decompose   [line 320]

mixed decompose( integer $num)

Decomposes an integer into a sum of Fibonacci numbers
  • Return: an array of Fibonacci numbers on success, PEAR_Error otherwise
  • Access: public

Parameters:

integer   $num     

[ Top ]

getIndexOf   [line 387]

mixed getIndexOf( integer $num)

Gets the index in the Fibonacci series of a given number.

If the integer given is not a Fibonacci number a PEAR_Error object will be returned.

  • Return: the index of the number in the series on success, PEAR_Error otherwise.
  • Access: public

Parameters:

integer   $num     the Fibonacci number

[ Top ]

isFibonacci   [line 303]

mixed isFibonacci( integer $num)

Determines if a particular integer is part of the Fibonacci series
  • Return: TRUE if it is a Fibonacci number, FALSE if not, PEAR_Error if the parameter was not an integer
  • See: Math_Fibonacci::term
  • Access: public

Parameters:

integer   $num     

[ Top ]

series   [line 270]

mixed series( integer $idx1, [optional $idx2 = null])

Returns a series of Fibonacci numbers using the given limits.

Method accepts two parameters, of which the second one is optional. If two parameters are passed, the first one will be lower bound and the second one the upper bound. If only one parameter is passed, it will be the upper bound, and the lower bound will be 0 (zero).

  • Return: on success, an array of integers where the keys correspond to the indexes of the corresponding Fibonacci numbers, or PEAR_Error othewise
  • Access: public

Parameters:

integer   $idx1     the lower index for the series (if two parameters) were passed, or the upper index if only one was given.
optional   $idx2     integer $idx2 the upper index for the series

[ Top ]

term   [line 209]

mixed &term( mixed $n)

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

  • Return: numeric on success, PEAR_Error otherwise
  • Access: public

Parameters:

mixed   $n     the index of the Fibonacci number to calculate, as an integer or a Math_Integer number

[ Top ]


Documentation generated on Fri, 30 Apr 2004 22:02:32 +0200 by phpDocumentor 1.2.3. PEAR Logo Copyright © PHP Group 2004.