Sunday, September 29, 2013

Fibonacci Numbers with PHP

hi

how to calculate fibonacci numbers with php? you can do it this way.


CODE

<?php

fibonacci(1000);

function fibonacci($numbersToShow){
  $arr[0] = 0;
  $arr[1] = 1;
   
  echo "0: ". $arr[0] . "<br>";
  echo "1: ". $arr[1] . "<br>";
   
  for($n = 1; $n < $numbersToShow; $n++) {
      $arr[$n + 1] = $arr[$n] + $arr[$n - 1];
      echo ($n + 1) . ": " . $arr[$n + 1] . "<br>";
  }
}

?>

No comments:

Post a Comment