Created
February 25, 2013 06:53
-
-
Save waltarix/5028200 to your computer and use it in GitHub Desktop.
Fibo, Tribo and Tetra nacci number, Lucas number in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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