Skip to content

Instantly share code, notes, and snippets.

@waltarix
Created February 25, 2013 06:53
Show Gist options
  • Save waltarix/5028200 to your computer and use it in GitHub Desktop.
Save waltarix/5028200 to your computer and use it in GitHub Desktop.
Fibo, Tribo and Tetra nacci number, Lucas number in PHP
<?php
const CALC_LIMIT = 1000;
// fibo
var_dump(implode(', ', nacci(CALC_LIMIT, [0, 1])));
// tribo
var_dump(implode(', ', nacci(CALC_LIMIT, [0, 0, 1])));
// tetra
var_dump(implode(', ', nacci(CALC_LIMIT, [0, 0, 0, 1])));
// lucas
var_dump(implode(', ', nacci(CALC_LIMIT, [2, 1])));
function nacci($max, $seq) {
$seq_len = count($seq);
while ($max >= ($current = array_sum(array_slice($seq, count($seq) - $seq_len, $seq_len)))) {
$seq[] = $current;
}
return $seq;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment