A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Zeckendorf in Python page! Here, you'll find the source code for this program as well as a description of how the program works.
import sys
from typing import NoReturn
def usage() -> NoReturn:
print("Usage: please input a non-negative integer")
sys.exit(1)
def fibonacci_up_to(n: int) -> list[int]:
result: list[int] = []
a = 1
b = 2
while a <= n:
result.append(a)
a, b = b, a + b
return result
def zeckendorf(n: int) -> list[int]:
result: list[int] = []
fibs = fibonacci_up_to(n)
idx = len(fibs) - 1
while idx >= 0 and n > 0:
fib = fibs[idx]
if fib <= n:
n -= fib
result.append(fib)
idx -= 2
else:
idx -= 1
return result
def main() -> int:
if len(sys.argv) < 2:
usage()
try:
n = int(sys.argv[1])
except ValueError:
usage()
if n < 0:
usage()
result = zeckendorf(n)
print(result)
if __name__ == "__main__":
main()
Zeckendorf in Python was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.