Skip to content

Instantly share code, notes, and snippets.

@soldev-42
Created November 3, 2015 19:48
Show Gist options
  • Select an option

  • Save soldev-42/cd9e2fd357ad45c252ab to your computer and use it in GitHub Desktop.

Select an option

Save soldev-42/cd9e2fd357ad45c252ab to your computer and use it in GitHub Desktop.
<?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