Zeckendorf arithmetic: Difference between revisions

m
 
(40 intermediate revisions by 18 users not shown)
Line 70:
[http://arxiv.org/pdf/1207.4497.pdf Efficient algorithms for Zeckendorf arithmetic] is interesting. The sections on addition and subtraction are particularly relevant for this task.
 
=={{header|C++11l}}==
{{works withtrans|C++11Python}}
 
<lang cpp>// For a class N which implements Zeckendorf numbers:
<syntaxhighlight lang="11l">T Zeckendorf
// I define an increment operation ++()
Int dLen
// I define a comparison operation <=(other N)
dVal = 0
// I define an addition operation +=(other N)
 
// I define a subtraction operation -=(other N)
F (x = ‘0’)
// Nigel Galloway October 28th., 2012
V q = 1
#include <iostream>
V i = x.len - 1
enum class zd {N00,N01,N10,N11};
.dLen = i I/ 2
class N {
L i >= 0
private:
.dVal = .dVal + (x[i].code - ‘0’.code) * q
int dVal = 0, dLen;
q = q * 2
void _a(int i) {
for (;; i++) {= i - 1
 
if (dLen < i) dLen = i;
F a(n)
switch ((zd)((dVal >> (i*2)) & 3)) {
V i = n
case zd::N00: case zd::N01: return;
L
case zd::N10: if (((dVal >> ((i+1)*2)) & 1) != 1) return;
I dVal += (1.dLen << (i*2+1)); return;
case zd::N11: dVal &= ~(3.dLen << (i*2));= _b((i+1)*2);
V j = (.dVal >> (i * 2)) [&] 3
}}}
I j == 0 | j == 1
void _b(int pos) {
if (pos == 0) {++*this; return;} R
if (((dVal >> pos) & 1)I j == 0) {2
I (.dVal >> ((i += 1) <<* pos;2) [&] 1) != 1
_a(pos/2); R
if (pos > .dVal = .dVal + (1) _a<< ((pos/i * 2)- + 1);)
} else { R
dVal &= ~(1 <<I j == pos);3
_b V temp = 3 << (posi +* 12);
_b(pos - ((pos > 1 temp = temp (+)? 2:-1));
.dVal = .dVal [&] temp
}}
void _c .b(int(i pos+ 1) {* 2)
if (((dVal >> pos) & 1)i == 1)i {dVal+ &= ~(1 << pos); return;}
 
_c(pos + 1);
F if b(pos > 0) _b(pos - 1); else ++*this;
return; I pos == 0
.inc()
}
R
public:
N I (char.dVal const*>> xpos) [&] 1 == "0") {
int i = 0, q .dVal = .dVal + (1; << pos)
for (; x[i] > 0; i++.a(Int(pos / 2));
I pos > 1
for (dLen = --i/2; i >= 0; i--) {dVal+=(x[i]-48)*q; q*=2;
.a(Int(pos / 2) - 1)
}}
E
const N& operator++() {dVal += 1; _a(0); return *this;}
V temp = 1 << pos
const N& operator+=(const N& other) {
temp = temp (+) -1
for (int GN = 0; GN < (other.dLen + 1) * 2; GN++) if ((other.dVal >> GN) & 1 == 1) _b(GN);
.dVal = .dVal [&] temp
return *this;
.b(pos + 1)
}
.b(pos - (I pos > 1 {2} E 1))
const N& operator-=(const N& other) {
 
for (int GN = 0; GN < (other.dLen + 1) * 2; GN++) if ((other.dVal >> GN) & 1 == 1) _c(GN);
F c(pos)
for (;((dVal >> dLen*2) & 3) == 0 or dLen == 0; dLen--);
I (.dVal >> pos) [&] 1 == 1
return *this;
V temp = 1 << pos
}
const N& operator* temp =(const N&temp other(+) {-1
N Na = other, Nb .dVal = other,.dVal Nt,[&] Nr;temp
R
for (int i = 0; i <= (dLen + 1) * 2; i++) {
if .c(((dValpos >> i) &+ 1) > 0) Nr += Nb;
NtI =pos Nb;> Nb += Na; Na = Nt;0
.b(pos - 1)
}
return *this = Nr;E
.inc()
}
 
const bool operator<=(const N& other) const {return dVal <= other.dVal;}
F inc() -> Void
friend std::ostream& operator<<(std::ostream&, const N&);
.dVal = .dVal + 1
};
.a(0)
N operator "" N(char const* x) {return N(x);}
 
std::ostream &operator<<(std::ostream &os, const N &G) {
F +(rhs)
const static std::string dig[] {"00","01","10"}, dig1[] {"","1","10"};
if (G.dVal == 0) returnV oscopy <<= "0";(.)
V rhs_dVal = rhs.dVal
os << dig1[(G.dVal >> (G.dLen*2)) & 3];
for (int i = G.dLen-1;V ilimit >= 0; i--) os << dig[(Grhs.dValdLen >>+ (i*2)1) &* 3];2
L(gn) 0 .< limit
return os;
I ((rhs_dVal >> gn) [&] 1) == 1
}
copy.b(gn)
</lang>
R copy
===Testing===
 
The following tests addtition:
F -(rhs)
<lang cpp>int main(void) {
V copy = (.)
N G;
V rhs_dVal = rhs.dVal
G = 10N;
V limit = (rhs.dLen + 1) * 2
G += 10N;
L(gn) 0 .< limit
std::cout << G << std::endl;
I (rhs_dVal >> gn) [&] 1 == 1
G += 10N;
copy.c(gn)
std::cout << G << std::endl;
L (((copy.dVal >> ((copy.dLen * 2) [&] 31)) [&] 3) == 0) | (copy.dLen == 0)
G += 1001N;
copy.dLen = copy.dLen - 1
std::cout << G << std::endl;
G += 1000N; R copy
 
std::cout << G << std::endl;
G +=F 10101N;*(rhs)
V na = copy(rhs)
std::cout << G << std::endl;
V nb = copy(rhs)
return 0;
V nr = Zeckendorf()
}</lang>
V dVal = .dVal
L(i) 0 .< (.dLen + 1) * 2
I ((dVal >> i) [&] 1) > 0
nr = nr + nb
V nt = copy(nb)
nb = nb + na
na = copy(nt)
R nr
 
F String()
V dig = [‘00’, ‘01’, ‘10’]
V dig1 = [‘’, ‘1’, ‘10’]
 
I .dVal == 0
R ‘0’
V idx = (.dVal >> ((.dLen * 2) [&] 31)) [&] 3
String sb = dig1[idx]
V i = .dLen - 1
L i >= 0
idx = (.dVal >> (i * 2)) [&] 3
sb ‘’= dig[idx]
i = i - 1
R sb
 
print(‘Addition:’)
V g = Zeckendorf(‘10’)
g = g + Zeckendorf(‘10’)
print(g)
g = g + Zeckendorf(‘10’)
print(g)
g = g + Zeckendorf(‘1001’)
print(g)
g = g + Zeckendorf(‘1000’)
print(g)
g = g + Zeckendorf(‘10101’)
print(g)
print()
 
print(‘Subtraction:’)
g = Zeckendorf(‘1000’)
g = g - Zeckendorf(‘101’)
print(g)
g = Zeckendorf(‘10101010’)
g = g - Zeckendorf(‘1010101’)
print(g)
print()
 
print(‘Multiplication:’)
g = Zeckendorf(‘1001’)
g = g * Zeckendorf(‘101’)
print(g)
g = Zeckendorf(‘101010’)
g = g + Zeckendorf(‘101’)
print(g)</syntaxhighlight>
 
{{out}}
<pre>
Addition:
101
1001
Line 169 ⟶ 225:
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100
</pre>
 
The following tests subtraction:
=={{header|C}}==
<lang cpp>int main(void) {
{{trans|D}}
N G;
<syntaxhighlight lang="c">#include <stdbool.h>
G = 1000N;
#include <stdio.h>
G -= 101N;
#include <string.h>
std::cout << G << std::endl;
 
G = 10101010N;
int inv(int a) {
G -= 1010101N;
return a ^ -1;
std::cout << G << std::endl;
}
return 0;
 
}</lang>
struct Zeckendorf {
int dVal, dLen;
};
 
void a(struct Zeckendorf *self, int n) {
void b(struct Zeckendorf *, int); // forward declare
 
int i = n;
while (true) {
if (self->dLen < i) self->dLen = i;
int j = (self->dVal >> (i * 2)) & 3;
switch (j) {
case 0:
case 1:
return;
case 2:
if (((self->dVal >> ((i + 1) * 2)) & 1) != 1) return;
self->dVal += 1 << (i * 2 + 1);
return;
case 3:
self->dVal = self->dVal & inv(3 << (i * 2));
b(self, (i + 1) * 2);
break;
default:
break;
}
i++;
}
}
 
void b(struct Zeckendorf *self, int pos) {
void increment(struct Zeckendorf *); // forward declare
 
if (pos == 0) {
increment(self);
return;
}
if (((self->dVal >> pos) & 1) == 0) {
self->dVal += 1 << pos;
a(self, pos / 2);
if (pos > 1) a(self, pos / 2 - 1);
} else {
self->dVal = self->dVal & inv(1 << pos);
b(self, pos + 1);
b(self, pos - (pos > 1 ? 2 : 1));
}
}
 
void c(struct Zeckendorf *self, int pos) {
if (((self->dVal >> pos) & 1) == 1) {
self->dVal = self->dVal & inv(1 << pos);
return;
}
c(self, pos + 1);
if (pos > 0) {
b(self, pos - 1);
} else {
increment(self);
}
}
 
struct Zeckendorf makeZeckendorf(char *x) {
struct Zeckendorf z = { 0, 0 };
int i = strlen(x) - 1;
int q = 1;
 
z.dLen = i / 2;
while (i >= 0) {
z.dVal += (x[i] - '0') * q;
q *= 2;
i--;
}
 
return z;
}
 
void increment(struct Zeckendorf *self) {
self->dVal++;
a(self, 0);
}
 
void addAssign(struct Zeckendorf *self, struct Zeckendorf rhs) {
int gn;
for (gn = 0; gn < (rhs.dLen + 1) * 2; gn++) {
if (((rhs.dVal >> gn) & 1) == 1) {
b(self, gn);
}
}
}
 
void subAssign(struct Zeckendorf *self, struct Zeckendorf rhs) {
int gn;
for (gn = 0; gn < (rhs.dLen + 1) * 2; gn++) {
if (((rhs.dVal >> gn) & 1) == 1) {
c(self, gn);
}
}
while ((((self->dVal >> self->dLen * 2) & 3) == 0) || (self->dLen == 0)) {
self->dLen--;
}
}
 
void mulAssign(struct Zeckendorf *self, struct Zeckendorf rhs) {
struct Zeckendorf na = rhs;
struct Zeckendorf nb = rhs;
struct Zeckendorf nr = makeZeckendorf("0");
struct Zeckendorf nt;
int i;
 
for (i = 0; i < (self->dLen + 1) * 2; i++) {
if (((self->dVal >> i) & 1) > 0) addAssign(&nr, nb);
nt = nb;
addAssign(&nb, na);
na = nt;
}
 
*self = nr;
}
 
void printZeckendorf(struct Zeckendorf z) {
static const char *const dig[3] = { "00", "01", "10" };
static const char *const dig1[3] = { "", "1", "10" };
 
if (z.dVal == 0) {
printf("0");
return;
} else {
int idx = (z.dVal >> (z.dLen * 2)) & 3;
int i;
 
printf(dig1[idx]);
for (i = z.dLen - 1; i >= 0; i--) {
idx = (z.dVal >> (i * 2)) & 3;
printf(dig[idx]);
}
}
}
 
int main() {
struct Zeckendorf g;
 
printf("Addition:\n");
g = makeZeckendorf("10");
addAssign(&g, makeZeckendorf("10"));
printZeckendorf(g);
printf("\n");
addAssign(&g, makeZeckendorf("10"));
printZeckendorf(g);
printf("\n");
addAssign(&g, makeZeckendorf("1001"));
printZeckendorf(g);
printf("\n");
addAssign(&g, makeZeckendorf("1000"));
printZeckendorf(g);
printf("\n");
addAssign(&g, makeZeckendorf("10101"));
printZeckendorf(g);
printf("\n\n");
 
printf("Subtraction:\n");
g = makeZeckendorf("1000");
subAssign(&g, makeZeckendorf("101"));
printZeckendorf(g);
printf("\n");
g = makeZeckendorf("10101010");
subAssign(&g, makeZeckendorf("1010101"));
printZeckendorf(g);
printf("\n\n");
 
printf("Multiplication:\n");
g = makeZeckendorf("1001");
mulAssign(&g, makeZeckendorf("101"));
printZeckendorf(g);
printf("\n");
g = makeZeckendorf("101010");
addAssign(&g, makeZeckendorf("101"));
printZeckendorf(g);
printf("\n");
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
</pre>
The following tests multiplication:
<lang cpp>
int main(void) {
N G = 1001N;
G *= 101N;
std::cout << G << std::endl;
 
Multiplication:
G = 101010N;
G += 101N;
std::cout << G << std::endl;
return 0;
}</lang>
{{out}}
<pre>
1000100
1000100</pre>
</pre>
 
=={{header|C#|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
 
Line 394 ⟶ 632:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 410 ⟶ 648:
1000100
1000100</pre>
 
=={{header|C++}}==
{{works with|C++11}}
<syntaxhighlight lang="cpp">// For a class N which implements Zeckendorf numbers:
// I define an increment operation ++()
// I define a comparison operation <=(other N)
// I define an addition operation +=(other N)
// I define a subtraction operation -=(other N)
// Nigel Galloway October 28th., 2012
#include <iostream>
enum class zd {N00,N01,N10,N11};
class N {
private:
int dVal = 0, dLen;
void _a(int i) {
for (;; i++) {
if (dLen < i) dLen = i;
switch ((zd)((dVal >> (i*2)) & 3)) {
case zd::N00: case zd::N01: return;
case zd::N10: if (((dVal >> ((i+1)*2)) & 1) != 1) return;
dVal += (1 << (i*2+1)); return;
case zd::N11: dVal &= ~(3 << (i*2)); _b((i+1)*2);
}}}
void _b(int pos) {
if (pos == 0) {++*this; return;}
if (((dVal >> pos) & 1) == 0) {
dVal += 1 << pos;
_a(pos/2);
if (pos > 1) _a((pos/2)-1);
} else {
dVal &= ~(1 << pos);
_b(pos + 1);
_b(pos - ((pos > 1)? 2:1));
}}
void _c(int pos) {
if (((dVal >> pos) & 1) == 1) {dVal &= ~(1 << pos); return;}
_c(pos + 1);
if (pos > 0) _b(pos - 1); else ++*this;
return;
}
public:
N(char const* x = "0") {
int i = 0, q = 1;
for (; x[i] > 0; i++);
for (dLen = --i/2; i >= 0; i--) {dVal+=(x[i]-48)*q; q*=2;
}}
const N& operator++() {dVal += 1; _a(0); return *this;}
const N& operator+=(const N& other) {
for (int GN = 0; GN < (other.dLen + 1) * 2; GN++) if ((other.dVal >> GN) & 1 == 1) _b(GN);
return *this;
}
const N& operator-=(const N& other) {
for (int GN = 0; GN < (other.dLen + 1) * 2; GN++) if ((other.dVal >> GN) & 1 == 1) _c(GN);
for (;((dVal >> dLen*2) & 3) == 0 or dLen == 0; dLen--);
return *this;
}
const N& operator*=(const N& other) {
N Na = other, Nb = other, Nt, Nr;
for (int i = 0; i <= (dLen + 1) * 2; i++) {
if (((dVal >> i) & 1) > 0) Nr += Nb;
Nt = Nb; Nb += Na; Na = Nt;
}
return *this = Nr;
}
const bool operator<=(const N& other) const {return dVal <= other.dVal;}
friend std::ostream& operator<<(std::ostream&, const N&);
};
N operator "" N(char const* x) {return N(x);}
std::ostream &operator<<(std::ostream &os, const N &G) {
const static std::string dig[] {"00","01","10"}, dig1[] {"","1","10"};
if (G.dVal == 0) return os << "0";
os << dig1[(G.dVal >> (G.dLen*2)) & 3];
for (int i = G.dLen-1; i >= 0; i--) os << dig[(G.dVal >> (i*2)) & 3];
return os;
}
</syntaxhighlight>
===Testing===
The following tests addtition:
<syntaxhighlight lang="cpp">int main(void) {
N G;
G = 10N;
G += 10N;
std::cout << G << std::endl;
G += 10N;
std::cout << G << std::endl;
G += 1001N;
std::cout << G << std::endl;
G += 1000N;
std::cout << G << std::endl;
G += 10101N;
std::cout << G << std::endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>
101
1001
10101
100101
1010000
</pre>
The following tests subtraction:
<syntaxhighlight lang="cpp">int main(void) {
N G;
G = 1000N;
G -= 101N;
std::cout << G << std::endl;
G = 10101010N;
G -= 1010101N;
std::cout << G << std::endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>
1
1000000
</pre>
The following tests multiplication:
<syntaxhighlight lang="cpp">
int main(void) {
N G = 1001N;
G *= 101N;
std::cout << G << std::endl;
 
G = 101010N;
G += 101N;
std::cout << G << std::endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>
1000100
1000100
</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
int inv(int a) {
Line 584 ⟶ 956:
g += "101".Z;
writeln(g);
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 600 ⟶ 972:
1000100
1000100</pre>
 
=={{header|Dart}}==
{{trans|Kotlin}}
<syntaxhighlight lang="Dart">
class Zeckendorf {
int dVal = 0;
int dLen = 0;
 
Zeckendorf(String x) {
var q = 1;
var i = x.length - 1;
dLen = i ~/ 2;
while (i >= 0) {
dVal += (x[i].codeUnitAt(0) - '0'.codeUnitAt(0)) * q;
q *= 2;
i--;
}
}
 
void a(int n) {
var i = n;
while (true) {
if (dLen < i) dLen = i;
var j = (dVal >> (i * 2)) & 3;
switch (j) {
case 0:
case 1:
return;
case 2:
if (((dVal >> ((i + 1) * 2)) & 1) != 1) return;
dVal += 1 << (i * 2 + 1);
return;
case 3:
dVal &= ~(3 << (i * 2));
b((i + 1) * 2);
break;
}
i++;
}
}
 
void b(int pos) {
if (pos == 0) {
this.increment();
return;
}
if (((dVal >> pos) & 1) == 0) {
dVal += 1 << pos;
a(pos ~/ 2);
if (pos > 1) a(pos ~/ 2 - 1);
} else {
dVal &= ~(1 << pos);
b(pos + 1);
b(pos - (pos > 1 ? 2 : 1));
}
}
 
void c(int pos) {
if (((dVal >> pos) & 1) == 1) {
dVal &= ~(1 << pos);
return;
}
c(pos + 1);
if (pos > 0)
b(pos - 1);
else
this.increment();
}
 
Zeckendorf increment() {
dVal += 1;
a(0);
return this;
}
 
void operator + (Zeckendorf other) {
for (var gn = 0; gn < (other.dLen + 1) * 2; gn++) {
if (((other.dVal >> gn) & 1) == 1) b(gn);
}
}
 
void operator - (Zeckendorf other) {
for (var gn = 0; gn < (other.dLen + 1) * 2; gn++) {
if (((other.dVal >> gn) & 1) == 1) c(gn);
}
while (dLen > 0 && (((dVal >> dLen * 2) & 3) == 0)) dLen--;
}
 
 
void operator * (Zeckendorf other) {
var na = other.copy();
var nb = other.copy();
Zeckendorf nt;
var nr = Zeckendorf("0");
for (var i = 0; i <= (dLen + 1) * 2; i++) {
if (((dVal >> i) & 1) > 0) nr + nb;
nt = nb.copy();
nb + na;
na = nt.copy();
}
dVal = nr.dVal;
dLen = nr.dLen;
}
 
int compareTo(Zeckendorf other) {
return dVal.compareTo(other.dVal);
}
 
@override
String toString() {
if (dVal == 0) return "0";
var sb = StringBuffer(dig1[(dVal >> (dLen * 2)) & 3]);
for (var i = dLen - 1; i >= 0; i--) {
sb.write(dig[(dVal >> (i * 2)) & 3]);
}
return sb.toString();
}
 
Zeckendorf copy() {
var z = Zeckendorf("0");
z.dVal = dVal;
z.dLen = dLen;
return z;
}
 
static final List<String> dig = ["00", "01", "10"];
static final List<String> dig1 = ["", "1", "10"];
}
 
void main() {
print("Addition:");
var g = Zeckendorf("10");
g + Zeckendorf("10");
print(g);
g + Zeckendorf("10");
print(g);
g + Zeckendorf("1001");
print(g);
g + Zeckendorf("1000");
print(g);
g + Zeckendorf("10101");
print(g);
 
print("\nSubtraction:");
g = Zeckendorf("1000");
g - Zeckendorf("101");
print(g);
g = Zeckendorf("10101010");
g - Zeckendorf("1010101");
print(g);
 
print("\nMultiplication:");
g = Zeckendorf("1001");
g * Zeckendorf("101");
print(g);
g = Zeckendorf("101010");
g + Zeckendorf("101");
print(g);
}
</syntaxhighlight>
{{out}}
<pre>
Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100
 
</pre>
 
 
=={{header|Elena}}==
{{trans|C++}}
ELENA 36.4x :
<langsyntaxhighlight lang="elena">import extensions.;
 
const dig = (new string[]{"00","01","10").};
const dig1 = (new string[]{"","1","10").};
sealed struct ZeckendorfNumber :: BaseNumber
{
int dVal.
int dLen.
clone
= ZeckendorfNumber newInternal(dVal,dLen).
cast n(literal s)
[
int q := 1.
int i := s length - 1.
dLen := i / 2.
 
sealed struct ZeckendorfNumber
int x := dLen.
{
 
int dVal := 0.;
int dLen;
clone()
= ZeckendorfNumber.newInternal(dVal,dLen);
cast n(string s)
{
int i := s.Length - 1;
int q := 1;
dLen := i / 2;
dVal := 0;
while (i >= 0)
[{
dVal += ((intConvertor .convert(s[i]) - 48) * q).;
q *= 2;
qi *-= 2. 1
}
i -= 1.}
].
internal readContent(ref int val, ref int len)
]
{
internal readContent(ref<int> val, ref<int>:= len)dVal;
len := dLen;
[
}
val value := dVal.
len value := dLen.
private a(int n)
]
{
sealed private a( int i := n);
 
[
int i := n.
while (true)
[{
if (dLen < i)
[{
dLen := i.
].};
(int v := (dVal >>$shr (i * 2)) && 3) =>;
v => 0 [ ^ self ];
10 [{ ^ self ];}
21 [{ ^ self }
ifnot ((dVal >> ((i + 1) * 2)) allMask:1){
[ifnot ((dVal $shr ((i + 1) * 2)).allMask(1))
^ self.{
]. ^ self
};
dVal += (1 << (i*2 + 1)).
^dVal self.+= (1 $shl (i*2 + 1));
];
3 [ ^ self
dVal := dVal && ((3 << i*2) inverted).}
3 {
selfint tmp := 3 $shl b((i+1) * 2).;
] tmp := tmp.bxor(-1);
dVal := dVal & tmp;
i += 1.
] self.b((i+1)*2)
] };
inc i += 1
[ }
dVal += 1.}
self a(0).
]inc()
{
dVal += 1;
self.a(0)
}
private b(int pos)
[{
if (pos == 0) [{ ^ self .inc() ].};
ifnot((dVal >>$shr pos) .allMask:(1))
[{
dVal += (1 <<$shl pos).;
self .a(pos / 2).;
if (pos > 1) [{ self .a((pos / 2) - 1) ]}
];}
[else
{
dVal := dVal && (1 << pos) inverted.
selfdVal b(pos:= +dVal & (1 $shl pos).BInverted;
self .b(pos - (pos >+ 1) iif(2,1)).;
int arg := pos - ((pos > 1) ? 2 : 1);
].
self.b(/*pos - ((pos > 1) ? 2 : 1)*/arg)
]
}
}
private c(IntNumber pos)
 
[
private if c((dVal >>int pos) allMask:1)
[{
if ((dVal := dVal &&$shr pos).allMask(1 << pos) inverted.)
{
int tmp := 1 $shl pos;
tmp := tmp.bxor(-1);
dVal := dVal & tmp;
^ self
].};
self .c(pos + 1).;
if (pos > 0)
[{
self .b(pos - 1).
];}
[else
self inc.{
] self.inc()
}
]
}
internal constructor sum(ZeckendorfNumber n, ZeckendorfNumber m)
[{
int mVal := 0.;
int mLen := 0.;
n .readContent(&dValref int v, &dLenref int l).;
m .readContent(&ref mVal, &ref mLen).;
var nndVal := n.v;
var mmdLen := m.l;
 
for(int GN := 0; GN < till((mLen + 1) * 2); do(:GN += 1)
[{
if ((mVal shiftRight:GN;$shr GN).allMask:(1))
[{
self .b(GN).
].}
].}
]}
internal constructor difference(ZeckendorfNumber n, ZeckendorfNumber m)
[{
int mVal := 0.;
int mLen := 0.;
n .readContent(&dValref int v, &dLenref int l).;
m .readContent(&ref mVal, &ref mLen).;
0dVal till((mLen + 1) * 2) do(:GN) = v;
[dLen := l;
if (mVal shiftRight:GN; allMask:1)
for(int GN := 0; GN < (mLen + 1) * 2; GN += 1) [
self c(GN).{
]if ((mVal $shr GN).allMask(1))
]. {
self.c(GN)
}
while ((((dVal >> (dLen*2)) && 3) == 0) || (dLen == 0))
[};
dLen -= 1.
while (((dVal $shr (dLen*2)) & 3) == 0 || dLen == 0)
].
] {
dLen -= 1
}
}
internal constructor product(ZeckendorfNumber n, ZeckendorfNumber m)
[{
n .readContent(&dValref int v, &dLenref int l).;
auto NadVal := m.v;
auto NbdLen := m.l;
auto Nr := 0n.
autoZeckendorfNumber NtNa := 0n.m;
ZeckendorfNumber Nb := m;
0ZeckendorfNumber to((dLenNr +:= 1) * 2) do(:i)0n;
[ZeckendorfNumber Nt := 0n;
if (((dVal shiftRight:i) && 1) > 0)
for(int i := 0; i < (dLen + 1) * 2; i += 1) [
{
if (((dVal $shr i) & 1) > 0)
{
Nr += Nb
].};
Nt := Nb.;
Nb += Na.;
Na := Nt.
].};
Nr .readContent(&dValref v, &dLenref l).;
]
dVal := v;
dLen := l;
}
internal constructor newInternal(int v, int l)
[{
dVal := v.;
dLen := l.
]}
string toPrintable()
T<literal> literal
[{
if (dVal == 0)
[{ ^ "0". ].};
literalstring s := dig1[(dVal >>$shr (dLen * 2)) && 3].;
int i := dLen - 1.;
while (i >= 0)
[{
s := s + dig[(dVal >>$shr (i * 2)) && 3].;
i-=1
].};
^ s.
]}
add(ZeckendorfNumber n)
= ZeckendorfNumber .sum(self, n).;
subtract(ZeckendorfNumber n)
= ZeckendorfNumber .difference(self, n).;
multiply(ZeckendorfNumber n)
= ZeckendorfNumber .product(self, n).;
}
 
public program()
{
[
console .printLine("Addition:").;
var n := 10n.;
n += 10n. ;
console .printLine(n).;
n += 10n. ;
console .printLine(n).;
n += 1001n. ;
console .printLine(n).;
n += 1000n. ;
console .printLine(n).;
n += 10101n. ;
console .printLine(n).;
console .printLine("Subtraction:").;
n := 1000n.;
n -= 101n.;
console .printLine(n).;
n := 10101010n.;
n -= 1010101n.;
console .printLine(n).;
console .printLine("Multiplication:").;
n := 1001n.;
n *= 101n.;
console .printLine(n).;
n := 101010n.;
n += 101n.;
console .printLine(n).
}</syntaxhighlight>
]</lang>
{{out}}
<pre>
Line 868 ⟶ 1,434:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,051 ⟶ 1,617:
g.PlusAssign(NewZeck("101"))
fmt.Println(g)
}</langsyntaxhighlight>
 
{{out}}
Line 1,070 ⟶ 1,636:
1000100
</pre>
 
=={{header|Haskell}}==
 
We make Zeckendorf numbers first class citizens implementing instances of <code>Eq</code>, <code>Ord</code>, <code>Num</code>, <code>Enum</code>, <code>Real</code> and <code>Integral</code> classes. So everything that could be done with integral numbers is applicable with Zeckendorf numbers.
 
Addition and subtraction are done using cellular automata. Conversion from integers, multiplication and division are implemented via generalized Fibonacci series (Zeckendorf tables).
 
<syntaxhighlight lang="haskell">{-# LANGUAGE LambdaCase #-}
import Data.List (find, mapAccumL)
import Control.Arrow (first, second)
 
-- Generalized Fibonacci series defined for any Num instance, and for Zeckendorf numbers as well.
-- Used to build Zeckendorf tables.
fibs :: Num a => a -> a -> [a]
fibs a b = res
where
res = a : b : zipWith (+) res (tail res)
 
data Fib = Fib { sign :: Int, digits :: [Int]}
 
-- smart constructor
mkFib s ds =
case dropWhile (==0) ds of
[] -> 0
ds -> Fib s (reverse ds)
 
-- Textual representation
instance Show Fib where
show (Fib s ds) = sig s ++ foldMap show (reverse ds)
where sig = \case { -1 -> "-"; s -> "" }
 
-- Equivalence relation
instance Eq Fib where
Fib sa a == Fib sb b = sa == sb && a == b
 
-- Order relation
instance Ord Fib where
a `compare` b =
sign a `compare` sign b <>
case find (/= 0) $ alignWith (-) (digits a) (digits b) of
Nothing -> EQ
Just 1 -> if sign a > 0 then GT else LT
Just (-1) -> if sign a > 0 then LT else GT
 
-- Arithmetic
instance Num Fib where
negate (Fib s ds) = Fib (negate s) ds
abs (Fib s ds) = Fib 1 ds
signum (Fib s _) = fromIntegral s
 
fromInteger n =
case compare n 0 of
LT -> negate $ fromInteger (- n)
EQ -> Fib 0 [0]
GT -> Fib 1 . reverse . fst $ divModFib n 1
 
0 + a = a
a + 0 = a
a + b =
case (sign a, sign b) of
( 1, 1) -> res
(-1, 1) -> b - (-a)
( 1,-1) -> a - (-b)
(-1,-1) -> - ((- a) + (- b))
where
res = mkFib 1 . process $ 0:0:c
c = alignWith (+) (digits a) (digits b)
-- use cellular automata
process =
runRight 3 r2 . runLeftR 3 r2 . runRightR 4 r1
 
0 - a = -a
a - 0 = a
a - b =
case (sign a, sign b) of
( 1, 1) -> res
(-1, 1) -> - ((-a) + b)
( 1,-1) -> a + (-b)
(-1,-1) -> - ((-a) - (-b))
where
res = case find (/= 0) c of
Just 1 -> mkFib 1 . process $ c
Just (-1) -> - (b - a)
Nothing -> 0
c = alignWith (-) (digits a) (digits b)
-- use cellular automata
process =
runRight 3 r2 . runLeftR 3 r2 . runRightR 4 r1 . runRight 3 r3
 
0 * a = 0
a * 0 = 0
1 * a = a
a * 1 = a
a * b =
case (sign a, sign b) of
(1, 1) -> res
(-1, 1) -> - ((-a) * b)
( 1,-1) -> - (a * (-b))
(-1,-1) -> ((-a) * (-b))
where
-- use Zeckendorf table
table = fibs a (a + a)
res = sum $ onlyOnes $ zip (digits b) table
onlyOnes = map snd . filter ((==1) . fst)
 
-- Enumeration
instance Enum Fib where
toEnum = fromInteger . fromIntegral
fromEnum = fromIntegral . toInteger
instance Real Fib where
toRational = fromInteger . toInteger
-- Integral division
instance Integral Fib where
toInteger (Fib s ds) = signum (fromIntegral s) * res
where
res = sum (zipWith (*) (fibs 1 2) (fromIntegral <$> ds))
 
quotRem 0 _ = (0, 0)
quotRem a 0 = error "divide by zero"
quotRem a b = case (sign a, sign b) of
(1, 1) -> first (mkFib 1) $ divModFib a b
(-1, 1) -> second negate . first negate $ quotRem (-a) b
( 1,-1) -> first negate $ quotRem a (-b)
(-1,-1) -> second negate $ quotRem (-a) (-b)
 
------------------------------------------------------------
-- helper funtions
 
-- general division using Zeckendorf table
divModFib :: (Ord a, Num c, Num a) => a -> a -> ([c], a)
divModFib a b = (q, r)
where
(r, q) = mapAccumL f a $ reverse $ takeWhile (<= a) table
table = fibs b (b+b)
f n x = if n < x then (n, 0) else (n - x, 1)
 
-- application of rewriting rules
-- runs window from left to right
runRight n f = go
where
go [] = []
go lst = let (w, r) = splitAt n lst
(h: t) = f w
in h : go (t ++ r)
-- runs window from left to right and reverses the result
runRightR n f = go []
where
go res [] = res
go res lst = let (w, r) = splitAt n lst
(h: t) = f w
in go (h : res) (t ++ r)
 
-- runs reversed window and reverses the result
runLeftR n f = runRightR n (reverse . f . reverse)
 
-- rewriting rules from [C. Ahlbach et. all]
r1 = \case [0,3,0] -> [1,1,1]
[0,2,0] -> [1,0,1]
[0,1,2] -> [1,0,1]
[0,2,1] -> [1,1,0]
[x,0,2] -> [x,1,0]
[x,0,3] -> [x,1,1]
[0,1,2,0] -> [1,0,1,0]
[0,2,0,x] -> [1,0,0,x+1]
[0,3,0,x] -> [1,1,0,x+1]
[0,2,1,x] -> [1,1,0,x ]
[0,1,2,x] -> [1,0,1,x ]
l -> l
 
r2 = \case [0,1,1] -> [1,0,0]
l -> l
 
r3 = \case [1,-1] -> [0,1]
[2,-1] -> [1,1]
[1, 0, 0] -> [0,1,1]
[1,-1, 0] -> [0,0,1]
[1,-1, 1] -> [0,0,2]
[1, 0,-1] -> [0,1,0]
[2, 0, 0] -> [1,1,1]
[2,-1, 0] -> [1,0,1]
[2,-1, 1] -> [1,0,2]
[2, 0,-1] -> [1,1,0]
l -> l
 
alignWith :: (Int -> Int -> a) -> [Int] -> [Int] -> [a]
alignWith f a b = go [] a b
where
go res as [] = ((`f` 0) <$> reverse as) ++ res
go res [] bs = ((0 `f`) <$> reverse bs) ++ res
go res (a:as) (b:bs) = go (f a b : res) as bs</syntaxhighlight>
 
<pre>λ> 15 :: Fib
100010
 
λ> 153 :: Fib
10000010001
 
λ> [1..13] :: [Fib]
[1,10,100,101,1000,1001,1010,10000,10001,10010,10100,10101,100000]
 
λ> 15 + 47 :: Fib
100001010
 
λ> toInteger it
62
 
λ> 15 - 47 :: Fib
-1010100
 
λ> toInteger it
-32
 
λ> 15 * 47 :: Fib
10001000001001
 
λ> toInteger it
705
 
λ> 47 `div` 15 :: Fib
100
 
λ> 47 `mod` 15 :: Fib
10</pre>
 
=={{header|J}}==
Loosely based on the [[#Perl|perl]] implementation:<syntaxhighlight lang="j">zform=: {{ 10 |."1@(#.inv) y }} :. (10#.|."1) NB. use decimal numbers for representation
zinc=: {{ carry ({.,2}.])carry 1,y }}
zdec=: {{ (|.k$0 1),y }.~k=. 1+y i.1 }}
zadd=: {{ x while. 1 e. y do. x=. zinc x [ y=. zdec y end. }}
zsub=: {{ x while. 1 e. y do. x=. zdec x [ y=. zdec y end. }} NB. intended for unsigned arithmetic
zmul=: {{ t=. 0 0 while. 1 e. y do. t=. t zadd x [ y=. zdec y end. }}
zdiv=: {{ t=. 0 0 while. x zge y do. t=. zinc t [ x=. x zsub y end. }} NB. discards remainder
carry=: {{
s=. 0
for_b. y do.
if. (1+b) = s=. s-_1^b do. y=. (-.b) (b_index-0,b)} y end.
end.
if. 2=s do. y,1 else. y end.
}}
zge=: {{ cmp=. x -/@,: y while. (#cmp)*0={:cmp do. cmp=. }:cmp end. 0<:{:cmp }}</syntaxhighlight>
 
For example, we use the decimal number 10100 to represent 11 in base 10, and 1010 would represent 7. We convert these numbers to an internal zeckendorf representation and add them, then convert the result back to decimal 101000 which represents 18 in base 10.
 
Task examples:<syntaxhighlight lang="j"> 1 zadd&.zform 1
10
10 zadd&.zform 10
101
10100 zadd&.zform 1010
101000
10100 zsub&.zform 1010
101
10100 zmul&.zform 100101
10010010001
10100 zdiv&.zform 1010
1
10100 zdiv&.zform 1000
10
100001000001 zdiv&.zform 100010
100101
100001000001 zdiv&.zform 100101
100010</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight Javalang="java">import java.util.List;
 
public class Zeckendorf implements Comparable<Zeckendorf> {
Line 1,259 ⟶ 2,089:
System.out.println(g);
}
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 1,277 ⟶ 2,107:
 
=={{header|Julia}}==
Influenced by the format of the Tcl and Perl 6Raku versions, but added other functionality.
<langsyntaxhighlight lang="julia">import Base.*, Base.+, Base.-, Base./, Base.show, Base.!=, Base.==, Base.<=, Base.<, Base.>, Base.>=, Base.divrem
 
const z0 = "0"
Line 1,291 ⟶ 2,121:
tolen(x::Z, n::Int) = (s = x.s; while length(s) < n s = z0 * s end; s)
 
<(x::Z, y::Z) = (l = pairlen(x, y); flipordered ? tolen(x, l) >= tolen(y, l) : tolen(x, l) < tolen(y, l))
>(x::Z, y::Z) = (l = pairlen(x, y); flipordered ? tolen(x, l) <= tolen(y, l) : tolen(x, l) > tolen(y, l))
==(x::Z, y::Z) = (l = pairlen(x, y); tolen(x, l) == tolen(y, l))
<=(x::Z, y::Z) = (l = pairlen(x, y); flipordered ? tolen(x, l) >= tolen(y, l) : tolen(x, l) <= tolen(y, l))
>=(x::Z, y::Z) = (l = pairlen(x, y); flipordered ? tolen(x, l) <= tolen(y, l) : tolen(x, l) >= tolen(y, l))
!=(x::Z, y::Z) = (l = pairlen(x, y); tolen(x, l) != tolen(y, l))
 
Line 1,450 ⟶ 2,280:
 
zeckendorftest()
</langsyntaxhighlight>{{output}}<pre>
Addition:
Z("101")
Z("1001")
Z("10101")
Z("100101")
Z("1010000")
Subtraction:
Z("1")
Z("1000000")
Multiplication:
Z("1000100")
Z("101000101")
Division:
Z("1001")
(Z("1001"), Z("1"))
</pre>
 
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
class Zeckendorf(x: String = "0") : Comparable<Zeckendorf> {
Line 1,628 ⟶ 2,458:
g += "101".Z
println(g)
}</langsyntaxhighlight>
 
{{out}}
Line 1,648 ⟶ 2,478:
</pre>
 
=={{header|Perl 6Nim}}==
{{trans|Go}}
This is a somewhat limited implementation of Zeckendorf arithmetic operators. They only handle positive integer values. There are no actual calculations, everything is done with string manipulations, so it doesn't matter what glyphs you use for 1 and 0.
<syntaxhighlight lang="nim">type Zeckendorf = object
dVal: Natural
dLen: Natural
 
const
{{works with|rakudo|2015.09}}
Dig = ["00", "01", "10"]
Dig1 = ["", "1", "10"]
 
# Forward references.
Implemented arithmetic operators:
func b(z: var Zeckendorf; pos: Natural)
addition: '''+z'''
func inc(z: var Zeckendorf)
subtraction: '''-z'''
multiplication: '''*z'''
division: '''/z''' (more of a divmod really)
post increment: '''++z'''
post decrement: '''--z'''
 
Comparison operators:
equal '''eqz'''
not equal '''nez'''
greater than '''gtz'''
less than '''ltz'''
 
func a(z: var Zeckendorf; n: Natural) =
<lang perl6>
var i = n
my $z1 = '1'; # glyph to use for a '1'
while true:
my $z0 = '0'; # glyph to use for a '0'
 
if z.dLen < i: z.dLen = i
sub zorder($a) { ($z0 lt $z1) ?? $a !! $a.trans([$z0, $z1] => [$z1, $z0]) };
let j = z.dVal shr (i * 2) and 3
 
case j
######## Zeckendorf comparison operators #########
of 0, 1:
return
of 2:
if (z.dVal shr ((i + 1) * 2) and 1) != 1: return
z.dVal += 1 shl (i * 2 + 1)
return
of 3:
z.dVal = z.dVal and not (3 shl (i * 2))
z.b((i + 1) * 2)
else:
assert(false)
 
inc i
# less than
sub infix:<ltz>($a, $b) { $a.&zorder lt $b.&zorder };
 
# greater than
sub infix:<gtz>($a, $b) { $a.&zorder gt $b.&zorder };
 
func b(z: var Zeckendorf; pos: Natural) =
# equal
if pos == 0:
sub infix:<eqz>($a, $b) { $a eq $b };
inc z
return
 
if (z.dVal shr pos and 1) == 0:
# not equal
z.dVal += 1 shl pos
sub infix:<nez>($a, $b) { $a ne $b };
z.a(pos div 2)
if pos > 1: z.a(pos div 2 - 1)
else:
z.dVal = z.dVal and not(1 shl pos)
z.b(pos + 1)
z.b(pos - (if pos > 1: 2 else: 1))
 
 
########func Operatorsc(z: forvar Zeckendorf; arithmeticpos: ########Natural) =
if (z.dVal shr pos and 1) == 1:
z.dVal = z.dVal and not(1 shl pos)
return
 
z.c(pos + 1)
# post increment
if pos > 0:
sub postfix:<++z>($a is rw) {
z.b(pos - 1)
$a = ("$z0$z0"~$a).subst(/("$z0$z0")($z1+ %% $z0)?$/,
else:
-> $/ { "$z0$z1" ~ ($1 ?? $z0 x $1.chars !! '') });
$ainc ~~ s/^$z0+//;z
$a
}
 
# post decrement
sub postfix:<--z>($a is rw) {
$a.=subst(/$z1($z0*)$/,
-> $/ {$z0 ~ "$z1$z0" x $0.chars div 2 ~ $z1 x $0.chars mod 2});
$a ~~ s/^$z0+(.+)$/$0/;
$a
}
 
func initZeckendorf(s = "0"): Zeckendorf =
# addition
var q = 1
sub infix:<+z>($a is copy, $b is copy) { $a++z while $b--z nez $z0; $a };
var i = s.high
result.dLen = i div 2
while i >= 0:
result.dVal += (ord(s[i]) - ord('0')) * q
q *= 2
dec i
 
# subtraction
sub infix:<-z>($a is copy, $b is copy) { $a--z while $b--z nez $z0; $a };
 
func inc(z: var Zeckendorf) =
# multiplication
inc z.dVal
sub infix:<*z>($a, $b) {
z.a(0)
return $z0 if $a eqz $z0 or $b eqz $z0;
return $a if $b eqz $z1;
return $b if $a eqz $z1;
my $c = $a;
my $d = $z1;
repeat {
my $e = $z0;
repeat { $c++z; $e++z } until $e eqz $a;
$d++z;
} until $d eqz $b;
$c
};
 
# division (really more of a div mod)
sub infix:</z>($a is copy, $b is copy) {
fail "Divide by zero" if $b eqz $z0;
return $a if $a eqz $z0 or $b eqz $z1;
my $c = $z0;
repeat {
my $d = $b +z ($z1 ~ $z0);
$c++z;
$a--z while $d--z nez $z0
} until $a ltz $b;
$c ~= " remainder $a" if $a nez $z0;
$c
};
 
func `+=`(z1: var Zeckendorf; z2: Zeckendorf) =
for gn in 0 .. (2 * z2.dLen + 1):
if (z2.dVal shr gn and 1) == 1:
z1.b(gn)
 
###################### Testing ######################
 
func `-=`(z1: var Zeckendorf; z2: Zeckendorf) =
# helper sub to translate constants into the particular glyphs you used
for gn in 0 .. (2 * z2.dLen + 1):
sub z($a) { $a.trans([<1 0>] => [$z1, $z0]) };
if (z2.dVal shr gn and 1) == 1:
z1.c(gn)
 
while z1.dLen > 0 and (z1.dVal shr (z1.dLen * 2) and 3) == 0:
say "Using the glyph '$z1' for 1 and '$z0' for 0\n";
dec z1.dLen
 
my $fmt = "%-22s = %15s %s\n";
 
func `*=`(z1: var Zeckendorf; z2: Zeckendorf) =
my $zeck = $z1;
var na, nb = z2
var nr: Zeckendorf
for i in 0 .. (z1.dLen + 1) * 2:
if (z1.dVal shr i and 1) > 0: nr += nb
let nt = nb
nb += na
na = nt
z1 = nr
 
func`$`(z: var Zeckendorf): string =
printf( $fmt, "$zeck++z", $zeck++z, '# increment' ) for 1 .. 10;
if z.dVal == 0: return "0"
result.add Dig1[z.dVal shr (z.dLen * 2) and 3]
for i in countdown(z.dLen - 1, 0):
result.add Dig[z.dVal shr (i * 2) and 3]
 
when isMainModule:
printf $fmt, "$zeck +z {z('1010')}", $zeck +z= z('1010'), '# addition';
 
var g: Zeckendorf
printf $fmt, "$zeck -z {z('100')}", $zeck -z= z('100'), '# subtraction';
 
echo "Addition:"
printf $fmt, "$zeck *z {z('100101')}", $zeck *z= z('100101'), '# multiplication';
g = initZeckendorf("10")
g += initZeckendorf("10")
echo g
g += initZeckendorf("10")
echo g
g += initZeckendorf("1001")
echo g
g += initZeckendorf("1000")
echo g
g += initZeckendorf("10101")
echo g
 
printf $fmt, "$zeck /z {z('100')}", $zeck /z= z('100'), '# division';
 
echo "\nSubtraction:"
printf( $fmt, "$zeck--z", $zeck--z, '# decrement' ) for 1 .. 5;
g = initZeckendorf("1000")
g -= initZeckendorf("101")
echo g
g = initZeckendorf("10101010")
g -= initZeckendorf("1010101")
echo g
 
echo "\nMultiplication:"
printf $fmt, "$zeck *z {z('101001')}", $zeck *z= z('101001'), '# multiplication';
g = initZeckendorf("1001")
g *= initZeckendorf("101")
echo g
g = initZeckendorf("101010")
g += initZeckendorf("101")
echo g</syntaxhighlight>
 
{{out}}
printf $fmt, "$zeck /z {z('100')}", $zeck /z= z('100'), '# division';</lang>
<pre>Addition:
101
1001
10101
100101
1010000
 
Subtraction:
'''Testing Output'''
1
{{incorrect|Perl_6|10100+1010 <> 100101, see talk page}}
1000000
<pre>
Using the glyph '1' for 1 and '0' for 0
 
Multiplication:
1++z = 10 # increment
1000100
10++z = 100 # increment
1000100</pre>
100++z = 101 # increment
101++z = 1000 # increment
1000++z = 1001 # increment
1001++z = 1010 # increment
1010++z = 10000 # increment
10000++z = 10001 # increment
10001++z = 10010 # increment
10010++z = 10100 # increment
10100 +z 1010 = 100101 # addition
100101 -z 100 = 100010 # subtraction
100010 *z 100101 = 100001000001 # multiplication
100001000001 /z 100 = 101010001 # division
101010001--z = 101010000 # decrement
101010000--z = 101001010 # decrement
101001010--z = 101001001 # decrement
101001001--z = 101001000 # decrement
101001000--z = 101000101 # decrement
101000101 *z 101001 = 101010000010101 # multiplication
101010000010101 /z 100 = 1001010001001 remainder 10 # division
</pre>
Output using 'X' for 1 and 'O' for 0:
<pre>
Using the glyph 'X' for 1 and 'O' for 0
 
=={{header|Perl}}==
X++z = XO # increment
<syntaxhighlight lang="perl">use v5.36;
XO++z = XOO # increment
XOO++z = XOX # increment
XOX++z = XOOO # increment
XOOO++z = XOOX # increment
XOOX++z = XOXO # increment
XOXO++z = XOOOO # increment
XOOOO++z = XOOOX # increment
XOOOX++z = XOOXO # increment
XOOXO++z = XOXOO # increment
XOXOO +z XOXO = XOOXOX # addition
XOOXOX -z XOO = XOOOXO # subtraction
XOOOXO *z XOOXOX = XOOOOXOOOOOX # multiplication
XOOOOXOOOOOX /z XOO = XOXOXOOOX # division
XOXOXOOOX--z = XOXOXOOOO # decrement
XOXOXOOOO--z = XOXOOXOXO # decrement
XOXOOXOXO--z = XOXOOXOOX # decrement
XOXOOXOOX--z = XOXOOXOOO # decrement
XOXOOXOOO--z = XOXOOOXOX # decrement
XOXOOOXOX *z XOXOOX = XOXOXOOOOOXOXOX # multiplication
XOXOXOOOOOXOXOX /z XOO = XOOXOXOOOXOOX remainder XO # division
</pre>
 
package Zeckendorf;
=={{header|Phix}}==
use overload qw("" zstring + zadd - zsub ++ zinc -- zdec * zmul / zdiv ge zge);
Uses a binary representation of Zeckendorf numbers, eg decimal 11 is stored as 0b10100, ie meaning 8+3, but actually 20 in decimal.<br>
As such, they can be directly compared using the standard comparison operators, and printed quite trivially just by using the %b format.<br>
They are however (and not all that surprisingly) pulled apart into individual bits for addition/subtraction, etc.<br>
Does not handle negative numbers or anything >139583862445 (-ve probably doable but messy, >1.4e12 requires a total rewrite, probably using string representation).
<lang Phix>sequence fib = {1,1}
 
sub new ($class, $value) {
function zeckendorf(atom n)
bless \$value, ref $class || $class;
-- Same as [[Zeckendorf_number_representation#Phix]]
}
atom r = 0
while fib[$]<n do
fib &= fib[$] + fib[$-1]
end while
integer k = length(fib)
while k>2 and n<fib[k] do
k -= 1
end while
for i=k to 2 by -1 do
integer c = n>=fib[i]
r += r+c
n -= c*fib[i]
end for
return r
end function
 
sub zinc ($self, $, $) {
function decimal(object z)
local $_ = $$self;
-- Convert Zeckendorf number(s) to decimal
s/0$/1/ or s/(?:^|0)1$/10/;
atom dec = 0, bit = 2
if1 sequencewhile s/(z?:^|0) then11/100/;
$$self = $self->new( s/^0+\B//r )
for i=1 to length(z) do
}
z[i] = decimal(z[i])
end for
return z
end if
while z do
if and_bits(z,1) then
dec += fib[bit]
end if
bit += 1
if bit>length(fib) then
fib &= fib[$] + fib[$-1]
end if
z = floor(z/2)
end while
return dec
end function
 
sub zdec ($self, $, $) {
function to_bits(integer x)
local $_ = $$self;
-- Simplified copy of int_to_bits(), but in reverse order,
1 while s/100(?=0*$)/011/;
-- and +ve only but (also only) as many bits as needed, and
s/1$/0/ || s/10$/01/;
-- ensures there are *two* trailing 0 (most significant)
$$self = $self->new( s/^0+\B//r )
sequence bits = {}
}
if x<0 then ?9/0 end if -- sanity/avoid infinite loop
while 1 do
bits &= remainder(x,2)
if x=0 then exit end if
x = floor(x/2)
end while
bits &= 0 -- (since eg 101+101 -> 10000)
return bits
end function
 
sub zadd ($self, $other, $) {
function to_bits2(integer a,b)
my ($x, $y) = map $self->new($$_), $self, $other;
-- Apply to_bits() to a and b, and pad to the same length
$x++, $y-- while $$y;
sequence sa = to_bits(a), sb = to_bits(b)
$x
integer diff = length(sa)-length(sb)
}
if diff!=0 then
if diff<0 then sa &= repeat(0,-diff)
else sb &= repeat(0,+diff)
end if
end if
return {sa,sb}
end function
 
sub zsub ($self, $other, $) {
function to_int(sequence bits)
my ($x, $y) = map $self->new($$_), $self, $other;
-- Copy of bits_to_int(), but in reverse order (lsb last)
atom val = 0$x--, p$y-- =while 1$$y;
$x
for i=length(bits) to 1 by -1 do
}
if bits[i] then
val += p
end if
p += p
end for
return val
end function
function zstr(object z)
if sequence(z) then
for i=1 to length(z) do
z[i] = zstr(z[i])
end for
return z
end if
return sprintf("%b",z)
end function
 
sub zmul ($self, $other, $) {
function rep(sequence res, integer ds, sequence was, wth)
my ($x, $y) = map $self->new($$_), $self, $other;
-- helper for cleanup, validates replacements
integermy de$product = ds+lengthZeckendorf->new(was0)-1;
$product = $product + $x, $y-- while $y;
if res[ds..de]!=was then ?9/0 end if
$product
if length(was)!=length(wth) then ?9/0 end if
}
res[ds..de] = wth
return res
end function
 
sub zdiv ($self, $other, $) {
function zcleanup(sequence res)
my ($x, $y) = map $self->new($$_), $self, $other;
-- (shared by zadd and zsub)
integermy l$quotient = lengthZeckendorf->new(res0);
$quotient++, $x = $x - $y while $x ge $y;
-- first stage, left to right, {020x -> 100x', 030x -> 110x', 021x->110x, 012x->101x}
$quotient
for i=1 to l-3 do
}
switch res[i..i+2]
case {0,2,0}: res[i..i+2] = {1,0,0} res[i+3] += 1
case {0,3,0}: res[i..i+2] = {1,1,0} res[i+3] += 1
case {0,2,1}: res[i..i+2] = {1,1,0}
case {0,1,2}: res[i..i+2] = {1,0,1}
end switch
end for
-- first stage cleanup
if l>1 then
if res[l-1]=3 then res = rep(res,l-2,{0,3,0},{1,1,1}) -- 030 -> 111
elsif res[l-1]=2 then
if res[l-2]=0 then res = rep(res,l-2,{0,2,0},{1,0,1}) -- 020 -> 101
else res = rep(res,l-3,{0,1,2,0},{1,0,1,0}) -- 0120 -> 1010
end if
end if
end if
if res[l]=3 then res = rep(res,l-1,{0,3},{1,1}) -- 03 -> 11
elsif res[l]=2 then
if res[l-1]=0 then res = rep(res,l-1,{0,2},{1,0}) -- 02 -> 10
else res = rep(res,l-2,{0,1,2},{1,0,1}) -- 012 -> 101
end if
end if
-- second stage, pass 1, right to left, 011 -> 100
for i=length(res)-2 to 1 by -1 do
if res[i..i+2]={0,1,1} then res[i..i+2] = {1,0,0} end if
end for
-- second stage, pass 2, left to right, 011 -> 100
for i=1 to length(res)-2 do
if res[i..i+2]={0,1,1} then res[i..i+2] = {1,0,0} end if
end for
return to_int(res)
end function
 
sub zge ($self, $other, $) {
function zadd(integer a, b)
my $l; $l = length $$other if length $other > ($l = length $$self);
sequence {sa,sb} = to_bits2(a,b)
0 x ($l - length $$self) . $$self ge 0 x ($l - length $$other) . $$other;
return zcleanup(reverse(sq_add(sa,sb)))
}
end function
 
sub asdecimal ($self) {
function zinc(integer a)
returnmy($aa, $bb, $n) = zadd(a1,0b1 1, 0);
for ( reverse split '', $$self ) {
end function
$n += $bb * $_;
($aa, $bb) = ($bb, $aa + $bb);
}
$n
}
 
sub fromdecimal ($self, $value) {
function zsub(integer a, b)
sequencemy {sa,sb}$z = to_bits2$self->new(a,b0);
$z++ for 1 .. $value;
sequence res = reverse(sq_sub(sa,sb))
$z
-- (/not/ combined with the first pass of the add routine!)
}
for i=1 to length(res)-2 do
switch res[i..i+2] do
case {1, 0, 0}: res[i..i+2] = {0,1,1}
case {1,-1, 0}: res[i..i+2] = {0,0,1}
case {1,-1, 1}: res[i..i+2] = {0,0,2}
case {1, 0,-1}: res[i..i+2] = {0,1,0}
case {2, 0, 0}: res[i..i+2] = {1,1,1}
case {2,-1, 0}: res[i..i+2] = {1,0,1}
case {2,-1, 1}: res[i..i+2] = {1,0,2}
case {2, 0,-1}: res[i..i+2] = {1,1,0}
end switch
end for
-- copied from PicoLisp: {1,-1} -> {0,1} and {2,-1} -> {1,1}
for i=1 to length(res)-1 do
switch res[i..i+1] do
case {1,-1}: res[i..i+1] = {0,1}
case {2,-1}: res[i..i+1] = {1,1}
end switch
end for
if find(-1,res) then ?9/0 end if -- sanity check
return zcleanup(res)
end function
 
sub zstring { ${ shift() } }
function zdec(integer a)
return zsub(a,0b1)
end function
 
package main;
function zmul(integer a, b)
integer res = 0
sequence mult = {a,zadd(a,a)} -- (as per task desc)
integer bits = 2
while bits<b do
mult = append(mult,zadd(mult[$],mult[$-1]))
bits *= 2
end while
integer bit = 1
while b do
if and_bits(b,1) then
res = zadd(res,mult[bit])
end if
b = floor(b/2)
bit += 1
end while
return res
end function
 
for ( split /\n/, <<END ) # test cases
function zdiv(integer a, b)
1 + 1
integer res = 0
10 + 10
sequence mult = {b,zadd(b,b)}
10100 + 1010
integer bits = 2
10100 - 1010
while mult[$]<a do
10100 * 1010
mult = append(mult,zadd(mult[$],mult[$-1]))
bits100010 *= 2100101
10100 / end while1010
101000 / 1000
for i=length(mult) to 1 by -1 do
100001000001 / 100010
integer mi = mult[i]
100001000001 / 100101
if mi<=a then
END
res = zadd(res,bits)
{
a = zsub(a,mi)
my ($left, $op, $right) = split;
if a=0 then exit end if
my ($x, $y) = map Zeckendorf->new($_), $left, $right;
end if
my $answer =
bits = floor(bits/2)
$op eq '+' ? $x + $y :
end for
return$op {res,a}eq '--' (a? is$x - the$y remainder):
$op eq '*' ? $x * $y :
end function
$op eq '/' ? $x / $y :
die "bad op <$op>";
printf "%12s %s %-9s => %12s in Zeckendorf\n", $x, $op, $y, $answer;
printf "%12d %s %-9d => %12d in decimal\n\n",
$x->asdecimal, $op, $y->asdecimal, $answer->asdecimal;
}</syntaxhighlight>
{{out}}
<pre> 1 + 1 => 10 in Zeckendorf
1 + 1 => 2 in decimal
 
10 + 10 => 101 in Zeckendorf
2 + 2 => 4 in decimal
 
10100 + 1010 => 101000 in Zeckendorf
11 + 7 => 18 in decimal
 
10100 - 1010 => 101 in Zeckendorf
for i=0 to 20 do
11 - 7 => 4 in decimal
integer zi = zeckendorf(i)
atom d = decimal(zi)
printf(1,"%2d: %7b (%d)\n",{i,zi,d})
end for
 
10100 * 1010 => 101000001 in Zeckendorf
procedure test(atom a, string op, atom b, object res, string expected)
11 * 7 => 77 in decimal
string zres = iff(atom(res)?zstr(res):join(zstr(res)," rem ")),
dres = sprintf(iff(atom(res)?"%d":"%d rem %d"),decimal(res)),
aka = sprintf("aka %d %s %d = %s",{decimal(a),op,decimal(b),dres}),
ok = iff(zres=expected?"":" *** ERROR ***!!")
printf(1,"%s %s %s = %s, %s %s\n",{zstr(a),op,zstr(b),zres,aka,ok})
end procedure
 
100010 * 100101 => 100001000001 in Zeckendorf
test(0b0,"+",0b0,zadd(0b0,0b0),"0")
15 * 17 => 255 in decimal
test(0b101,"+",0b101,zadd(0b101,0b101),"10000")
test(0b10100,"-",0b1000,zsub(0b10100,0b1000),"1001")
test(0b100100,"-",0b1000,zsub(0b100100,0b1000),"10100")
test(0b1001,"*",0b101,zmul(0b1001,0b101),"1000100")
test(0b1000101,"/",0b101,zdiv(0b1000101,0b101),"1001 rem 1")
 
10100 / 1010 => 1 in Zeckendorf
test(0b10,"+",0b10,zadd(0b10,0b10),"101")
11 / 7 => 1 in decimal
test(0b101,"+",0b10,zadd(0b101,0b10),"1001")
test(0b1001,"+",0b1001,zadd(0b1001,0b1001),"10101")
test(0b10101,"+",0b1000,zadd(0b10101,0b1000),"100101")
test(0b100101,"+",0b10101,zadd(0b100101,0b10101),"1010000")
test(0b1000,"-",0b101,zsub(0b1000,0b101),"1")
test(0b10101010,"-",0b1010101,zsub(0b10101010,0b1010101),"1000000")
test(0b1001,"*",0b101,zmul(0b1001,0b101),"1000100")
test(0b101010,"+",0b101,zadd(0b101010,0b101),"1000100")
 
101000 / 1000 => 100 in Zeckendorf
test(0b10100,"+",0b1010,zadd(0b10100,0b1010),"101000")
18 / 5 => 3 in decimal
test(0b101000,"-",0b1010,zsub(0b101000,0b1010),"10100")
 
100001000001 / 100010 => 100101 in Zeckendorf
test(0b100010,"*",0b100101,zmul(0b100010,0b100101),"100001000001")
255 / 15 => 17 in decimal
test(0b100001000001,"/",0b100,zdiv(0b100001000001,0b100),"101010001 rem 0")
test(0b101000101,"*",0b101001,zmul(0b101000101,0b101001),"101010000010101")
test(0b101010000010101,"/",0b100,zdiv(0b101010000010101,0b100),"1001010001001 rem 10")
 
100001000001 / 100101 => 100010 in Zeckendorf
test(0b10100010010100,"+",0b1001000001,zadd(0b10100010010100,0b1001000001),"100000000010101")
255 / 17 => 15 in decimal</pre>
test(0b10100010010100,"-",0b1001000001,zsub(0b10100010010100,0b1001000001),"10010001000010")
test(0b10000,"*",0b1001000001,zmul(0b10000,0b1001000001),"10100010010100")
test(0b1010001010000001001,"/",0b100000000100000,zdiv(0b1010001010000001001,0b100000000100000),"10001 rem 10100001010101")
 
=={{header|Phix}}==
test(0b10100,"+",0b1010,zadd(0b10100,0b1010),"101000")
Uses a binary representation of Zeckendorf numbers, eg decimal 11 is stored as 0b10100, ie meaning 8+3, but actually 20 in decimal.<br>
test(0b10100,"-",0b1010,zsub(0b10100,0b1010),"101")
As such, they can be directly compared using the standard comparison operators, and printed quite trivially just by using the %b format.<br>
test(0b10100,"*",0b1010,zmul(0b10100,0b1010),"101000001")
They are however (and not all that surprisingly) pulled apart into individual bits for addition/subtraction, etc.<br>
test(0b10100,"/",0b1010,zdiv(0b10100,0b1010),"1 rem 101")
Does not handle negative numbers or anything >139583862445 (-ve probably doable but messy, >1.4e12 requires a total rewrite, probably using string representation).
integer m = zmul(0b10100,0b1010)
<!--<syntaxhighlight lang="phix">(phixonline)-->
test(m,"/",0b1010,zdiv(m,0b1010),"10100 rem 0")</lang>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fib</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zeckendorf</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Same as [[Zeckendorf_number_representation#Phix]]</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[$]<</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">fib</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">k</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">r</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Convert Zeckendorf number(s) to decimal</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">dec</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">z</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">dec</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">bit</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bit</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fib</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fib</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">fib</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">dec</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_bits</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Simplified copy of int_to_bits(), but in reverse order,
-- and +ve only but (also only) as many bits as needed, and
-- ensures there are *two* trailing 0 (most significant)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">x</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- sanity/avoid infinite loop</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- (since eg 101+101 -&gt; 10000)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">bits</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_bits2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Apply to_bits() to a and b, and pad to the same length</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sa</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">sb</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">diff</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">diff</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">sa</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span> <span style="color: #000000;">sb</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_int</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Copy of bits_to_int(), but in reverse order (lsb last)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">val</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">val</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">val</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%b"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">was</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wth</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- helper for cleanup, validates replacements </span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">de</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">was</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ds</span><span style="color: #0000FF;">..</span><span style="color: #000000;">de</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">was</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">was</span><span style="color: #0000FF;">)!=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wth</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ds</span><span style="color: #0000FF;">..</span><span style="color: #000000;">de</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">wth</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zcleanup</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- (shared by zadd and zsub)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- first stage, left to right, {020x -&gt; 100x', 030x -&gt; 110x', 021x-&gt;110x, 012x-&gt;101x}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- first stage cleanup</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 030 -&gt; 111</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 020 -&gt; 101</span>
<span style="color: #008080;">else</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 0120 -&gt; 1010</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 03 -&gt; 11</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 02 -&gt; 10</span>
<span style="color: #008080;">else</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 012 -&gt; 101</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- second stage, pass 1, right to left, 011 -&gt; 100</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- second stage, pass 2, left to right, 011 -&gt; 100</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]={</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">to_int</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_bits2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">zcleanup</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zinc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">to_bits2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sb</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- (/not/ combined with the first pass of the add routine!)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- copied from PicoLisp: {1,-1} -&gt; {0,1} and {2,-1} -&gt; {1,1}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- sanity check</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">zcleanup</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zdec</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mult</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)}</span> <span style="color: #000080;font-style:italic;">-- (as per task desc)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;"><</span><span style="color: #000000;">b</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">mult</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span>
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">b</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">bit</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mult</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">mult</span><span style="color: #0000FF;">[$]<</span><span style="color: #000000;">a</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">mult</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span>
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mult</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">mi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mult</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">mi</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">a</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mi</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- (a is the remainder)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">20</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">zi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zeckendorf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zi</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %7b (%d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">op</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">zres</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">):</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #008000;">" rem "</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">dres</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%d rem %d"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">aka</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aka %d %s %d = %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">decimal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span><span style="color: #000000;">dres</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zres</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" *** ERROR ***!!"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %s %s = %s, %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zstr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span><span style="color: #000000;">zres</span><span style="color: #0000FF;">,</span><span style="color: #000000;">aka</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10000"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1000100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1000101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1000101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1001 rem 1"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1001"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"100101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1010000"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10101010</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10101010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1000000"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1000100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101010</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1000100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101000"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100010</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100101</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"100001000001"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100001000001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b100001000001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101010001 rem 0"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101000101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101000101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b101001</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101010000010101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101010000010101</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b101010000010101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1001010001001 rem 10"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100010010100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100010010100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"100000000010101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100010010100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100010010100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10010001000010"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1001000001</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10100010010100"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1010001010000001001</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100000000100000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1010001010000001001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b100000000100000</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10001 rem 10100001010101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zadd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101000"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zsub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"101000001"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"1 rem 101"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zmul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b10100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zdiv</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1010</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"10100 rem 0"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,151 ⟶ 3,106:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="text">(seed (in "/dev/urandom" (rd 8)))
 
(de unpad (Lst)
Line 2,283 ⟶ 3,238:
(test (numz (- X 1)) (decz (numz X))) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|Python}}==
<syntaxhighlight lang="python">import copy
 
class Zeckendorf:
def __init__(self, x='0'):
q = 1
i = len(x) - 1
self.dLen = int(i / 2)
self.dVal = 0
while i >= 0:
self.dVal = self.dVal + (ord(x[i]) - ord('0')) * q
q = q * 2
i = i -1
 
def a(self, n):
i = n
while True:
if self.dLen < i:
self.dLen = i
j = (self.dVal >> (i * 2)) & 3
if j == 0 or j == 1:
return
if j == 2:
if (self.dVal >> ((i + 1) * 2) & 1) != 1:
return
self.dVal = self.dVal + (1 << (i * 2 + 1))
return
if j == 3:
temp = 3 << (i * 2)
temp = temp ^ -1
self.dVal = self.dVal & temp
self.b((i + 1) * 2)
i = i + 1
 
def b(self, pos):
if pos == 0:
self.inc()
return
if (self.dVal >> pos) & 1 == 0:
self.dVal = self.dVal + (1 << pos)
self.a(int(pos / 2))
if pos > 1:
self.a(int(pos / 2) - 1)
else:
temp = 1 << pos
temp = temp ^ -1
self.dVal = self.dVal & temp
self.b(pos + 1)
self.b(pos - (2 if pos > 1 else 1))
 
def c(self, pos):
if (self.dVal >> pos) & 1 == 1:
temp = 1 << pos
temp = temp ^ -1
self.dVal = self.dVal & temp
return
self.c(pos + 1)
if pos > 0:
self.b(pos - 1)
else:
self.inc()
 
def inc(self):
self.dVal = self.dVal + 1
self.a(0)
 
def __add__(self, rhs):
copy = self
rhs_dVal = rhs.dVal
limit = (rhs.dLen + 1) * 2
for gn in range(0, limit):
if ((rhs_dVal >> gn) & 1) == 1:
copy.b(gn)
return copy
 
def __sub__(self, rhs):
copy = self
rhs_dVal = rhs.dVal
limit = (rhs.dLen + 1) * 2
for gn in range(0, limit):
if (rhs_dVal >> gn) & 1 == 1:
copy.c(gn)
while (((copy.dVal >> ((copy.dLen * 2) & 31)) & 3) == 0) or (copy.dLen == 0):
copy.dLen = copy.dLen - 1
return copy
 
def __mul__(self, rhs):
na = copy.deepcopy(rhs)
nb = copy.deepcopy(rhs)
nr = Zeckendorf()
dVal = self.dVal
for i in range(0, (self.dLen + 1) * 2):
if ((dVal >> i) & 1) > 0:
nr = nr + nb
nt = copy.deepcopy(nb)
nb = nb + na
na = copy.deepcopy(nt)
return nr
 
def __str__(self):
dig = ["00", "01", "10"]
dig1 = ["", "1", "10"]
 
if self.dVal == 0:
return '0'
idx = (self.dVal >> ((self.dLen * 2) & 31)) & 3
sb = dig1[idx]
i = self.dLen - 1
while i >= 0:
idx = (self.dVal >> (i * 2)) & 3
sb = sb + dig[idx]
i = i - 1
return sb
 
# main
print "Addition:"
g = Zeckendorf("10")
g = g + Zeckendorf("10")
print g
g = g + Zeckendorf("10")
print g
g = g + Zeckendorf("1001")
print g
g = g + Zeckendorf("1000")
print g
g = g + Zeckendorf("10101")
print g
print
 
print "Subtraction:"
g = Zeckendorf("1000")
g = g - Zeckendorf("101")
print g
g = Zeckendorf("10101010")
g = g - Zeckendorf("1010101")
print g
print
 
print "Multiplication:"
g = Zeckendorf("1001")
g = g * Zeckendorf("101")
print g
g = Zeckendorf("101010")
g = g + Zeckendorf("101")
print g</syntaxhighlight>
{{out}}
<pre>Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100</pre>
 
=={{header|Quackery}}==
 
Unsigned (non-negative) Zeckendorf arithmetic.
 
Implements the required functions; addition, subtraction, multiplication and division, the optional decrement, increment and comparative functions, and additionally double and modulus, since they come for free with addition and division respectively.
 
The algorithms are of my own devising, without reference to the description in the task or existing research, so are potentially novel, but probably not.
 
I really should have taken notes as I was going along, so here is the hand-wavy explanation:
 
Mostly Zeckendorf numbers are represented bitwise to benefit from the inherent parallelism of bitwise logic, but occasionally as nests (the Quackery name for dynamic arrays) of 0s and 1s for ease of coding.
 
The word <code>canonise</code> puts a Zecekendorf number in canonical form; no two adjacent bits are set to 1, runs of 0s are allowed. The converse operation, <code>defrock</code> puts a number as far from canonical form as possible; no two adjacent bits are set to 0, runs of 1s are allowed. Despite their similarities they are coded quite differently as there was a long gap between coding one and then the other and as noted above, I didn't take notes.
 
Addition works by isolating the bits in both arguments that are set to 1, removing them from both and then bitwise xoring them together and canonising. After this the isolated bits are doubled and these two numbers (the xored number and the isolated bits number) are added. This is repeated until the xored number is 0. Doubling is achieved by shifting the number an appropriate distance left and right and adding the left shifted and right shifted numbers. <code>zadd</code> and <code>zdouble</code> are mutually recursive.
 
<code>2blit</code> separates the lowest two bits from a Zeckedorf number so that <code>zdouble</code> can treat them as a special case.
 
Multiplication is basically the Russian Peasant algorithm with the twist that instead of doubling we start with two instances of one of the multiplicands and repeatedly add them Fibonacci style.
 
Subtraction is implemented as ''difference'' (i.e. abs(a-b) as this is an unsigned implementation.) The process is to reduce both numbers in value until the smaller one equals zero. Continuing the naming theme established by <code>canonise</code> and <code>defrock</code>, the word that removes the bits that are set to 1 in both arguments is called <code>exorcise</code>. The appropriate sequence of exorcisms and defrockings will reduce the smaller argument to zero much of the time.
 
However, numbers which alternate 1s and 0s (e.g. ...01010101...) are immune to both canonisation and defrocking. When this occurs we add the smaller number to the larger number and double the smaller number and repeat the exorisms and defrockings. Extensive testing leads me to believe with a very high degree of confidence this is sufficient, but I have not proved it in a mathematical sense.
 
Division is basic binary long division with a twist; instead of multiplying the divisor by 2 until it's large enough, use it to make a fibonacci style sequence, except starting with a couple of copies of the divisor rather than 1s.
 
The while-again loop computes a nest of all the fibonacci multiples up to the dividend, the witheach loop tries subracting each one from largest to smallest and builds up the result accordingly. The remainder (modulus) comes for free as what is left at the end of all the subtractions.
 
To demonstrate that these words correctly implement Zeckendorf arithmetic, I have used them to implement Euclid's algorithm for Greatest Common Denominator, and used that to implement Largest Common Multiple. We repeatedly give <code>zlcm</code> two random numbers up to one quintillion (converted to Zeckendorf notation) and print the result (converted back to decimal), next to the same computation made using the conventional representation. <code>zgcd</code> and <code>zlcm</code> exercise the multiplication, division and modulus routines repeatedly, and those exercise the addition, subtraction and comparison routines.
 
<code>bin</code> is an extension to the Quackery compiler to allow it to understand numbers in binary notation (and hence also Zeckendorf notation).
 
<code>gcd</code> and <code>lcm</code> are defined at [[Least common multiple#Quackery]], and <code>n->z</code> and <code>z->n</code> are defined at [[Zeckendorf number representation#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ nextword
dup $ "" = if
[ $ '"bin" needs to be followed by a string.'
message put bail ]
dup
2 base put
$->n
base release
not if
[ drop
$ " is not a binary number."
join message put
bail ]
nip
swap dip join ] builds bin ( [ $ --> [ $ )
 
[ ^ not ] is zeq ( z z --> b )
 
[ zeq not ] is zne ( z z --> b )
 
[ false unrot
[ 2dup zne while
rot drop
dup 1 & unrot
1 >> dip [ 1 >> ]
again ]
2drop ] is zlt ( z z --> b )
 
[ swap zlt ] is zgt ( z z --> b )
 
[ zlt not ] is zge ( z z --> b )
 
[ zgt not ] is zle ( z z --> b )
 
[ dup 1 << & 0 zeq ] is canonical ( z --> b )
 
[ [] swap
[ dup 1 & rot join
swap 1 >>
dup not until ]
drop ] is bits ( z --> [ )
 
[ dup canonical if done
0 0 rot bits
witheach
[ |
[ table
[ 1 << 0 ]
[ 1 << 1 | bin 10 ]
[ 1 << 0 ]
[ 1 >> 1 |
bin 10 << 0 ] ]
do ]
drop again ] is canonise ( z --> z )
 
[ dup bin -100
& swap bin 11 & ] is 2blit ( z --> z z )
 
[ 2blit bit | canonise ] is zinc ( z --> z )
 
[ dup 0 zeq if
[ $ "Cannot zdec zero."
fail ]
1
[ 2dup & if done
1 << again ]
tuck ^
swap 1 <<
[ bin 10 >>
tuck | swap
dup 0 zeq until ]
drop ] is zdec ( z --> z )
 
forward is zadd ( z --> z )
 
[ dup 2blit
[ table
0 bin 10 bin 101 ]
unrot bin 10 >>
swap 1 <<
rot | zadd ] is zdouble ( z --> z )
 
[ 2dup ^ canonise
unrot &
dup 0 zeq iff
drop done
zdouble again ] resolves zadd ( z z --> z )
 
[ tuck take zadd swap put ] is ztally ( z s --> )
 
[ 0 temp put
dip dup
[ dup while
dup 1 & if
[ over temp ztally ]
dip [ tuck zadd ]
1 >> again ]
drop 2drop temp take ] is zmult ( z z --> z )
 
[ 2dup & ~ tuck & dip & ] is exorcise ( z z --> z z )
 
[ dup
[ 0 ' [ 0 0 0 ] rot 1
[ 2dup > while
1 << again ]
1 <<
[ dup while
2swap 2over & 0 !=
dip
[ dup
' [ 1 0 0 ]
= if
[ drop
' [ 0 1 1 ] ] ]
join
behead
rot 1 << | swap
2swap 1 >> again ]
2drop
witheach
[ dip [ 1 << ] | ]
dup bin 111 &
bin 100 zeq if
[ bin -1000 &
bin 11 | ] ]
2dup zeq iff drop done
nip again ] is defrock ( z --> z )
 
[ 2dup zlt if swap
dup 0 zeq iff drop done
[ exorcise dup while
dip defrock
exorcise dup while
dup dip zadd
zdouble
again ]
drop canonise ] is zdiff ( z z --> z )
 
[ dup 0 zeq if
[ $ "Z-division by zero."
fail ]
0 unrot swap
temp put
dup nested
[ dup 0 peek
tuck dip rot zadd
temp share
over zge while
swap join
again ]
drop nip
temp take
swap witheach
[ rot 1 << unrot
2dup zge iff
[ zdiff
dip [ 1 | ] ]
else drop ] ] is zdivmod ( z z --> z z )
 
[ zdivmod drop ] is zdiv ( z z --> z )
 
[ zdivmod nip ] is zmod ( z z --> z )
 
[ [ dup while
tuck zmod again ]
drop ] is zgcd ( z z --> z )
 
[ 2dup and iff
[ 2dup zgcd
zdiv zmult ]
else and ] is zlcm ( z z --> z )
 
10 times
[ 10 15 ** random
10 15 ** random
2dup lcm echo cr
n->z dip n->z
zlcm z->n echo cr cr ]</syntaxhighlight>
 
{{out}}
 
<pre>25624571429859946191396654570
25624571429859946191396654570
 
24702413608219494319878326100
24702413608219494319878326100
 
177592191573881063687998734000
177592191573881063687998734000
 
28221788451919578670971892845
28221788451919578670971892845
 
99008448632249766843573255321
99008448632249766843573255321
 
312648960463735816244223692220
312648960463735816244223692220
 
146093274904252809568841733264
146093274904252809568841733264
 
169485448104022309641359784180
169485448104022309641359784180
 
593337022246602746222083444716
593337022246602746222083444716
 
50904418052185753625716614402
50904418052185753625716614402
</pre>
 
=={{header|Racket}}==
This implementation only handles natural (non-negative numbers). The algorithms for addition and subtraction use the techniques explained in the paper "Efficient algorithms for Zeckendorf arithmetic" (http://arxiv.org/pdf/1207.4497.pdf).
 
<langsyntaxhighlight lang="racket">#lang racket (require math)
 
(define sqrt5 (sqrt 5))
Line 2,483 ⟶ 3,845:
(example '/ zeck-quotient 9876 1000)
(example '% zeck-remainder 9876 1000)
</syntaxhighlight>
</lang>
 
{{output}}
Line 2,494 ⟶ 3,856:
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
This is a somewhat limited implementation of Zeckendorf arithmetic operators. They only handle positive integer values. There are no actual calculations, everything is done with string manipulations, so it doesn't matter what glyphs you use for 1 and 0.
 
Implemented arithmetic operators:
'''+z''' addition
'''-z''' subtraction
'''×z''' multiplication
'''/z''' division (more of a divmod really)
'''++z''' post increment
'''--z''' post decrement
 
Comparison operators:
'''eqz''' equal
'''nez''' not equal
'''gtz''' greater than
'''ltz''' less than
 
<syntaxhighlight lang="raku" line>my $z1 = '1'; # glyph to use for a '1'
my $z0 = '0'; # glyph to use for a '0'
 
sub zorder($a) { ($z0 lt $z1) ?? $a !! $a.trans([$z0, $z1] => [$z1, $z0]) }
 
######## Zeckendorf comparison operators #########
 
# less than
sub infix:<ltz>($a, $b) { $a.&zorder lt $b.&zorder }
 
# greater than
sub infix:<gtz>($a, $b) { $a.&zorder gt $b.&zorder }
 
# equal
sub infix:<eqz>($a, $b) { $a eq $b }
 
# not equal
sub infix:<nez>($a, $b) { $a ne $b }
 
######## Operators for Zeckendorf arithmetic ########
 
# post increment
sub postfix:<++z>($a is rw) {
$a = ("$z0$z0"~$a).subst(/("$z0$z0")($z1+ %% $z0)?$/,
-> $/ { "$z0$z1" ~ ($1 ?? $z0 x $1.chars !! '') });
$a ~~ s/^$z0+//;
$a
}
 
# post decrement
sub postfix:<--z>($a is rw) {
$a.=subst(/$z1($z0*)$/,
-> $/ {$z0 ~ "$z1$z0" x $0.chars div 2 ~ $z1 x $0.chars mod 2});
$a ~~ s/^$z0+(.+)$/$0/;
$a
}
 
# addition
sub infix:<+z>($a is copy, $b is copy) { $a++z; $a++z while $b--z nez $z0; $a }
 
# subtraction
sub infix:<-z>($a is copy, $b is copy) { $a--z; $a--z while $b--z nez $z0; $a }
 
# multiplication
sub infix:<×z>($a, $b) {
return $z0 if $a eqz $z0 or $b eqz $z0;
return $a if $b eqz $z1;
return $b if $a eqz $z1;
my $c = $a;
my $d = $z1;
repeat {
my $e = $z0;
repeat { $c++z; $e++z } until $e eqz $a;
$d++z;
} until $d eqz $b;
$c
}
 
# division (really more of a div mod)
sub infix:</z>($a is copy, $b is copy) {
fail "Divide by zero" if $b eqz $z0;
return $a if $a eqz $z0 or $b eqz $z1;
my $c = $z0;
repeat {
my $d = $b +z ($z1 ~ $z0);
$c++z;
$a++z;
$a--z while $d--z nez $z0
} until $a ltz $b;
$c ~= " remainder $a" if $a nez $z0;
$c
}
 
###################### Testing ######################
 
# helper sub to translate constants into the particular glyphs you used
sub z($a) { $a.trans([<1 0>] => [$z1, $z0]) }
 
say "Using the glyph '$z1' for 1 and '$z0' for 0\n";
 
my $fmt = "%-22s = %15s %s\n";
 
my $zeck = $z1;
 
printf( $fmt, "$zeck++z", $zeck++z, '# increment' ) for 1 .. 10;
 
printf $fmt, "$zeck +z {z('1010')}", $zeck +z= z('1010'), '# addition';
 
printf $fmt, "$zeck -z {z('100')}", $zeck -z= z('100'), '# subtraction';
 
printf $fmt, "$zeck ×z {z('100101')}", $zeck ×z= z('100101'), '# multiplication';
 
printf $fmt, "$zeck /z {z('100')}", $zeck /z= z('100'), '# division';
 
printf( $fmt, "$zeck--z", $zeck--z, '# decrement' ) for 1 .. 5;
 
printf $fmt, "$zeck ×z {z('101001')}", $zeck ×z= z('101001'), '# multiplication';
 
printf $fmt, "$zeck /z {z('100')}", $zeck /z= z('100'), '# division';</syntaxhighlight>
 
'''Testing Output'''
<pre>
Using the glyph '1' for 1 and '0' for 0
 
1++z = 10 # increment
10++z = 100 # increment
100++z = 101 # increment
101++z = 1000 # increment
1000++z = 1001 # increment
1001++z = 1010 # increment
1010++z = 10000 # increment
10000++z = 10001 # increment
10001++z = 10010 # increment
10010++z = 10100 # increment
10100 +z 1010 = 101000 # addition
101000 -z 100 = 100010 # subtraction
100010 ×z 100101 = 100001000001 # multiplication
100001000001 /z 100 = 101010001 # division
101010001--z = 101010000 # decrement
101010000--z = 101001010 # decrement
101001010--z = 101001001 # decrement
101001001--z = 101001000 # decrement
101001000--z = 101000101 # decrement
101000101 ×z 101001 = 101010000010101 # multiplication
101010000010101 /z 100 = 1001010001001 remainder 10 # division</pre>
Using alternate glyphs:
<pre>
Using the glyph 'X' for 1 and 'O' for 0
 
X++z = XO # increment
XO++z = XOO # increment
XOO++z = XOX # increment
XOX++z = XOOO # increment
XOOO++z = XOOX # increment
XOOX++z = XOXO # increment
XOXO++z = XOOOO # increment
XOOOO++z = XOOOX # increment
XOOOX++z = XOOXO # increment
XOOXO++z = XOXOO # increment
XOXOO +z XOXO = XOXOOO # addition
XOXOOO -z XOO = XOOOXO # subtraction
XOOOXO ×z XOOXOX = XOOOOXOOOOOX # multiplication
XOOOOXOOOOOX /z XOO = XOXOXOOOX # division
XOXOXOOOX--z = XOXOXOOOO # decrement
XOXOXOOOO--z = XOXOOXOXO # decrement
XOXOOXOXO--z = XOXOOXOOX # decrement
XOXOOXOOX--z = XOXOOXOOO # decrement
XOXOOXOOO--z = XOXOOOXOX # decrement
XOXOOOXOX ×z XOXOOX = XOXOXOOOOOXOXOX # multiplication
XOXOXOOOOOXOXOX /z XOO = XOOXOXOOOXOOX remainder XO # division</pre>
 
=={{header|Rust}}==
{{trans|C#}}
<syntaxhighlight lang="Rust">
struct Zeckendorf {
d_val: i32,
d_len: i32,
}
 
impl Zeckendorf {
fn new(x: &str) -> Zeckendorf {
let mut d_val = 0;
let mut q = 1;
let mut i = x.len() as i32 - 1;
let d_len = i / 2;
while i >= 0 {
d_val += (x.chars().nth(i as usize).unwrap() as i32 - '0' as i32) * q;
q *= 2;
i -= 1;
}
 
Zeckendorf { d_val, d_len }
}
 
fn a(&mut self, n: i32) {
let mut i = n;
loop {
if self.d_len < i {
self.d_len = i;
}
let j = (self.d_val >> (i * 2)) & 3;
match j {
0 | 1 => return,
2 => {
if ((self.d_val >> ((i + 1) * 2)) & 1) != 1 {
return;
}
self.d_val += 1 << (i * 2 + 1);
return;
}
3 => {
let temp = 3 << (i * 2);
let temp = !temp;
self.d_val &= temp;
self.b((i + 1) * 2);
}
_ => (),
}
i += 1;
}
}
 
fn b(&mut self, pos: i32) {
if pos == 0 {
self.inc();
return;
}
if ((self.d_val >> pos) & 1) == 0 {
self.d_val += 1 << pos;
self.a(pos / 2);
if pos > 1 {
self.a(pos / 2 - 1);
}
} else {
let temp = 1 << pos;
let temp = !temp;
self.d_val &= temp;
self.b(pos + 1);
self.b(pos - if pos > 1 { 2 } else { 1 });
}
}
 
fn c(&mut self, pos: i32) {
if ((self.d_val >> pos) & 1) == 1 {
let temp = 1 << pos;
let temp = !temp;
self.d_val &= temp;
return;
}
self.c(pos + 1);
if pos > 0 {
self.b(pos - 1);
} else {
self.inc();
}
}
 
fn inc(&mut self) -> &mut Self {
self.d_val += 1;
self.a(0);
self
}
 
fn copy(&self) -> Zeckendorf {
Zeckendorf {
d_val: self.d_val,
d_len: self.d_len,
}
}
 
fn plus_assign(&mut self, other: &Zeckendorf) {
for gn in 0..(other.d_len + 1) * 2 {
if ((other.d_val >> gn) & 1) == 1 {
self.b(gn);
}
}
}
 
fn minus_assign(&mut self, other: &Zeckendorf) {
for gn in 0..(other.d_len + 1) * 2 {
if ((other.d_val >> gn) & 1) == 1 {
self.c(gn);
}
}
while (((self.d_val >> self.d_len * 2) & 3) == 0) || (self.d_len == 0) {
self.d_len -= 1;
}
}
 
fn times_assign(&mut self, other: &Zeckendorf) {
let mut na = other.copy();
let mut nb = other.copy();
let mut nt;
let mut nr = Zeckendorf::new("0");
for i in 0..(self.d_len + 1) * 2 {
if ((self.d_val >> i) & 1) > 0 {
nr.plus_assign(&nb);
}
nt = nb.copy();
nb.plus_assign(&na);
na = nt.copy(); // `na` is now mutable, so this reassignment is allowed
}
self.d_val = nr.d_val;
self.d_len = nr.d_len;
}
 
fn to_string(&self) -> String {
if self.d_val == 0 {
return "0".to_string();
}
 
let dig = ["00", "01", "10"];
let dig1 = ["", "1", "10"];
 
let idx = (self.d_val >> (self.d_len * 2)) & 3;
let mut sb = String::from(dig1[idx as usize]);
for i in (0..self.d_len).rev() {
let idx = (self.d_val >> (i * 2)) & 3;
sb.push_str(dig[idx as usize]);
}
sb
}
}
 
fn main() {
println!("Addition:");
let mut g = Zeckendorf::new("10");
g.plus_assign(&Zeckendorf::new("10"));
println!("{}", g.to_string());
g.plus_assign(&Zeckendorf::new("10"));
println!("{}", g.to_string());
g.plus_assign(&Zeckendorf::new("1001"));
println!("{}", g.to_string());
g.plus_assign(&Zeckendorf::new("1000"));
println!("{}", g.to_string());
g.plus_assign(&Zeckendorf::new("10101"));
println!("{}", g.to_string());
println!();
 
println!("Subtraction:");
g = Zeckendorf::new("1000");
g.minus_assign(&Zeckendorf::new("101"));
println!("{}", g.to_string());
g = Zeckendorf::new("10101010");
g.minus_assign(&Zeckendorf::new("1010101"));
println!("{}", g.to_string());
println!();
 
println!("Multiplication:");
g = Zeckendorf::new("1001");
g.times_assign(&Zeckendorf::new("101"));
println!("{}", g.to_string());
g = Zeckendorf::new("101010");
g.plus_assign(&Zeckendorf::new("101"));
println!("{}", g.to_string());
}
</syntaxhighlight>
{{out}}
<pre>
Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100
</pre>
=={{header|Scala}}==
{{works with|Scala|2.913.1}}
The addition is an implementation of an algorithm suggested in http[:]//arxiv.org/pdf/1207.4497.pdf: Efficient Algorithms for Zeckendorf Arithmetic.
<syntaxhighlight lang="scala">
<lang Scala>object ZA extends App {
import scala.collection.mutable.ListBuffer
import Stream._
import scala.collection.mutable.ListBuffer
 
object ZZeckendorfArithmetic extends App {
// only for comfort and result checking:
val fibs: Stream[BigInt] = {def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j,i+j); series(1,0).tail.tail.tail }
val z2i: Z => BigInt = z => (z.z.abs.toString.map(_.asDigit).reverse.zipWithIndex.map{case (v,i)=>v*fibs(i)}:\BigInt(0))(_+_)*z.z.signum
 
var fmts = Map(Z("0")->List[Z](Z("0"))) //map of Fibonacci multiples table of divisors
 
val elapsed: (=> Unit) => Long = f => {
// get multiply table from fmts
val s = System.currentTimeMillis
def mt(z: Z): List[Z] = {fmts.getOrElse(z,Nil) match {case Nil => {val e = mwv(z); fmts=fmts+(z->e); e}; case l => l}}
 
f
// multiply weight vector
 
def mwv(z: Z): List[Z] = {
(System.currentTimeMillis - s) / 1000
val wv = new ListBuffer[Z]; wv += z; wv += (z+z)
var zs = "11"; val upper = z.z.abs.toString
while ((zs.size<upper.size)) {wv += (wv.toList.last + wv.toList.reverse.tail.head); zs = "1"+zs}
wv.toList
}
val add: (Z, Z) => Z = (z1, z2) => z1 + z2
val subtract: (Z, Z) => Z = (z1, z2) => z1 - z2
// get division table (division weight vector)
defval dt(ddmultiply: (Z, ds: Z): List[=> Z] = {(z1, z2) => z1 * z2
val divide: (Z, Z) => Option[Z] = (z1, z2) => z1 / z2
val wv = new ListBuffer[Z]; mt(ds).copyToBuffer(wv)
val modulo: (Z, Z) => Option[Z] = (z1, z2) => z1 % z2
var zs = ds.z.abs.toString; val upper = dd.z.abs.toString
val ops = Map(("+", add), ("-", subtract), ("*", multiply), ("/", divide), ("%", modulo))
while ((zs.size<upper.size)) {wv += (wv.toList.last + wv.toList.reverse.tail.head); zs = "1"+zs}
val calcs = List(
wv.toList
(Z("101"), "+", Z("10100"))
}
, (Z("101"), "-", Z("10100"))
}
, (Z("101"), "*", Z("10100"))
, (Z("101"), "/", Z("10100"))
, (Z("-1010101"), "+", Z("10100"))
, (Z("-1010101"), "-", Z("10100"))
, (Z("-1010101"), "*", Z("10100"))
, (Z("-1010101"), "/", Z("10100"))
, (Z("1000101010"), "+", Z("10101010"))
, (Z("1000101010"), "-", Z("10101010"))
, (Z("1000101010"), "*", Z("10101010"))
, (Z("1000101010"), "/", Z("10101010"))
, (Z("10100"), "+", Z("1010"))
, (Z("100101"), "-", Z("100"))
, (Z("1010101010101010101"), "+", Z("-1010101010101"))
, (Z("1010101010101010101"), "-", Z("-1010101010101"))
, (Z("1010101010101010101"), "*", Z("-1010101010101"))
, (Z("1010101010101010101"), "/", Z("-1010101010101"))
, (Z("1010101010101010101"), "%", Z("-1010101010101"))
, (Z("1010101010101010101"), "+", Z("101010101010101"))
, (Z("1010101010101010101"), "-", Z("101010101010101"))
, (Z("1010101010101010101"), "*", Z("101010101010101"))
, (Z("1010101010101010101"), "/", Z("101010101010101"))
, (Z("1010101010101010101"), "%", Z("101010101010101"))
, (Z("10101010101010101010"), "+", Z("1010101010101010"))
, (Z("10101010101010101010"), "-", Z("1010101010101010"))
, (Z("10101010101010101010"), "*", Z("1010101010101010"))
, (Z("10101010101010101010"), "/", Z("1010101010101010"))
, (Z("10101010101010101010"), "%", Z("1010101010101010"))
, (Z("1010"), "%", Z("10"))
, (Z("1010"), "%", Z("-10"))
, (Z("-1010"), "%", Z("10"))
, (Z("-1010"), "%", Z("-10"))
, (Z("100"), "/", Z("0"))
, (Z("100"), "%", Z("0"))
)
val iadd: (BigInt, BigInt) => BigInt = (a, b) => a + b
val isub: (BigInt, BigInt) => BigInt = (a, b) => a - b
 
// just for result checking:
 
case class Z(var zs: String) {
import Z._
require ((zs.toSet--Set('-','0','1')==Set()) && (!zs.contains("11")))
 
varval zimul: (BigInt, BigInt) => BigInt = (zsa, b) => a * b
val idiv: (BigInt, BigInt) => Option[BigInt] = (a, b) => if (b == 0) None else Some(a / b)
override def toString = z+"Z(i:"+z2i(this)+")"
val imod: (BigInt, BigInt) => Option[BigInt] = (a, b) => if (b == 0) None else Some(a % b)
def size = z.abs.toString.size
val iops = Map(("+", iadd), ("-", isub), ("*", imul), ("/", idiv), ("%", imod))
 
case class Z(var zs: String) {
//--- fa(summand1.z,summand2.z) --------------------------
 
val fa: (BigInt,BigInt) => BigInt = (z1, z2) => {
import Z._
val v =z1.toString.map(_.asDigit).reverse.padTo(5,0).zipAll(z2.toString.map(_.asDigit).reverse, 0, 0)
 
val arr1 = (v.map(p=>p._1+p._2):+0 reverse).toArray
require((zs.toSet -- Set('-', '0', '1') == Set()) && (!zs.contains("11")))
(0 to arr1.size-4) foreach {i=> //stage1
 
val a = arr1.slice(i,i+4).toList
//--- fa(summand1.z,summand2.z) --------------------------
val b = (a:\"")(_+_) dropRight 1
val fa: val(BigInt, a1BigInt) => bBigInt match= (z1, z2) => {
val v = z1.toString.toCharArray.map(_.asDigit).reverse.padTo(5, 0).zipAll(z2.toString.toCharArray.map(_.asDigit).reverse, 0, 0)
case "020" => List(1,0,0, a(3)+1)
val arr1 = case "030"(v.map(p => List(1,1,0,p._1 a(3+ p._2) :+1 0).reverse
(0 to arr1.length - case4) "021"foreach { i => List(1,1,0, a(3))//stage1
val case "012"a => Listarr1.slice(1,0,1i, a(3)i + 4).toList
val b case= _a.foldRight("")("" + _ + _) =>dropRight a1
val a1 = b match {
case "020" => List(1, 0, 0, a(3) + 1)
case "030" => List(1, 1, 0, a(3) + 1)
case "021" => List(1, 1, 0, a(3))
case "012" => List(1, 0, 1, a(3))
case _ => a
}
0 to 3 foreach { j => arr1(j + i) = a1(j) }
}
0val toarr2 3 foreach {j=> arr1.foldRight(j+i"")("" =+ a1(j_ + _)}
.replace("0120", "1010").replace("030", "111").replace("003", "100").replace("020", "101")
}
.replace("003", "100").replace("012", "101").replace("021", "110")
val arr2 = (arr1:\"")(_+_)
.replace("012002", "101010").replace("03003", "111").replace("003","100").replace("020","10111")
.reverse.toArray
.replace("003","100").replace("012","101").replace("021","110")
(0 to arr2.length - 3) foreach { i => //stage2, step1
.replace("02","10").replace("03","11")
val a = arr2.slice(i, i + 3).toList
.reverse.toArray
val b = a.foldRight("")("" + _ + _)
(0 to arr2.size-3) foreach {i=> //stage2, step1
val aa1 = arr2.slice(i,i+3).toListb match {
val b = (a:\ case "110") => List(_+_'0', '0', '1')
val a1 = b matchcase _ => {a
}
case "110" => List('0','0','1')
0 to case2 _foreach { j => arr2(j + i) => aa1(j) }
}
0val toarr3 2 foreach {j=> arr2.foldRight(j+i"")("" =+ a1_ + _).concat(j"0")}.reverse.toArray
(0 to arr3.length - 3) foreach { i => //stage2, step2
}
val arr3a = arr3.slice(arr2:\"")(_i, i +_ 3).concat("0").reverse.toArraytoList
val b = a.foldRight("")("" + _ + _)
(0 to arr3.size-3) foreach {i=> //stage2, step2
val aa1 = arr3.slice(i,i+3).toListb match {
val b = (a:\ case "011") => List(_+_'1', '0', '0')
val a1 = b matchcase _ => {a
}
case "011" => List('1','0','0')
0 to case2 _foreach { j => arr3(j + i) => aa1(j) }
}
0 to 2 foreach {j=>BigInt(arr3.foldRight(j+i"")("" =+ a1(j_ + _))}
}
//--- fs(minuend.z,subtrahend.z) -------------------------
BigInt((arr3:\"")(_+_))
val fs: (BigInt, BigInt) => BigInt = (min, sub) => {
}
val zmvr = min.toString.toCharArray.map(_.asDigit).reverse
val zsvr = sub.toString.toCharArray.map(_.asDigit).reverse.padTo(zmvr.length, 0)
val v = zmvr.zipAll(zsvr, 0, 0).reverse
val last = v.length - 1
val zma = zmvr.reverse.toArray
 
val zsa = zsvr.reverse.toArray
//--- fs(minuend.z,subtrahend.z) -------------------------
for (i <- (0 to last).reverse) {
val fs: (BigInt,BigInt) => BigInt = (min,sub) => {
val zmvre = min.toString.mapzma(i) - zsa(_.asDigiti).reverse
if (e < 0) {
val zsvr = sub.toString.map(_.asDigit).reverse.padTo(zmvr.size,0)
val v zma(i - 1) = zmvr.zipAllzma(zsvr,i 0,- 01).reverse - 1
val last zma(i) = v.size-10
val part = Z(((i to last).map(zma(_))).foldRight("")("" + _ + _))
val zma = zmvr.reverse.toArray; val zsa = zsvr.reverse.toArray
val carry = Z("1".padTo(last - i, "0").foldRight("")("" + _ + _))
for (i <- 0 to last reverse) {
val esum = zma(i)-zsa(i)part + carry
 
if (e<0) {
zma(i-1) val sums = zma(i-1)-1sum.z.toString
(1 to sum.size) foreach { j => zma(last - sum.size + j) = sums(j - 1).asDigit }
zma(i) = 0
val part =if Z(((zma(i to- last).map(zma(_))):\"")(_+_1) < 0) {
val carry = Z( for ("1".padTo(lastj <-i," (0"):\"")(_+_ until i).reverse) {
val sum = part + carry; valif sums(zma(j) =< sum.z.toString0) {
(1 to sum.size) foreach {j=> zma(last-sum.size+j - 1) =sums zma(j - 1).asDigit} - 1
if ( zma(i-1)<0j) {= 0
for val part = Z(((j <-to 0last).map(zma(_))).foldRight("")("" to+ i-1_ reverse)+ {_))
if val carry = Z(zma"1".padTo(last - j)<, "0").foldRight("")("" {+ _ + _))
zma(j-1) val sum = zma(j-1)-1part + carry
 
zma(j) = 0
val partsums = Z((((j to last)sum.z.map(zma(_))):\"")(_+_))toString
val carry = Z(("1" to sum.padTosize) foreach { k => zma(last -j,"0"):\"" sum.size + k) = sums(_+_)k - 1).asDigit }
val sum = part + carry; val sums = sum.z.toString}
(1 to sum.size) foreach {k=>zma(last-sum.size+k)=sums(k-1).asDigit}
}
}
}
else zma(i) = e
zsa(i) = 0
}
else BigInt(zma.foldRight(i"")("" =+ e_ + _))
zsa(i) = 0
}
//--- fm(multiplicand.z,multplier.z) ---------------------
BigInt((zma:\"")(_+_))
val fm: (BigInt, BigInt) => BigInt = (mc, mp) => {
}
val mct = mt(Z(mc.toString))
val mpxi = mp.toString.reverse.toCharArray.map(_.asDigit).zipWithIndex.filter(_._1 != 0).map(_._2)
mpxi.foldRight(Z("0"))((fi, sum) => sum + mct(fi)).z
}
//--- fd(dividend.z,divisor.z) ---------------------------
val fd: (BigInt, BigInt) => BigInt = (dd, ds) => {
val dst = dt(Z(dd.toString), Z(ds.toString)).reverse
var diff = Z(dd.toString)
val zd = ListBuffer[String]()
0 until dst.length foreach { i =>
if (dst(i) > diff) zd += "0" else {
diff = diff - dst(i)
 
zd += "1"
//--- fm(multiplicand.z,multplier.z) ---------------------
}
val fm: (BigInt,BigInt) => BigInt = (mc, mp) => {
}
val mct = mt(Z(mc.toString))
BigInt(zd.mkString)
val mpxi = mp.toString.reverse.map(_.asDigit).zipWithIndex.filter(_._1 != 0).map(_._2)
}
(mpxi:\Z("0"))((fi,sum)=>sum+mct(fi)).z
val fasig: (Z, Z) => Int = (z1, z2) => if (z1.z.abs > z2.z.abs) z1.z.signum else z2.z.signum
}
val fssig: (Z, Z) => Int = (z1, z2) =>
if ((z1.z.abs > z2.z.abs && z1.z.signum > 0) || (z1.z.abs < z2.z.abs && z1.z.signum < 0)) 1 else -1
var z: BigInt = BigInt(zs)
 
override def toString: String = "" + z + "Z(i:" + z2i(this) + ")"
//--- fd(dividend.z,divisor.z) ---------------------------
 
val fd: (BigInt,BigInt) => BigInt = (dd, ds) => {
valdef dstsize: Int = dt(Z(ddz.toString),Z(dsabs.toString)).reverselength
 
var diff = Z(dd.toString)
valdef zd++ : Z = ListBuffer[String](){
val za = this + Z("1")
(0 to dst.size-1) foreach {i=>
 
if (dst(i)>diff) zd+="0" else {diff = diff-dst(i); zd+="1"}
this.zs = za.zs
 
this.z = za.z
 
this
}
BigInt(zd.mkString)
}
val fasig: (Z, Z) => Int = (z1, z2) => if (z1.z.abs>z2.z.abs) z1.z.signum else z2.z.signum
val fssig: (Z, Z) => Int = (z1, z2) =>
if ((z1.z.abs>z2.z.abs && z1.z.signum>0)||(z1.z.abs<z2.z.abs && z1.z.signum<0)) 1 else -1
 
def +(that: Z): Z =
if (this == Z("0")) that
else if (that == Z("0")) this
else if (this.z.signum == that.z.signum) Z((fa(this.z.abs.max(that.z.abs), this.z.abs.min(that.z.abs)) * this.z.signum).toString)
else if (this.z.abs == that.z.abs) Z("0")
else Z((fs(this.z.abs.max(that.z.abs), this.z.abs.min(that.z.abs)) * fasig(this, that)).toString)
 
def -- : Z = {
def ++ : Z = {val za = this + Z("1"); this.zs = za.zs; this.z = za.z; this}
val zs = this - Z("1")
 
def -(that: Z): Z this.zs = zs.zs
if (this==Z("0")) Z((that.z*(-1)).toString)
else if (that==Z("0")) this
else if (this.z.signum != that.z.signum) Z((fa(this.z.abs.max(that.z.abs),this.z.abs.min(that.z.abs))*this.z.signum).toString)
else if (this.z.abs == that.z.abs) Z("0")
else Z((fs(this.z.abs.max(that.z.abs),this.z.abs.min(that.z.abs))*fssig(this, that)).toString)
 
def -- : Z = {val zs = this - Z("1"); this.zs = zs.zs; this.z = zs.z; this}
 
def * (that: Z): Z = this
}
if (this==Z("0")||that==Z("0")) Z("0")
else if (this==Z("1")) that
else if (that==Z("1")) this
else Z((fm(this.z.abs.max(that.z.abs),this.z.abs.min(that.z.abs))*this.z.signum*that.z.signum).toString)
 
def / def -(that: Z): Option[Z] =
if (thatthis == Z("0")) NoneZ((that.z * (-1)).toString)
else if (thisthat == Z("0")) Some(Z("0"))this
else if (this.z.signum != that.z.signum) Z((fa(this.z.abs.max(that.z.abs), this.z.abs.min(that.z.abs)) * this.z.signum).toString)
else if (that==Z("1")) Some(Z("1"))
else if (this.z.abs <== that.z.abs) Some(Z("0"))
else if else Z((fs(this.z == .abs.max(that.z.abs), Somethis.z.abs.min(Z("1"that.z.abs)) * fssig(this, that)).toString)
else Some(Z((fd(this.z.abs.max(that.z.abs),this.z.abs.min(that.z.abs))*this.z.signum*that.z.signum).toString))
 
def % (that: Z): Option[Z] =
if (that == Z("0")) None
else if (this == Z("0")) Some(Z("0"))
else if (that == Z("1")) Some(Z("0"))
else if (this.z.abs < that.z.abs) Some(this)
else if (this.z == that.z) Some(Z("0") )
else this / that match {case None => None; case Some(z) => Some(this-z*that)}
case None => None
 
def < (that: Z): Boolean case Some(z) => Some(this.z <- z * that.z)
}
def <= (that: Z): Boolean = this.z <= that.z
def > (that: Z): Boolean = this.z > that.z
def >= (that: Z): Boolean = this.z >= that.z
}
 
def *(that: Z): Z =
val elapsed: (=> Unit) => Long = f => {val s = System.currentTimeMillis; f; (System.currentTimeMillis - s)/1000}
if (this == Z("0") || that == Z("0")) Z("0")
else if (this == Z("1")) that
else if (that == Z("1")) this
else Z((fm(this.z.abs.max(that.z.abs), this.z.abs.min(that.z.abs)) * this.z.signum * that.z.signum).toString)
 
val add: def /(that: (Z,Z): => Option[Z] = (z1,z2) => z1+z2
val subtract: if (Z,Z)that =>= Z = (z1,z2"0")) => z1-z2None
else if (this == Z("0")) Some(Z("0"))
val multiply: (Z,Z) => Z = (z1,z2) => z1*z2
else if (that == Z("1")) Some(Z("1"))
val divide: (Z,Z) => Option[Z] = (z1,z2) => z1/z2
else if (this.z.abs < that.z.abs) Some(Z("0"))
val modulo: (Z,Z) => Option[Z] = (z1,z2) => z1%z2
else if (this.z == that.z) Some(Z("1"))
else Some(Z((fd(this.z.abs.max(that.z.abs), this.z.abs.min(that.z.abs)) * this.z.signum * that.z.signum).toString))
 
def <(that: Z): Boolean = this.z < that.z
val ops = Map(("+",add),("-",subtract),("*",multiply),("/",divide),("%",modulo))
 
def <=(that: Z): Boolean = this.z <= that.z
val calcs = List(
(Z("101"),"+",Z("10100"))
, (Z("101"),"-",Z("10100"))
, (Z("101"),"*",Z("10100"))
, (Z("101"),"/",Z("10100"))
, (Z("-1010101"),"+",Z("10100"))
, (Z("-1010101"),"-",Z("10100"))
, (Z("-1010101"),"*",Z("10100"))
, (Z("-1010101"),"/",Z("10100"))
, (Z("1000101010"),"+",Z("10101010"))
, (Z("1000101010"),"-",Z("10101010"))
, (Z("1000101010"),"*",Z("10101010"))
, (Z("1000101010"),"/",Z("10101010"))
, (Z("10100"),"+",Z("1010"))
, (Z("100101"),"-",Z("100"))
, (Z("1010101010101010101"),"+",Z("-1010101010101"))
, (Z("1010101010101010101"),"-",Z("-1010101010101"))
, (Z("1010101010101010101"),"*",Z("-1010101010101"))
, (Z("1010101010101010101"),"/",Z("-1010101010101"))
, (Z("1010101010101010101"),"%",Z("-1010101010101"))
, (Z("1010101010101010101"),"+",Z("101010101010101"))
, (Z("1010101010101010101"),"-",Z("101010101010101"))
, (Z("1010101010101010101"),"*",Z("101010101010101"))
, (Z("1010101010101010101"),"/",Z("101010101010101"))
, (Z("1010101010101010101"),"%",Z("101010101010101"))
, (Z("10101010101010101010"),"+",Z("1010101010101010"))
, (Z("10101010101010101010"),"-",Z("1010101010101010"))
, (Z("10101010101010101010"),"*",Z("1010101010101010"))
, (Z("10101010101010101010"),"/",Z("1010101010101010"))
, (Z("10101010101010101010"),"%",Z("1010101010101010"))
, (Z("1010"),"%",Z("10"))
, (Z("1010"),"%",Z("-10"))
, (Z("-1010"),"%",Z("10"))
, (Z("-1010"),"%",Z("-10"))
, (Z("100"),"/",Z("0"))
, (Z("100"),"%",Z("0"))
)
 
def >(that: Z): Boolean = this.z > that.z
// just for result checking:
import Z._
val iadd: (BigInt,BigInt) => BigInt = (a,b) => a+b
val isub: (BigInt,BigInt) => BigInt = (a,b) => a-b
val imul: (BigInt,BigInt) => BigInt = (a,b) => a*b
val idiv: (BigInt,BigInt) => Option[BigInt] = (a,b) => if (b==0) None else Some(a/b)
val imod: (BigInt,BigInt) => Option[BigInt] = (a,b) => if (b==0) None else Some(a%b)
val iops = Map(("+",iadd),("-",isub),("*",imul),("/",idiv),("%",imod))
 
def >=(that: Z): Boolean = this.z >= that.z
println("elapsed time: "+elapsed{
calcs foreach {case (op1,op,op2) => println(op1+" "+op+" "+op2+" = "
+{(ops(op))(op1,op2) match {case None => None; case Some(z) => z; case z => z}}
.ensuring{x=>(iops(op))(z2i(op1),z2i(op2)) match {case None => None == x; case Some(i) => i == z2i(x.asInstanceOf[Z]); case i => i == z2i(x.asInstanceOf[Z])}})}
}+" sec"
)
 
}
}</lang>
 
object Z {
// only for comfort and result checking:
val fibs: LazyList[BigInt] = {
def series(i: BigInt, j: BigInt): LazyList[BigInt] = i #:: series(j, i + j)
 
series(1, 0).tail.tail.tail
}
val z2i: Z => BigInt = z => z.z.abs.toString.toCharArray.map(_.asDigit).reverse.zipWithIndex.map { case (v, i) => v * fibs(i) }.foldRight(BigInt(0))(_ + _) * z.z.signum
 
var fmts: Map[Z, List[Z]] = Map(Z("0") -> List[Z](Z("0"))) //map of Fibonacci multiples table of divisors
 
// get division table (division weight vector)
def dt(dd: Z, ds: Z): List[Z] = {
val wv = new ListBuffer[Z]
wv ++= mt(ds)
var zs = ds.z.abs.toString
val upper = dd.z.abs.toString
while ((zs.length < upper.length)) {
wv += (wv.toList.last + wv.toList.reverse.tail.head)
 
zs = "1" + zs
}
wv.toList
}
 
// get multiply table from fmts
def mt(z: Z): List[Z] = {
fmts.getOrElse(z, Nil) match {
case Nil =>
val e = mwv(z)
fmts = fmts + (z -> e)
e
case l => l
}
}
 
// multiply weight vector
def mwv(z: Z): List[Z] = {
val wv = new ListBuffer[Z]
wv += z
wv += (z + z)
var zs = "11"
val upper = z.z.abs.toString
while ((zs.length < upper.length)) {
wv += (wv.toList.last + wv.toList.reverse.tail.head)
 
zs = "1" + zs
}
wv.toList
}
}
 
println("elapsed time: " + elapsed {
calcs foreach { case (op1, op, op2) => println("" + op1 + " " + op + " " + op2 + " = "
+ {
(ops(op)) (op1, op2) match {
case None => None
 
case Some(z) => z
 
case z => z
}
}
.ensuring { x =>
(iops(op)) (z2i(op1), z2i(op2)) match {
case None => None == x
 
case Some(i) => i == z2i(x.asInstanceOf[Z])
 
case i => i == z2i(x.asInstanceOf[Z])
}
})
}
} + " sec"
)
 
}
</syntaxhighlight>
Output:
<pre style="height: 30ex; overflow: scroll">101Z(i:4) + 10100Z(i:11) = 100010Z(i:15)
Line 2,787 ⟶ 4,604:
 
=={{header|Tcl}}==
{{trans|Perl 6Raku}}<!-- mostly for the technique of using incr/decr -->
<langsyntaxhighlight lang="tcl">namespace eval zeckendorf {
# Want to use alternate symbols? Change these
variable zero "0"
Line 2,862 ⟶ 4,679:
namespace export \[a-y\]*
namespace ensemble create
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts [zeckendorf add "10100" "1010"]
puts [zeckendorf sub "10100" "1010"]
puts [zeckendorf mul "10100" "1010"]
puts [zeckendorf div "10100" "1010"]
puts [zeckendorf div [zeckendorf mul "10100" "1010"] "1010"]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,876 ⟶ 4,693:
1
10100
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
 
Class Zeckendorf
Implements IComparable(Of Zeckendorf)
 
Private Shared ReadOnly dig As String() = {"00", "01", "10"}
Private Shared ReadOnly dig1 As String() = {"", "1", "10"}
 
Private dVal As Integer = 0
Private dLen As Integer = 0
 
Public Sub New(Optional x As String = "0")
Dim q = 1
Dim i = x.Length - 1
dLen = i \ 2
 
Dim z = Asc("0")
While i >= 0
Dim a = Asc(x(i))
dVal += (a - z) * q
q *= 2
i -= 1
End While
End Sub
 
Private Sub A(n As Integer)
Dim i = n
While True
If dLen < i Then
dLen = i
End If
Dim j = (dVal >> (i * 2)) And 3
If j = 0 OrElse j = 1 Then
Return
ElseIf j = 2 Then
If ((dVal >> ((i + 1) * 2)) And 1) <> 1 Then
Return
End If
dVal += 1 << (i * 2 + 1)
Return
ElseIf j = 3 Then
Dim temp = 3 << (i * 2)
temp = temp Xor -1
dVal = dVal And temp
B((i + 1) * 2)
End If
i += 1
End While
End Sub
 
Private Sub B(pos As Integer)
If pos = 0 Then
Inc()
Return
End If
If ((dVal >> pos) And 1) = 0 Then
dVal += 1 << pos
A(pos \ 2)
If pos > 1 Then
A(pos \ 2 - 1)
End If
Else
Dim temp = 1 << pos
temp = temp Xor -1
dVal = dVal And temp
B(pos + 1)
B(pos - If(pos > 1, 2, 1))
End If
End Sub
 
Private Sub C(pos As Integer)
If ((dVal >> pos) And 1) = 1 Then
Dim temp = 1 << pos
temp = temp Xor -1
dVal = dVal And temp
Return
End If
C(pos + 1)
If pos > 0 Then
B(pos - 1)
Else
Inc()
End If
End Sub
 
Public Function Inc() As Zeckendorf
dVal += 1
A(0)
Return Me
End Function
 
Public Function Copy() As Zeckendorf
Dim z As New Zeckendorf With {
.dVal = dVal,
.dLen = dLen
}
Return z
End Function
 
Public Sub PlusAssign(other As Zeckendorf)
Dim gn = 0
While gn < (other.dLen + 1) * 2
If ((other.dVal >> gn) And 1) = 1 Then
B(gn)
End If
gn += 1
End While
End Sub
 
Public Sub MinusAssign(other As Zeckendorf)
Dim gn = 0
While gn < (other.dLen + 1) * 2
If ((other.dVal >> gn) And 1) = 1 Then
C(gn)
End If
gn += 1
End While
While (((dVal >> dLen * 2) And 3) = 0) OrElse dLen = 0
dLen -= 1
End While
End Sub
 
Public Sub TimesAssign(other As Zeckendorf)
Dim na = other.Copy
Dim nb = other.Copy
Dim nt As Zeckendorf
Dim nr As New Zeckendorf
Dim i = 0
While i < (dLen + 1) * 2
If ((dVal >> i) And 1) > 0 Then
nr.PlusAssign(nb)
End If
nt = nb.Copy
nb.PlusAssign(na)
na = nt.Copy
i += 1
End While
dVal = nr.dVal
dLen = nr.dLen
End Sub
 
Public Function CompareTo(other As Zeckendorf) As Integer Implements IComparable(Of Zeckendorf).CompareTo
Return dVal.CompareTo(other.dVal)
End Function
 
Public Overrides Function ToString() As String
If dVal = 0 Then
Return "0"
End If
 
Dim idx = (dVal >> (dLen * 2)) And 3
Dim sb As New StringBuilder(dig1(idx))
Dim i = dLen - 1
While i >= 0
idx = (dVal >> (i * 2)) And 3
sb.Append(dig(idx))
i -= 1
End While
Return sb.ToString
End Function
End Class
 
Sub Main()
Console.WriteLine("Addition:")
Dim g As New Zeckendorf("10")
g.PlusAssign(New Zeckendorf("10"))
Console.WriteLine(g)
g.PlusAssign(New Zeckendorf("10"))
Console.WriteLine(g)
g.PlusAssign(New Zeckendorf("1001"))
Console.WriteLine(g)
g.PlusAssign(New Zeckendorf("1000"))
Console.WriteLine(g)
g.PlusAssign(New Zeckendorf("10101"))
Console.WriteLine(g)
Console.WriteLine()
 
Console.WriteLine("Subtraction:")
g = New Zeckendorf("1000")
g.MinusAssign(New Zeckendorf("101"))
Console.WriteLine(g)
g = New Zeckendorf("10101010")
g.MinusAssign(New Zeckendorf("1010101"))
Console.WriteLine(g)
Console.WriteLine()
 
Console.WriteLine("Multiplication:")
g = New Zeckendorf("1001")
g.TimesAssign(New Zeckendorf("101"))
Console.WriteLine(g)
g = New Zeckendorf("101010")
g.PlusAssign(New Zeckendorf("101"))
Console.WriteLine(g)
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import strings
const (
dig = ["00", "01", "10"]
dig1 = ["", "1", "10"]
)
struct Zeckendorf {
mut:
d_val int
d_len int
}
fn new_zeck(xx string) Zeckendorf {
mut z := Zeckendorf{}
mut x := xx
if x == "" {
x = "0"
}
mut q := 1
mut i := x.len - 1
z.d_len = i / 2
for ; i >= 0; i-- {
z.d_val += int(x[i]-'0'[0]) * q
q *= 2
}
return z
}
fn (mut z Zeckendorf) a(ii int) {
mut i:=ii
for ; ; i++ {
if z.d_len < i {
z.d_len = i
}
j := (z.d_val >> u32(i*2)) & 3
if j in [0, 1] {
return
} else if j==2 {
if ((z.d_val >> (u32(i+1) * 2)) & 1) != 1 {
return
}
z.d_val += 1 << u32(i*2+1)
return
} else {// 3
z.d_val &= ~(3 << u32(i*2))
z.b((i + 1) * 2)
}
}
}
fn (mut z Zeckendorf) b(p int) {
mut pos := p
if pos == 0 {
z.inc()
return
}
if ((z.d_val >> u32(pos)) & 1) == 0 {
z.d_val += 1 << u32(pos)
z.a(pos / 2)
if pos > 1 {
z.a(pos/2 - 1)
}
} else {
z.d_val &= ~(1 << u32(pos))
z.b(pos + 1)
mut temp := 1
if pos > 1 {
temp = 2
}
z.b(pos - temp)
}
}
fn (mut z Zeckendorf) c(p int) {
mut pos := p
if ((z.d_val >> u32(pos)) & 1) == 1 {
z.d_val &= ~(1 << u32(pos))
return
}
z.c(pos + 1)
if pos > 0 {
z.b(pos - 1)
} else {
z.inc()
}
}
fn (mut z Zeckendorf) inc() {
z.d_val++
z.a(0)
}
fn (mut z1 Zeckendorf) plus_assign(z2 Zeckendorf) {
for gn := 0; gn < (z2.d_len+1)*2; gn++ {
if ((z2.d_val >> u32(gn)) & 1) == 1 {
z1.b(gn)
}
}
}
fn (mut z1 Zeckendorf) minus_assign(z2 Zeckendorf) {
for gn := 0; gn < (z2.d_len+1)*2; gn++ {
if ((z2.d_val >> u32(gn)) & 1) == 1 {
z1.c(gn)
}
}
for z1.d_len > 0 && ((z1.d_val>>u32(z1.d_len*2))&3) == 0 {
z1.d_len--
}
}
fn (mut z1 Zeckendorf) times_assign(z2 Zeckendorf) {
mut na := z2.copy()
mut nb := z2.copy()
mut nr := Zeckendorf{}
for i := 0; i <= (z1.d_len+1)*2; i++ {
if ((z1.d_val >> u32(i)) & 1) > 0 {
nr.plus_assign(nb)
}
nt := nb.copy()
nb.plus_assign(na)
na = nt.copy()
}
z1.d_val = nr.d_val
z1.d_len = nr.d_len
}
fn (z Zeckendorf) copy() Zeckendorf {
return Zeckendorf{z.d_val, z.d_len}
}
fn (z1 Zeckendorf) compare(z2 Zeckendorf) int {
if z1.d_val < z2.d_val {
return -1
} else if z1.d_val > z2.d_val {
return 1
} else {
return 0
}
}
fn (z Zeckendorf) str() string {
if z.d_val == 0 {
return "0"
}
mut sb := strings.new_builder(128)
sb.write_string(dig1[(z.d_val>>u32(z.d_len*2))&3])
for i := z.d_len - 1; i >= 0; i-- {
sb.write_string(dig[(z.d_val>>u32(i*2))&3])
}
return sb.str()
}
fn main() {
println("Addition:")
mut g := new_zeck("10")
g.plus_assign(new_zeck("10"))
println(g)
g.plus_assign(new_zeck("10"))
println(g)
g.plus_assign(new_zeck("1001"))
println(g)
g.plus_assign(new_zeck("1000"))
println(g)
g.plus_assign(new_zeck("10101"))
println(g)
println("\nSubtraction:")
g = new_zeck("1000")
g.minus_assign(new_zeck("101"))
println(g)
g = new_zeck("10101010")
g.minus_assign(new_zeck("1010101"))
println(g)
println("\nMultiplication:")
g = new_zeck("1001")
g.times_assign(new_zeck("101"))
println(g)
g = new_zeck("101010")
g.plus_assign(new_zeck("101"))
println(g)
}</syntaxhighlight>
 
{{out}}
<pre>
Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-trait}}
<syntaxhighlight lang="wren">import "./trait" for Comparable
 
class Zeckendorf is Comparable {
static dig { ["00", "01", "10"] }
static dig1 { ["", "1", "10"] }
 
construct new(x) {
var q = 1
var i = x.count - 1
_dLen = (i / 2).floor
_dVal = 0
while (i >= 0) {
_dVal = _dVal + (x[i].bytes[0] - 48) * q
q = q * 2
i = i - 1
}
}
 
dLen { _dLen }
dVal { _dVal }
 
dLen=(v) { _dLen = v }
dVal=(v) { _dVal = v }
 
a(n) {
var i = n
while (true) {
if (_dLen < i) _dLen = i
var j = (_dVal >> (i * 2)) & 3
if (j == 0 || j == 1) return
if (j == 2) {
if (((_dVal >> ((i + 1) * 2)) & 1) != 1) return
_dVal = _dVal + (1 << (i * 2 + 1))
return
}
if (j == 3) {
_dVal = _dVal & ~(3 << (i * 2))
b((i + 1) * 2)
}
i = i + 1
}
}
 
b(pos) {
if (pos == 0) {
var thiz = this
thiz.inc
return
}
if (((_dVal >> pos) & 1) == 0) {
_dVal = _dVal + (1 << pos)
a((pos / 2).floor)
if (pos > 1) a((pos / 2).floor - 1)
} else {
_dVal = _dVal & ~(1 << pos)
b(pos + 1)
b(pos - ((pos > 1) ? 2 : 1))
}
}
 
c(pos) {
if (((_dVal >> pos) & 1) == 1) {
_dVal = _dVal & ~(1 << pos)
return
}
c(pos + 1)
if (pos > 0) {
b(pos - 1)
} else {
var thiz = this
thiz.inc
}
}
 
inc {
_dVal = _dVal + 1
a(0)
return this
}
 
plusAssign(other) {
for (gn in 0...(other.dLen + 1) * 2) {
if (((other.dVal >> gn) & 1) == 1) b(gn)
}
}
 
minusAssign(other) {
for (gn in 0...(other.dLen + 1) * 2) {
if (((other.dVal >> gn) & 1) == 1) c(gn)
}
while ((((_dVal >> _dLen * 2) & 3) == 0) || (_dLen == 0)) _dLen = _dLen - 1
}
 
timesAssign(other) {
var na = other.copy()
var nb = other.copy()
var nr = Zeckendorf.new("0")
for (i in 0..(_dLen + 1) * 2) {
if (((_dVal >> i) & 1) > 0) nr.plusAssign(nb)
var nt = nb.copy()
nb.plusAssign(na)
na = nt.copy()
}
_dVal = nr.dVal
_dLen = nr.dLen
}
 
compare(other) { (_dVal - other.dVal).sign }
 
toString {
if (_dVal == 0) return "0"
var sb = Zeckendorf.dig1[(_dVal >> (_dLen * 2)) & 3]
if (_dLen > 0) {
for (i in _dLen - 1..0) {
sb = sb + Zeckendorf.dig[(_dVal >> (i * 2)) & 3]
}
}
return sb
}
 
copy() {
var z = Zeckendorf.new("0")
z.dVal = _dVal
z.dLen = _dLen
return z
}
}
 
var Z = Zeckendorf // type alias
System.print("Addition:")
var g = Z.new("10")
g.plusAssign(Z.new("10"))
System.print(g)
g.plusAssign(Z.new("10"))
System.print(g)
g.plusAssign(Z.new("1001"))
System.print(g)
g.plusAssign(Z.new("1000"))
System.print(g)
g.plusAssign(Z.new("10101"))
System.print(g)
System.print("\nSubtraction:")
g = Z.new("1000")
g.minusAssign(Z.new("101"))
System.print(g)
g = Z.new("10101010")
g.minusAssign(Z.new("1010101"))
System.print(g)
System.print("\nMultiplication:")
g = Z.new("1001")
g.timesAssign(Z.new("101"))
System.print(g)
g = Z.new("101010")
g.plusAssign(Z.new("101"))
System.print(g)</syntaxhighlight>
 
{{out}}
<pre>
Addition:
101
1001
10101
100101
1010000
 
Subtraction:
1
1000000
 
Multiplication:
1000100
1000100
</pre>
 
1,480

edits