Created
November 3, 2015 19:46
-
-
Save soldev-42/6384f8ff046568c2df16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @param {Integer} $n number of fibonacci sequence items | |
| * @return {String} fibonacci numbers calculated using recursion | |
| */ | |
| function fibonacciRec($n, $a = array()){ | |
| if(!isset($a[0]) || $a[0] != 1){ | |
| $a[0] = 1; | |
| } | |
| if(!isset($a[1]) || $a[1] != 1){ | |
| $a[1] = 1; | |
| } | |
| $t = count($a); | |
| $a[] = $a[$t - 1] + $a[$t - 2]; | |
| if($n > count($a)){ | |
| return fibonacciRec($n, $a); | |
| } | |
| return implode(', ', $a); | |
| } | |
| echo 'Recursive fibonacci '.fibonacciRec(10)."\n"; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment