Last active
December 12, 2015 02:18
-
-
Save ymkjp/4697401 to your computer and use it in GitHub Desktop.
n-Step Fibonacci numbers
This file contains hidden or 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 | |
| $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