/**
  * Estimates the approximate index for a given number
  * It uses the approximate formula:
  * F(n) ~ (PHI^n)/sqrt(5), where '~' means the 'closest integer to'
  * This equation is based on the relation: phi = -1/PHI
  * Which turns Lucas' formula into:
  * F(n) = (PHI^2n + 1)/(PHI^n * sqrt(5))
  * From which we get the formula above, after making the approximation:
  * (PHI^2n + 1) -> (PHI^2n)
  *
  * @param integer $num
  * @return integer the approximate index
  * @access private
  */
 function &_estimateN(&$num)
 {
     /*{{{*/
     if (Math_IntegerOp::isMath_Integer($num)) {
         $f = $num->toString();
     } else {
         $f = $num;
     }
     return Math_Fibonacci::_closestInt((log($f) + MATH_LNSQRT5) / MATH_LNPHI);
 }
$decarr = Math_Fibonacci::decompose(new Math_Integer(34512));
foreach ($decarr as $fib) {
    $index = Math_Fibonacci::getIndexOf($fib);
    echo "F(" . $index->toString() . ") = " . $fib->toString() . "\n";
}
echo "\nF(n) closest to 314156 is: ";
$fib = Math_Fibonacci::closestTo(new Math_Integer(314156));
echo $fib->toString() . "\n\n";
echo 'The index for 1597 is : ';
$idx = Math_Fibonacci::getIndexOf(new Math_Integer(1597));
echo $idx->toString() . "\n\n";
$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";
}
// Benchmark below requires PEAR::Benchmark, PEAR::Math_Stats
// and PEAR::Math_Histogram
$benchmark = false;
if ($benchmark) {
    require_once 'Benchmark/Iterate.php';
    require_once 'Math/Histogram.php';
    // benchmark the fast algorithm
    $index = 45;
    // benchmark the lookup table
    //$index = 100;
    // benchmark the addition loop
    //$index = 1000;
    $runs = 2000;