Skip to content

Add Zeckendorf representation algorithm - #15663

Open
mgkvibez wants to merge 1 commit into
TheAlgorithms:masterfrom
mgkvibez:add-zeckendorf-representation
Open

mgkvibez wants to merge 1 commit into
TheAlgorithms:masterfrom
mgkvibez:add-zeckendorf-representation

Conversation

@mgkvibez

Copy link
Copy Markdown

Implements Zeckendorf's theorem: every positive integer can be written uniquely as the sum of one or more distinct, non-consecutive Fibonacci numbers.

maths/zeckendorf.py provides two functions:

  • _fibonacci_numbers_up_to(limit) builds the sequence starting at 1, 2 (rather than 1, 1) so that the value 1 is used at most once and the representation stays unique.
  • zeckendorf(number) recovers the representation with a greedy pass over those Fibonacci numbers from largest to smallest, which is always optimal for this problem.
>>> zeckendorf(100)
[89, 8, 3]
>>> sum(zeckendorf(100)) == 100
True

Doctests cover 0, 1, 2, 4, 10, 100, 1000, plus TypeError for non-integer input and ValueError for negative input.

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Local verification before opening: ruff check and ruff format --check clean under the repo's pyproject.toml, pytest maths/zeckendorf.py --doctest-modules 2 passed, 12 doctests passed. README.md and DIRECTORY.md left untouched for the automation to regenerate.

Implements Zeckendorf's theorem: every positive integer can be written
uniquely as the sum of one or more distinct, non-consecutive Fibonacci
numbers. The representation is recovered with a greedy pass from the
largest Fibonacci number downwards, which is optimal for this problem.

Includes doctests for 0, 1, 2, 4, 10, 100 and 1000, plus TypeError for
non-integers and ValueError for negative input.

https://en.wikipedia.org/wiki/Zeckendorf%27s_theorem
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant