Skip to content

Instantly share code, notes, and snippets.

@ymkjp
Last active December 12, 2015 02:18
Show Gist options
  • Select an option

  • Save ymkjp/4697401 to your computer and use it in GitHub Desktop.

Select an option

Save ymkjp/4697401 to your computer and use it in GitHub Desktop.
n-Step Fibonacci numbers
<?php
$end = 1000; // Set the end of number here
$step = 4; // Set the step of Fibonacci, in this script it's set as tetranacci numbers
$arr = array();
$nacci;
if ($step < 2) {
trigger_error('ERROR: $step must be 2 or greater.', E_USER_ERROR);
} else if ($step > $end) {
trigger_error('ERROR: $end must be greater than $step.', E_USER_ERROR);
}
while ($step > 1) {
--$step;
$arr[] = 0;
}
$arr[] = 1;
foreach ($arr as $v) {
echo $v . "\n";
}
nacci($arr, $end);
function nacci($arr, $end) {
$tail = 0;
foreach ($arr as $v) {
$tail = $tail + $v;
}
if ($tail <= $end) {
echo $tail . "\n";
array_shift($arr);
$arr[] = $tail;
nacci($arr, $end);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment