Zeckendorf arithmetic: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 73:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Zeckendorf
Int dLen
dVal = 0
Line 215:
g = Zeckendorf(‘101010’)
g = g + Zeckendorf(‘101’)
print(g)</langsyntaxhighlight>
 
{{out}}
Line 237:
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Line 425:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 444:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
 
Line 632:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 651:
=={{header|C++}}==
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">// For a class N which implements Zeckendorf numbers:
// I define an increment operation ++()
// I define a comparison operation <=(other N)
Line 723:
return os;
}
</syntaxhighlight>
</lang>
===Testing===
The following tests addtition:
<langsyntaxhighlight lang="cpp">int main(void) {
N G;
G = 10N;
Line 740:
std::cout << G << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 750:
</pre>
The following tests subtraction:
<langsyntaxhighlight lang="cpp">int main(void) {
N G;
G = 1000N;
Line 759:
std::cout << G << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 766:
</pre>
The following tests multiplication:
<langsyntaxhighlight lang="cpp">
int main(void) {
N G = 1001N;
Line 776:
std::cout << G << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 785:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
int inv(int a) {
Line 956:
g += "101".Z;
writeln(g);
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 976:
{{trans|C++}}
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
 
const dig = new string[]{"00","01","10"};
Line 1,224:
n += 101n;
console.printLine(n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,243:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,426:
g.PlusAssign(NewZeck("101"))
fmt.Println(g)
}</langsyntaxhighlight>
 
{{out}}
Line 1,452:
Addition and subtraction are done using cellular automata. Conversion from integers, multiplication and division are implemented via generalized Fibonacci series (Zeckendorf tables).
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE LambdaCase #-}
import Data.List (find, mapAccumL)
import Control.Arrow (first, second)
Line 1,637:
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</langsyntaxhighlight>
 
<pre>λ> 15 :: Fib
Line 1,673:
 
=={{header|J}}==
Loosely based on the [[#Perl|perl]] implementation:<langsyntaxhighlight Jlang="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 }}
Line 1,687:
if. 2=s do. y,1 else. y end.
}}
zge=: {{ cmp=. x -/@,: y while. (#cmp)*0={:cmp do. cmp=. }:cmp end. 0<:{:cmp }}</langsyntaxhighlight>
 
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:<langsyntaxhighlight Jlang="j"> 1 zadd&.zform 1
10
10 zadd&.zform 10
Line 1,708:
100101
100001000001 zdiv&.zform 100101
100010</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight Javalang="java">import java.util.List;
 
public class Zeckendorf implements Comparable<Zeckendorf> {
Line 1,898:
System.out.println(g);
}
}</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 1,917:
=={{header|Julia}}==
Influenced by the format of the Tcl and Raku 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 2,089:
 
zeckendorftest()
</langsyntaxhighlight>{{output}}<pre>
Addition:
101
Line 2,112:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
class Zeckendorf(x: String = "0") : Comparable<Zeckendorf> {
Line 2,267:
g += "101".Z
println(g)
}</langsyntaxhighlight>
 
{{out}}
Line 2,289:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">type Zeckendorf = object
dVal: Natural
dLen: Natural
Line 2,430:
g = initZeckendorf("101010")
g += initZeckendorf("101")
echo g</langsyntaxhighlight>
 
{{out}}
Line 2,449:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Zeckendorf_arithmetic
Line 2,569:
++$z for 1 .. $value;
return $z;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,609:
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).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,884:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,941:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="text">(seed (in "/dev/urandom" (rd 8)))
 
(de unpad (Lst)
Line 3,073:
(test (numz (- X 1)) (decz (numz X))) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import copy
 
class Zeckendorf:
Line 3,219:
g = Zeckendorf("101010")
g = g + Zeckendorf("101")
print g</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 3,239:
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 3,434:
(example '/ zeck-quotient 9876 1000)
(example '% zeck-remainder 9876 1000)
</syntaxhighlight>
</lang>
 
{{output}}
Line 3,464:
less than '''ltz'''
 
<syntaxhighlight lang="raku" perl6line>my $z1 = '1'; # glyph to use for a '1'
my $z0 = '0'; # glyph to use for a '0'
 
Line 3,562:
printf $fmt, "$zeck *z {z('101001')}", $zeck *z= z('101001'), '# multiplication';
 
printf $fmt, "$zeck /z {z('100')}", $zeck /z= z('100'), '# division';</langsyntaxhighlight>
 
'''Testing Output'''
Line 3,618:
{{works with|Scala|2.13.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>
import scala.collection.mutable.ListBuffer
 
Line 3,950:
 
}
</syntaxhighlight>
</lang>
Output:
<pre style="height: 30ex; overflow: scroll">101Z(i:4) + 10100Z(i:11) = 100010Z(i:15)
Line 3,991:
=={{header|Tcl}}==
{{trans|Raku}}<!-- 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 4,065:
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 4,083:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 4,280:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Addition:
Line 4,299:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import strings
const (
dig = ["00", "01", "10"]
Line 4,482:
g.plus_assign(new_zeck("101"))
println(g)
}</langsyntaxhighlight>
 
{{out}}
Line 4,505:
{{trans|Kotlin}}
{{libheader|Wren-trait}}
<langsyntaxhighlight lang="ecmascript">import "/trait" for Comparable
 
class Zeckendorf is Comparable {
Line 4,659:
g = Z.new("101010")
g.plusAssign(Z.new("101"))
System.print(g)</langsyntaxhighlight>
 
{{out}}
10,327

edits