Zeckendorf number representation: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 9 users not shown)
Line 704:
</pre>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> FOR n% = 0 TO 20
PRINT n% RIGHT$(" " + FNzeckendorf(n%), 8)
Line 759 ⟶ 760:
No Zeckendorf numbers contain consecutive 1's
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 17-10-2016
' compile with: fbc -s console
 
#Define max 92 ' max for Fibonacci number
 
Dim Shared As ULongInt fib(max)
 
fib(0) = 1
fib(1) = 1
 
For x As Integer = 2 To max
fib(x) = fib(x-1) + fib(x-2)
Next
 
Function num2zeck(n As Integer) As String
 
If n < 0 Then
Print "Error: no negative numbers allowed"
Beep : Sleep 5000,1 : End
End If
 
If n < 2 Then Return Str(n)
 
Dim As String zeckendorf
For x As Integer = max To 1 Step -1
If fib(x) <= n Then
zeckendorf = zeckendorf + "1"
n = n - fib(x)
Else
zeckendorf = zeckendorf + "0"
End If
Next
 
return LTrim(zeckendorf, "0") ' get rid of leading zeroes
End Function
 
' ------=< MAIN >=------
 
Dim As Integer x, e
Dim As String zeckendorf
Print "number zeckendorf"
 
For x = 0 To 200000
zeckendorf = num2zeck(x)
If x <= 20 Then Print x, zeckendorf
' check for two consecutive Fibonacci numbers
If InStr(zeckendorf, "11") <> 0 Then
Print " Error: two consecutive Fibonacci numbers "; x, zeckendorf
e = e +1
End If
Next
 
Print
If e = 0 Then
Print " No Zeckendorf numbers with two consecutive Fibonacci numbers found"
Else
Print e; " error(s) found"
End If
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>number zeckendorf
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
 
No Zeckendorf numbers with two consecutive Fibonacci numbers found</pre>
 
==={{header|Liberty BASIC}}===
''CBTJD'': 2020/03/09
{{works with|Just BASIC}}
<syntaxhighlight lang="vb">samples = 20
call zecklist samples
 
print "Decimal","Zeckendorf"
for n = 0 to samples
print n, zecklist$(n)
next n
 
Sub zecklist inDEC
dim zecklist$(inDEC)
do
bin$ = dec2bin$(count)
if instr(bin$,"11") = 0 then
zecklist$(found) = bin$
found = found + 1
end if
count = count+1
loop until found = inDEC + 1
End sub
 
function dec2bin$(inDEC)
do
bin$ = str$(inDEC mod 2) + bin$
inDEC = int(inDEC/2)
loop until inDEC = 0
dec2bin$ = bin$
end function</syntaxhighlight>
{{out}}
<pre>
Decimal Zeckendorf
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|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.s zeck(n.i)
Dim f.i(1) : Define i.i=1, o$
f(0)=1 : f(1)=1
While f(i)<n
i+1 : ReDim f(ArraySize(f())+1) : f(i)=f(i-1)+f(i-2)
Wend
For i=i To 1 Step -1
If n>=f(i) : o$+"1" : n-f(i) : Else : o$+"0" : EndIf
Next
If Len(o$)>1 : o$=LTrim(o$,"0") : EndIf
ProcedureReturn o$
EndProcedure
 
Define n.i, t$
OpenConsole("Zeckendorf number representation")
PrintN(~"\tNr.\tZeckendorf")
For n=0 To 20
t$=zeck(n)
If FindString(t$,"11")
PrintN("Error: n= "+Str(n)+~"\tZeckendorf= "+t$)
Break
Else
PrintN(~"\t"+RSet(Str(n),3," ")+~"\t"+RSet(t$,7," "))
EndIf
Next
Input()</syntaxhighlight>
{{out}}
<pre> Nr. Zeckendorf
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|QuickBASIC}}===
{{trans|ALGOL 68}}
<syntaxhighlight lang="qbasic">
' Zeckendorf number representation
DECLARE FUNCTION ToZeckendorf$ (N%)
' The maximum Fibonacci number that can fit in a
' 32 bit number is Fib&(45)
CONST MAXFIBINDEX% = 45, TRUE% = -1, FALSE% = 0
DIM SHARED Fib&(1 TO MAXFIBINDEX%)
Fib&(1) = 1: Fib&(2) = 2
FOR I% = 3 TO MAXFIBINDEX%
Fib&(I%) = Fib&(I% - 1) + Fib&(I% - 2)
NEXT I%
FOR I% = 0 TO 20
SixChars$ = SPACE$(6)
RSET SixChars$ = ToZeckendorf$(I%)
PRINT USING "### "; I%; : PRINT SixChars$
NEXT I%
END
 
FUNCTION ToZeckendorf$ (N%)
' returns the Zeckendorf representation of N% or "?" if one cannot be found
IF N% = 0 THEN
ToZeckendorf$ = "0"
ELSE
Result$ = ""
FPos% = MAXFIBINDEX%
Rest% = ABS(N%)
' Find the first non-zero Zeckendorf digit
WHILE FPos% > 1 AND Rest% < Fib&(FPos%)
FPos% = FPos% - 1
WEND
' If we found a digit, build the representation
IF FPos% >= 1 THEN ' have a digit
SkipDigit% = FALSE%
WHILE FPos% >= 1
IF Rest% <= 0 THEN
Result$ = Result$ + "0"
ELSEIF SkipDigit% THEN ' we used the previous digit
SkipDigit% = FALSE%
Result$ = Result$ + "0"
ELSEIF Rest% < Fib&(FPos%) THEN ' cannot use the digit at FPos%
SkipDigit% = FALSE%
Result$ = Result$ + "0"
ELSE ' can use this digit
SkipDigit% = TRUE%
Result$ = Result$ + "1"
Rest% = Rest% - Fib&(FPos%)
END IF
FPos% = FPos% - 1
WEND
END IF
IF Rest% = 0 THEN
ToZeckendorf$ = Result$
ELSE
ToZeckendorf$ = "?"
END IF
END IF
END FUNCTION
</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|Sinclair ZX81 BASIC}}===
Works on the 1k RAM model, albeit without much room for manoeuvre. (You'd like the Zeckendorf numbers further over towards the right-hand side of the screen? Sorry, can't spare the video RAM.) If you have 2k or more, you can replace the constant 6 with some higher value wherever it occurs in the program and enable yourself to represent bigger numbers in Zeckendorf form.
<syntaxhighlight lang="basic"> 10 DIM F(6)
20 LET F(1)=1
30 LET F(2)=2
40 FOR I=3 TO 6
50 LET F(I)=F(I-2)+F(I-1)
60 NEXT I
70 FOR I=0 TO 20
80 LET Z$=""
90 LET S$=" "
100 LET Z=I
110 FOR J=6 TO 1 STEP -1
120 IF J=1 THEN LET S$="0"
130 IF Z<F(J) THEN GOTO 180
140 LET Z$=Z$+"1"
150 LET Z=Z-F(J)
160 LET S$="0"
170 GOTO 190
180 LET Z$=Z$+S$
190 NEXT J
200 PRINT I;" ";
210 IF I<10 THEN PRINT " ";
220 PRINT Z$
230 NEXT I</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|uBasic/4tH}}===
<syntaxhighlight lang="text">For x = 0 to 20 ' Print Zeckendorf numbers 0 - 20
Print x,
Push x : Gosub _Zeckendorf ' get Zeckendorf number repres.
Print ' terminate line
Next
 
End
 
_Fibonacci
Push Tos() ' duplicate TOS()
@(0) = 0 ' This function returns the
@(1) = 1 ' Fibonacci number which is smaller
' or equal to TOS()
Do While @(1) < Tos() + 1
Push (@(1))
@(1) = @(0) + @(1) ' get next Fibonacci number
@(0) = Pop()
Loop ' loop if not exceeded TOS()
 
Gosub _Drop ' clear TOS()
Push @(0) ' return Fibonacci number
Return
 
_Zeckendorf
GoSub _Fibonacci ' This function breaks TOS() up
Print Tos(); ' into its Zeckendorf components
Push -(Pop() - Pop()) ' first digit is always there
' the remainder to resolve
Do While Tos() ' now go for the next digits
GoSub _Fibonacci
Print " + ";Tos(); ' print the next digit
Push -(Pop() - Pop())
Loop
 
Gosub _Drop ' clear TOS()
Return ' and return
 
_Drop
If Pop()%1 = 0 Then Return ' This function clears TOS()</syntaxhighlight>
Output:
<pre>0 0
1 1
2 2
3 3
4 3 + 1
5 5
6 5 + 1
7 5 + 2
8 8
9 8 + 1
10 8 + 2
11 8 + 3
12 8 + 3 + 1
13 13
14 13 + 1
15 13 + 2
16 13 + 3
17 13 + 3 + 1
18 13 + 5
19 13 + 5 + 1
20 13 + 5 + 2
 
0 OK, 0:901</pre>
 
==={{header|VBA}}===
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function zeckendorf(ByVal n As Integer) As Integer
Dim r As Integer: r = 0
Dim c As Integer
Dim fib As New Collection
fib.Add 1
fib.Add 1
Do While fib(fib.Count) < n
fib.Add fib(fib.Count - 1) + fib(fib.Count)
Loop
For i = fib.Count To 2 Step -1
c = n >= fib(i)
r = r + r - c
n = n + c * fib(i)
Next i
zeckendorf = r
End Function
Public Sub main()
Dim i As Integer
For i = 0 To 20
Debug.Print Format(i, "@@"); ":"; Format(WorksheetFunction.Dec2Bin(zeckendorf(i)), "@@@@@@@")
Next i
End Sub</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|VBScript}}===
<syntaxhighlight lang="vb">
Function Zeckendorf(n)
num = n
Set fibonacci = CreateObject("System.Collections.Arraylist")
fibonacci.Add 1 : fibonacci.Add 2
i = 1
Do While fibonacci(i) < num
fibonacci.Add fibonacci(i) + fibonacci(i-1)
i = i + 1
Loop
tmp = ""
For j = fibonacci.Count-1 To 0 Step -1
If fibonacci(j) <= num And (tmp = "" Or Left(tmp,1) <> "1") Then
tmp = tmp & "1"
num = num - fibonacci(j)
Else
tmp = tmp & "0"
End If
Next
Zeckendorf = CLng(tmp)
End Function
 
'testing the function
For k = 0 To 20
WScript.StdOut.WriteLine k & ": " & Zeckendorf(k)
Next
</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|Yabasic}}===
<syntaxhighlight lang="yabasic">sub Zeckendorf(n)
local i, n$, c
do
n$ = bin$(i)
if not instr(n$,"11") then
print c,":\t",n$
if c = n break
c = c + 1
end if
i = i + 1
loop
end sub
 
Zeckendorf(20)
</syntaxhighlight>
 
=={{header|bc}}==
<syntaxhighlight lang="bc">obase = 2
f[0] = 1
f[t = 1] = 2
 
define z(n) {
auto p, r
 
for (p = t; p >= 0; --p) {
r += r
if (n >= f[p]) {
r += 1
n -= f[p]
}
}
return(r)
}
 
for (x = 0; x != 21; ++x) {
if (x > f[t]) {
t += 1
f[t] = f[t - 2] + f[t - 1]
}
z(x)
}</syntaxhighlight>
{{out}}
<pre>0
1
10
100
101
1000
1001
1010
10000
10001
10010
10100
10101
100000
100001
100010
100100
100101
101000
101001
101010</pre>
 
=={{header|Befunge}}==
Line 1,431 ⟶ 1,996:
writefln("%2d: %6s", i, i.zeckendorf);
}</syntaxhighlight>
 
=={{header|Dart}}==
{{trans|Java}}
<syntaxhighlight lang="Dart">
class Zeckendorf {
static String getZeckendorf(int n) {
if (n == 0) {
return "0";
}
List<int> fibNumbers = [1];
int nextFib = 2;
while (nextFib <= n) {
fibNumbers.add(nextFib);
nextFib += fibNumbers[fibNumbers.length - 2];
}
StringBuffer sb = StringBuffer();
for (int i = fibNumbers.length - 1; i >= 0; i--) {
int fibNumber = fibNumbers[i];
sb.write((fibNumber <= n) ? "1" : "0");
if (fibNumber <= n) {
n -= fibNumber;
}
}
return sb.toString();
}
 
static void main() {
for (int i = 0; i <= 20; i++) {
print("Z($i)=${getZeckendorf(i)}");
}
}
}
 
void main() {
Zeckendorf.main();
}
</syntaxhighlight>
{{out}}
<pre>
Z(0)=0
Z(1)=1
Z(2)=10
Z(3)=100
Z(4)=101
Z(5)=1000
Z(6)=1001
Z(7)=1010
Z(8)=10000
Z(9)=10001
Z(10)=10010
Z(11)=10100
Z(12)=10101
Z(13)=100000
Z(14)=100001
Z(15)=100010
Z(16)=100100
Z(17)=100101
Z(18)=101000
Z(19)=101001
Z(20)=101010
 
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
const FibNums: array [0..21] of integer =
(1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
144, 233, 377, 610, 987, 1597, 2584,
4181, 6765, 10946, 17711, 28657);
 
 
function GetZeckNumber(N: integer): string;
{Returns Zeckendorf number for N as string}
var I: integer;
begin
Result:='';
{Subtract Fibonacci numbers from N}
for I:=High(FibNums) downto 0 do
if (N-FibNums[I])>=0 then
begin
Result:=Result+'1';
N:=N-FibNums[I];
end
else if Length(Result)>0 then Result:=Result+'0';
if Result='' then Result:='0';
end;
 
 
procedure ShowZeckendorfNumbers(Memo: TMemo);
var I: integer;
var S: string;
begin
S:='';
for I:=0 to 20 do
begin
Memo.Lines.Add(IntToStr(I)+': '+GetZeckNumber(I));
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
 
Elapsed Time: 26.683 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
proc mkfibs n . fib[] .
fib[] = [ ]
last = 1
current = 1
while current <= n
fib[] &= current
nxt = last + current
last = current
current = nxt
.
.
func$ zeckendorf n .
mkfibs n fib[]
for pos = len fib[] downto 1
if n >= fib[pos]
zeck$ &= "1"
n -= fib[pos]
else
zeck$ &= "0"
.
.
if zeck$ = ""
return "0"
.
return zeck$
.
for n = 0 to 20
print " " & n & " " & zeckendorf n
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,476 ⟶ 2,209:
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import system'collections;
Line 1,505 ⟶ 2,238:
while (currentFibonaciNum <= num)
{
fibonacciNumbers.append:(currentFibonaciNum);
fibPosition := fibPosition + 1;
Line 1,514 ⟶ 2,247:
int temp := num;
fibonacciNumbers.sequenceReverse().forEach::(item)
{
if (item <= temp)
Line 1,533 ⟶ 2,266:
public program()
{
for(int i := 1,; i <= 20,; i += 1)
{
console.printFormatted("{0} : {1}",i,i.zeckendorf()).writeLine()
Line 1,620 ⟶ 2,353:
for i <- 0..20, do: IO.puts "#{i}: #{Zeckendorf.number(i)}"</syntaxhighlight>
same output
 
 
=={{header|Erlang}}==
{{trans|Elixir}}
<syntaxhighlight lang="Erlang">
% Function to generate a list of the first N Zeckendorf numbers
number(N) ->
number_helper(N, 0, 0, []).
 
number_helper(0, _, _, Acc) ->
lists:reverse(Acc);
number_helper(N, Curr, Index, Acc) ->
case zn_loop(Curr) of
{Bin, Next} ->
number_helper(N - 1, Next, Index + 1, [{Bin, Index} | Acc])
end.
 
% Helper function to find the next Zeckendorf number
zn_loop(N) ->
Bin = my_integer_to_binary(N),
case re:run(Bin, "11", [{capture, none}]) of
match ->
zn_loop(N + 1);
nomatch ->
{Bin, N + 1}
end.
 
% Convert an integer to its binary representation as a string
my_integer_to_binary(N) ->
lists:flatten(io_lib:format("~.2B", [N])).
 
% Test function to output the first 21 Zeckendorf numbers
main([]) ->
ZnNumbers = number(21),
lists:foreach(
fun({Zn, I}) ->
io:format("~p: ~s~n", [I, Zn])
end, ZnNumbers).
</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|F_Sharp|F#}}==
Line 1,873 ⟶ 2,670:
20 101010
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 17-10-2016
' compile with: fbc -s console
 
#Define max 92 ' max for Fibonacci number
 
Dim Shared As ULongInt fib(max)
 
fib(0) = 1
fib(1) = 1
 
For x As Integer = 2 To max
fib(x) = fib(x-1) + fib(x-2)
Next
 
Function num2zeck(n As Integer) As String
 
If n < 0 Then
Print "Error: no negative numbers allowed"
Beep : Sleep 5000,1 : End
End If
 
If n < 2 Then Return Str(n)
 
Dim As String zeckendorf
For x As Integer = max To 1 Step -1
If fib(x) <= n Then
zeckendorf = zeckendorf + "1"
n = n - fib(x)
Else
zeckendorf = zeckendorf + "0"
End If
Next
 
return LTrim(zeckendorf, "0") ' get rid of leading zeroes
End Function
 
' ------=< MAIN >=------
 
Dim As Integer x, e
Dim As String zeckendorf
Print "number zeckendorf"
 
For x = 0 To 200000
zeckendorf = num2zeck(x)
If x <= 20 Then Print x, zeckendorf
' check for two consecutive Fibonacci numbers
If InStr(zeckendorf, "11") <> 0 Then
Print " Error: two consecutive Fibonacci numbers "; x, zeckendorf
e = e +1
End If
Next
 
Print
If e = 0 Then
Print " No Zeckendorf numbers with two consecutive Fibonacci numbers found"
Else
Print e; " error(s) found"
End If
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>number zeckendorf
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
 
No Zeckendorf numbers with two consecutive Fibonacci numbers found</pre>
 
=={{header|Go}}==
Line 2,639 ⟶ 3,341:
20 : 101010
</pre>
 
=={{header|Liberty BASIC}}==
''CBTJD'': 2020/03/09
<syntaxhighlight lang="vb">samples = 20
call zecklist samples
 
print "Decimal","Zeckendorf"
for n = 0 to samples
print n, zecklist$(n)
next n
 
Sub zecklist inDEC
dim zecklist$(inDEC)
do
bin$ = dec2bin$(count)
if instr(bin$,"11") = 0 then
zecklist$(found) = bin$
found = found + 1
end if
count = count+1
loop until found = inDEC + 1
End sub
 
function dec2bin$(inDEC)
do
bin$ = str$(inDEC mod 2) + bin$
inDEC = int(inDEC/2)
loop until inDEC = 0
dec2bin$ = bin$
end function</syntaxhighlight>
 
=={{header|Lingo}}==
Line 2,958 ⟶ 3,630:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">zeckendorf[0] = 0;
ZeckendorfRepresentation[0] = 0;
zeckendorf[n_Integer] :=
 
10^(# - 1) + zeckendorf[n - Fibonacci[# + 1]] &@
ZeckendorfRepresentation[n_Integer?Positive]:=
LengthWhile[
NumberDecompose[n, Reverse@Fibonacci@Range[2,1000]] // FromDigits
Fibonacci /@
 
Range[2, Ceiling@Log[GoldenRatio, n Sqrt@5]], # <= n &];
zeckendorfZeckendorfRepresentation /@ Range[0, 20]</syntaxhighlight>
{{Out}}
<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}}==
<syntaxhighlight lang="miniscript">
fibonacci = function(val)
if val < 1 then return []
fib = []
a = 1; b = 2
while a <= val
fib.insert(0, a)
next = a + b
a = b
b = next
end while
return fib
end function
 
zeckendorf = function(val)
seq = fibonacci(val)
s = ""
for i in seq
onOff = val >= i and (s == "" or s[-1] == "0")
s += str(onOff)
val -= (i*onOff)
end for
return s
end function
 
for i in range(1, 20)
print [i, zeckendorf(i)]
end for
</syntaxhighlight>
{{out}}
<pre>[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|Nim}}==
Line 3,012 ⟶ 3,802:
19 101001
20 101010</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let zeck n =
let rec enc x s = function
| h :: t when h <= x -> enc (x - h) (s ^ "1") t
| _ :: t -> enc x (s ^ "0") t
| _ -> s
and fib b a l =
if b > n
then enc (n - a) "1" l
else fib (b + a) b (a :: l)
in
if n = 0 then "0" else fib 2 1 []
 
let () =
for i = 0 to 20 do Printf.printf "%3u:%8s\n" i (zeck i) done</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|PARI/GP}}==
Line 3,577 ⟶ 4,407:
101010
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure.s zeck(n.i)
Dim f.i(1) : Define i.i=1, o$
f(0)=1 : f(1)=1
While f(i)<n
i+1 : ReDim f(ArraySize(f())+1) : f(i)=f(i-1)+f(i-2)
Wend
For i=i To 1 Step -1
If n>=f(i) : o$+"1" : n-f(i) : Else : o$+"0" : EndIf
Next
If Len(o$)>1 : o$=LTrim(o$,"0") : EndIf
ProcedureReturn o$
EndProcedure
 
Define n.i, t$
OpenConsole("Zeckendorf number representation")
PrintN(~"\tNr.\tZeckendorf")
For n=0 To 20
t$=zeck(n)
If FindString(t$,"11")
PrintN("Error: n= "+Str(n)+~"\tZeckendorf= "+t$)
Break
Else
PrintN(~"\t"+RSet(Str(n),3," ")+~"\t"+RSet(t$,7," "))
EndIf
Next
Input()</syntaxhighlight>
{{out}}
<pre> Nr. Zeckendorf
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|Python}}==
Line 3,766 ⟶ 4,545:
 
21 times
[ i^ dup echo
say " -> "
n->z dup binecho
say " -> "
z->n echo cr ]</syntaxhighlight>
 
{{Out}}
Line 4,191 ⟶ 4,970:
19: 101001
20: 101010</pre>
 
=={{header|Rust}}==
{{trans|C#}}
<syntaxhighlight lang="Rust">
use std::collections::VecDeque;
 
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
 
fn zeckendorf(num: u32) -> String {
let mut fibonacci_numbers = VecDeque::new();
let mut fib_position = 2;
let mut current_fibonacci_num = fibonacci(fib_position);
 
while current_fibonacci_num <= num {
fibonacci_numbers.push_front(current_fibonacci_num);
fib_position += 1;
current_fibonacci_num = fibonacci(fib_position);
}
 
let mut temp = num;
let mut output = String::new();
 
for item in fibonacci_numbers {
if item <= temp {
output.push('1');
temp -= item;
} else {
output.push('0');
}
}
 
output
}
 
fn main() {
for i in 1..=20 {
let zeckendorf_representation = zeckendorf(i);
println!("{} : {}", i, zeckendorf_representation);
}
}
</syntaxhighlight>
{{out}}
<pre>
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|Scala}}==
Line 4,390 ⟶ 5,240:
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|Sinclair ZX81 BASIC}}==
Works on the 1k RAM model, albeit without much room for manoeuvre. (You'd like the Zeckendorf numbers further over towards the right-hand side of the screen? Sorry, can't spare the video RAM.) If you have 2k or more, you can replace the constant 6 with some higher value wherever it occurs in the program and enable yourself to represent bigger numbers in Zeckendorf form.
<syntaxhighlight lang="basic"> 10 DIM F(6)
20 LET F(1)=1
30 LET F(2)=2
40 FOR I=3 TO 6
50 LET F(I)=F(I-2)+F(I-1)
60 NEXT I
70 FOR I=0 TO 20
80 LET Z$=""
90 LET S$=" "
100 LET Z=I
110 FOR J=6 TO 1 STEP -1
120 IF J=1 THEN LET S$="0"
130 IF Z<F(J) THEN GOTO 180
140 LET Z$=Z$+"1"
150 LET Z=Z-F(J)
160 LET S$="0"
170 GOTO 190
180 LET Z$=Z$+S$
190 NEXT J
200 PRINT I;" ";
210 IF I<10 THEN PRINT " ";
220 PRINT Z$
230 NEXT I</syntaxhighlight>
{{out}}
<pre>0 0
Line 4,583 ⟶ 5,385:
20: 101010</pre>
 
=={{header|uBasic/4tHUNIX Shell}}==
<syntaxhighlight lang="textsh">Forset x-- = 0 to 20 ' Print Zeckendorf numbers 0 -2 201
x=-1
Print x,
while [ $((x += 1)) -le 20 ]
Push x : Gosub _Zeckendorf ' get Zeckendorf number repres.
do
Print ' terminate line
[ $x -gt $1 ] && set -- $(($2 + $1)) "$@"
Next
n=$x zeck=''
 
for fib
End
do
 
zeck=$zeck$((n >= fib && (n -= fib) + 1))
_Fibonacci
done
Push Tos() ' duplicate TOS()
echo "$x: ${zeck#0}"
@(0) = 0 ' This function returns the
done</syntaxhighlight>
@(1) = 1 ' Fibonacci number which is smaller
{{out}}
' or equal to TOS()
<pre>0: 0
Do While @(1) < Tos() + 1
Push (@(1))
@(1) = @(0) + @(1) ' get next Fibonacci number
@(0) = Pop()
Loop ' loop if not exceeded TOS()
 
Gosub _Drop ' clear TOS()
Push @(0) ' return Fibonacci number
Return
 
_Zeckendorf
GoSub _Fibonacci ' This function breaks TOS() up
Print Tos(); ' into its Zeckendorf components
Push -(Pop() - Pop()) ' first digit is always there
' the remainder to resolve
Do While Tos() ' now go for the next digits
GoSub _Fibonacci
Print " + ";Tos(); ' print the next digit
Push -(Pop() - Pop())
Loop
 
Gosub _Drop ' clear TOS()
Return ' and return
 
_Drop
If Pop()%1 = 0 Then Return ' This function clears TOS()</syntaxhighlight>
Output:
<pre>0 0
1 1
2 2
3 3
4 3 + 1
5 5
6 5 + 1
7 5 + 2
8 8
9 8 + 1
10 8 + 2
11 8 + 3
12 8 + 3 + 1
13 13
14 13 + 1
15 13 + 2
16 13 + 3
17 13 + 3 + 1
18 13 + 5
19 13 + 5 + 1
20 13 + 5 + 2
 
0 OK, 0:901</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function zeckendorf(ByVal n As Integer) As Integer
Dim r As Integer: r = 0
Dim c As Integer
Dim fib As New Collection
fib.Add 1
fib.Add 1
Do While fib(fib.Count) < n
fib.Add fib(fib.Count - 1) + fib(fib.Count)
Loop
For i = fib.Count To 2 Step -1
c = n >= fib(i)
r = r + r - c
n = n + c * fib(i)
Next i
zeckendorf = r
End Function
Public Sub main()
Dim i As Integer
For i = 0 To 20
Debug.Print Format(i, "@@"); ":"; Format(WorksheetFunction.Dec2Bin(zeckendorf(i)), "@@@@@@@")
Next i
End Sub</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|VBScript}}==
<syntaxhighlight lang="vb">
Function Zeckendorf(n)
num = n
Set fibonacci = CreateObject("System.Collections.Arraylist")
fibonacci.Add 1 : fibonacci.Add 2
i = 1
Do While fibonacci(i) < num
fibonacci.Add fibonacci(i) + fibonacci(i-1)
i = i + 1
Loop
tmp = ""
For j = fibonacci.Count-1 To 0 Step -1
If fibonacci(j) <= num And (tmp = "" Or Left(tmp,1) <> "1") Then
tmp = tmp & "1"
num = num - fibonacci(j)
Else
tmp = tmp & "0"
End If
Next
Zeckendorf = CLng(tmp)
End Function
 
'testing the function
For k = 0 To 20
WScript.StdOut.WriteLine k & ": " & Zeckendorf(k)
Next
</syntaxhighlight>
 
{{Out}}
<pre>
0: 0
1: 1
2: 10
Line 4,745 ⟶ 5,419:
18: 101000
19: 101001
20: 101010</pre>
</pre>
 
=={{header|V (Vlang)}}==
Line 4,809 ⟶ 5,482:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var LIMIT = 46 // to stay within range of signed 32 bit integer
Line 4,921 ⟶ 5,594:
20: 101010
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">sub Zeckendorf(n)
local i, n$, c
do
n$ = bin$(i)
if not instr(n$,"11") then
print c,":\t",n$
if c = n break
c = c + 1
end if
i = i + 1
loop
end sub
 
Zeckendorf(20)
</syntaxhighlight>
 
=={{header|zkl}}==
Line 4,976 ⟶ 5,631:
</pre>
 
{{omit from|PL/0}}
{{omit from|Tiny BASIC}}
[[Category: Bitwise operations]]
9,485

edits