Zeckendorf in Mathematica

Published on 30 April 2026 (Updated: 30 April 2026)

Welcome to the Zeckendorf in Mathematica page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

ClearAll[zeckendorfMain, nf, zeckendorf];

nf[n_] := Fibonacci @ Floor @ Log[GoldenRatio, n * Sqrt[5] + 1/2];
zeckendorf[n_Integer?Positive] := -Differences[NestWhileList[# - nf[#] &, n, # != 0 &]];

$usage = "Usage: please input a non-negative integer";

zeckendorfMain[s_String /; StringMatchQ[s, DigitCharacter ..]] := 
  With[{n = FromDigits[s]},
    Which[
      n == 0, "",
      n > 0, StringRiffle[ToString /@ zeckendorf[n], ", "]
    ]
  ];

zeckendorfMain[___] := $usage;

Print /@ zeckendorfMain /@ {
    (* Valid cases *)
    "0",
    "55",
    "67",
    "10946",
    "16383",

    (* Invalid cases*)
    "",
    "-2",
    "2.6",
    "bad"
};

Print[zeckendorfMain[]]

Zeckendorf in Mathematica was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.