Created
November 3, 2015 19:48
-
-
Save soldev-42/cd9e2fd357ad45c252ab 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 iterative | |
| */ | |
| function fibonacciNonRec($n){ | |
| if($n == 1) return '1'; | |
| if($n == 2) return '1, 1'; | |
| else { | |
| $ret = '1, 1'; | |
| $first = $second = 1; | |
| $count = 3; | |
| while($count <= $n) { | |
| $result = $first + $second; | |
| $first = $second; | |
| $second = $result; | |
| $ret .= ', '.$result; | |
| $count++; | |
| } | |
| return $ret; | |
| } | |
| } | |
| echo 'Non recursive fibonacci '.fibonacciNonRec(10)."\n"; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment