A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Zeckendorf page! Here, you'll find a description of the project as well as a list of sample programs written in various languages.
This article was written by:
Edouard Zeckendorf was a Belgian amateur mathematician who proposed a Theorem that states that every non-negative integer (N) can be represented as a sum of distinct non-consecutive Fibonacci Numbers. In other words,
where:
Each Fibonacci number (Fk) is the sum of the previous two Fibonacci numbers:
This relationship is the reason why non-consecutive Fibonacci numbers are used.
To find the Fibonacci numbers that add up to N:
For example, for N = 67, these are the Fibonacci numbers up to 67:
Select the appropriate Fibonacci numbers:
| Trial Sum | Fibonacci Number | Selected |
|---|---|---|
| 0 | 55 | ✅ |
| 55 | 34 | |
| 55 | 21 | |
| 55 | 13 | |
| 55 | 8 | ✅ |
| 63 | 5 | |
| 63 | 3 | ✅ |
| 66 | 2 | |
| 66 | 1 | ✅ |
Therefore, using the selected numbers, N can be represented as:
A possible optimization for this procedure would be to skip Fibonacci numbers that are consecutive to the last selected value and to terminate the loop as soon as the sum equals to N.
Create a file called "Zeckendorf" using the naming convention appropriate for your language of choice.
Write a sample program that accepts a single non-negative integer from the command line and outputs a comma-separated list of Fibonacci numbers whose sum equals the specified value. For zero, output an empty list. If the command line argument is missing or not a non-negative integer, output the error message specified in the Testing section.
Every project in the Sample Programs repo should be tested. In this section, we specify the set of tests specific to Zeckendorf. In order to keep things simple, we split up the testing as follows:
| Description | Input | Output |
|---|---|---|
| Zero | "0" | "" |
| Small Fibonacci Value | "55" | "55" |
| Small Non-Fibonacci Value | "67" | "55, 8, 3, 1" |
| Large Fibonacci Value | "10946" | "10946" |
| Large Non-Fibonacci Value | "16383" | "10946, 4181, 987, 233, 34, 2" |
| Description | Input |
|---|---|
| No Input | |
| Empty Input | "" |
| Negative Input | "-2" |
| Floating Point Input | "2.6" |
| Non-Numeric Input | "bad" |
All of these tests should output the following:
Usage: please input a non-negative integer
There is 1 article: