How to resolve the algorithm Zeckendorf number representation step by step in the CLU programming language
How to resolve the algorithm Zeckendorf number representation step by step in the CLU programming language
Table of Contents
Problem Statement
Just as numbers can be represented in a positional notation as sums of multiples of the powers of ten (decimal) or two (binary); all the positive integers can be represented as the sum of one or zero times the distinct members of the Fibonacci series. Recall that the first six distinct Fibonacci numbers are: 1, 2, 3, 5, 8, 13. The decimal number eleven can be written as 013 + 18 + 05 + 13 + 02 + 01 or 010100 in positional notation where the columns represent multiplication by a particular member of the sequence. Leading zeroes are dropped so that 11 decimal becomes 10100. 10100 is not the only way to make 11 from the Fibonacci numbers however; 013 + 18 + 05 + 03 + 12 + 11 or 010011 would also represent decimal 11. For a true Zeckendorf number there is the added restriction that no two consecutive Fibonacci numbers can be used which leads to the former unique solution.
Generate and show here a table of the Zeckendorf number representations of the decimal numbers zero to twenty, in order.
The intention in this task to find the Zeckendorf form of an arbitrary integer. The Zeckendorf form can be iterated by some bit twiddling rather than calculating each value separately but leave that to another separate task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zeckendorf number representation step by step in the CLU programming language
Source code in the clu programming language
% Get list of distinct Fibonacci numbers up to N
fibonacci = proc (n: int) returns (array[int])
list: array[int] := array[int]$[]
a: int := 1
b: int := 2
while a <= n do
array[int]$addh(list,a)
a, b := b, a+b
end
return(list)
end fibonacci
% Find the Zeckendorf representation of N
zeckendorf = proc (n: int) returns (string) signals (negative)
if n<0 then signal negative end
if n=0 then return("0") end
fibs: array[int] := fibonacci(n)
result: array[char] := array[char]$[]
while ~array[int]$empty(fibs) do
fib: int := array[int]$remh(fibs)
if fib <= n then
n := n - fib
array[char]$addh(result,'1')
else
array[char]$addh(result,'0')
end
end
return(string$ac2s(result))
end zeckendorf
% Print the Zeckendorf representations of 0 to 20
start_up = proc ()
po: stream := stream$primary_output()
for i: int in int$from_to(0,20) do
stream$putright(po, int$unparse(i), 2)
stream$puts(po, ": ")
stream$putl(po, zeckendorf(i))
end
end start_up
You may also check:How to resolve the algorithm HTTPS step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Wasteful, equidigital and frugal numbers step by step in the F# programming language
You may also check:How to resolve the algorithm Binary digits step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the SparForte programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the Racket programming language