Zeckendorf number representation: Difference between revisions

Add MATLAB implementation
(Add Rust implementation)
(Add MATLAB implementation)
Line 3,576:
<pre>{0, 1, 10, 100, 101, 1000, 1001, 1010, 10000, 10001, 10010, 10100,
10101, 100000, 100001, 100010, 100100, 100101, 101000, 101001, 101010}</pre>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB">
clear all; close all; clc;
 
% Print the sequence for numbers from 0 to 20
for x = 0:20
zeckString = arrayfun(@num2str, zeck(x), 'UniformOutput', false);
zeckString = strjoin(zeckString, '');
fprintf("%d : %s\n", x, zeckString);
end
 
function dig = zeck(n)
if n <= 0
dig = 0;
return;
end
fib = [1, 2];
while fib(end) < n
fib(end + 1) = sum(fib(end-1:end));
end
fib = fliplr(fib); % Reverse the order of Fibonacci numbers
dig = [];
for i = 1:length(fib)
if fib(i) <= n
dig(end + 1) = 1;
n = n - fib(i);
else
dig(end + 1) = 0;
end
end
if dig(1) == 0
dig = dig(2:end);
end
end
</syntaxhighlight>
{{out}}
<pre>
0 : 0
1 : 1
2 : 10
3 : 100
4 : 101
5 : 1000
6 : 1001
7 : 1010
8 : 10000
9 : 10001
10 : 10010
11 : 10100
12 : 10101
13 : 100000
14 : 100001
15 : 100010
16 : 100100
17 : 100101
18 : 101000
19 : 101001
20 : 101010
 
</pre>
 
=={{header|MiniScript}}==
337

edits