for loop - PHP Fibonacci Sequence -
this php method suppose print fibonacci sequence specified value using loop. unsure why not work?
<?php function fib ($n) { // function called fib, declaire variable n (the sequence number) ($n=0;$n<30;$n++) { if ($n < 3) { return $n; } // if n smaller 3 return n (1 or 2) else { return fib ($n - 1) + fib ($n - 2); } /* if number 3 or above 2 sums (n-1) , (n-2) , add 2 sums (n-1)+(n-2) example fibonacci number 4 (4-1)+(4-2) = 5 3 + 2 = 5 */ } print $n; ?>
there way calculate fibbonacci number without iteration using rounding:
http://en.wikipedia.org/wiki/fibonacci_number#computation_by_rounding
function getfib($n) { return round(pow((sqrt(5)+1)/2, $n) / sqrt(5)); }
Comments
Post a Comment