How to resolve the algorithm Zeckendorf number representation step by step in the Picat programming language
How to resolve the algorithm Zeckendorf number representation step by step in the Picat 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 Picat programming language
Source code in the picat programming language
go =>
foreach(Num in 0..20)
zeckendorf_cp(Num,X,F),
Nums = [F[I] : I in 1..X.length, X[I] = 1],
printf("%2d %6s %w\n",Num, rep(X),Nums),
end,
nl.
zeckendorf_cp(Num, X,F) =>
F = get_fibs(Num).reverse(),
N = F.length,
X = new_list(N),
X :: 0..1,
% From the task description:
% """
% 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.
% """
foreach(I in 2..N)
X[I-1] #= 1 #=> X[I] #= 0
end,
scalar_product(F,X,Num),
solve([ff,split],X).
%
% Fibonacci numbers
%
table
fib(0) = 0.
fib(1) = 1.
fib(N) = fib(N-1) + fib(N-2).
%
% Remove leading 0's and stringify it
%
rep(X) = Str =>
First = 1,
if X.length > 1, X[First] = 0 then
while (X[First] == 0)
First := First + 1
end
end,
Str = [X[I].to_string() : I in First..X.length].join('').
%
% Return a list of fibs <= N
%
get_fibs(N) = Fibs =>
I = 2,
Fib = fib(I),
Fibs1 = [Fib],
while (Fib < N)
I := I + 1,
Fib := fib(I),
Fibs1 := Fibs1 ++ [Fib]
end,
Fibs = Fibs1.
go2 =>
foreach(Num in 0..20)
zeckendorf2(Num,X,F),
Nums = [F[I] : I in 1..X.length, X[I]= 1],
printf("%2d %6s %w\n",Num, rep(X),Nums)
end,
nl.
zeckendorf2(0, [0],[0]).
zeckendorf2(Num, X,F) :-
Fibs = get_fibs(Num),
I = Fibs.length,
N = Num,
X1 = [],
while (I > 0)
Fib := Fibs[I],
X1 := X1 ++ [cond(Fib > N,0,1)],
if Fib <= N then
N := N - Fib
end,
I := I - 1
end,
X = X1,
F = Fibs.reverse().
You may also check:How to resolve the algorithm Same fringe step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Video display modes step by step in the Wren programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Processing programming language