Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcmascort/f4abfa962759f4cc5992 to your computer and use it in GitHub Desktop.
Save marcmascort/f4abfa962759f4cc5992 to your computer and use it in GitHub Desktop.
Get the Fibonacci sequence up to a maximum value
<?php
function fibonacci_sequence_up_to($value=0)
{
$value = abs(intval($value));
$sequence = array();
$count = 0;
while($count===0 || $sequence[$count-1] < $value)
{
$a = isset($sequence[$count-2]) ? $sequence[$count-2] : 0;
$b = isset($sequence[$count-1]) ? $sequence[$count-1] : 1;
$sequence[] = $a + $b;
++$count;
}
return $sequence;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment