Skip to content

Instantly share code, notes, and snippets.

@ggirtsou
Created March 10, 2016 19:24
Show Gist options
  • Save ggirtsou/9d1897ca9cc4d96b8bfd to your computer and use it in GitHub Desktop.
Save ggirtsou/9d1897ca9cc4d96b8bfd to your computer and use it in GitHub Desktop.
Prints fibonacci sequence based on $numbersToShow starting from $startNumber
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
$startNumber = 0;
$numbersToShow = 10;
function fibonacci($number)
{
if ($number === 0) {
return 0;
} elseif ($number === 1) {
return 1;
} else {
return (fibonacci($number-1) + fibonacci($number-2));
}
}
for ($i = $startNumber; $i < ($startNumber+$numbersToShow); $i++) {
echo fibonacci($i).PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment