Zeckendorf arithmetic: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 73:
{{trans|Python}}
<
Int dLen
dVal = 0
Line 215:
g = Zeckendorf(‘101010’)
g = g + Zeckendorf(‘101’)
print(g)</
{{out}}
Line 237:
=={{header|C}}==
{{trans|D}}
<
#include <stdio.h>
#include <string.h>
Line 425:
return 0;
}</
{{out}}
<pre>Addition:
Line 444:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Text;
Line 632:
}
}
}</
{{out}}
<pre>Addition:
Line 651:
=={{header|C++}}==
{{works with|C++11}}
<
// I define an increment operation ++()
// I define a comparison operation <=(other N)
Line 723:
return os;
}
</syntaxhighlight>
===Testing===
The following tests addtition:
<
N G;
G = 10N;
Line 740:
std::cout << G << std::endl;
return 0;
}</
{{out}}
<pre>
Line 750:
</pre>
The following tests subtraction:
<
N G;
G = 1000N;
Line 759:
std::cout << G << std::endl;
return 0;
}</
{{out}}
<pre>
Line 766:
</pre>
The following tests multiplication:
<
int main(void) {
N G = 1001N;
Line 776:
std::cout << G << std::endl;
return 0;
}</
{{out}}
<pre>
Line 785:
=={{header|D}}==
{{trans|Kotlin}}
<
int inv(int a) {
Line 956:
g += "101".Z;
writeln(g);
}</
{{out}}
<pre>Addition:
Line 976:
{{trans|C++}}
ELENA 5.0 :
<
const dig = new string[]{"00","01","10"};
Line 1,224:
n += 101n;
console.printLine(n)
}</
{{out}}
<pre>
Line 1,243:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,426:
g.PlusAssign(NewZeck("101"))
fmt.Println(g)
}</
{{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).
<
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</
<pre>λ> 15 :: Fib
Line 1,673:
=={{header|J}}==
Loosely based on the [[#Perl|perl]] implementation:<
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 }}</
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:<
10
10 zadd&.zform 10
Line 1,708:
100101
100001000001 zdiv&.zform 100101
100010</
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<
public class Zeckendorf implements Comparable<Zeckendorf> {
Line 1,898:
System.out.println(g);
}
}</
{{out}}
<pre>Addition:
Line 1,917:
=={{header|Julia}}==
Influenced by the format of the Tcl and Raku versions, but added other functionality.
<
const z0 = "0"
Line 2,089:
zeckendorftest()
</
Addition:
101
Line 2,112:
=={{header|Kotlin}}==
{{trans|C++}}
<
class Zeckendorf(x: String = "0") : Comparable<Zeckendorf> {
Line 2,267:
g += "101".Z
println(g)
}</
{{out}}
Line 2,289:
=={{header|Nim}}==
{{trans|Go}}
<
dVal: Natural
dLen: Natural
Line 2,430:
g = initZeckendorf("101010")
g += initZeckendorf("101")
echo g</
{{out}}
Line 2,449:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Zeckendorf_arithmetic
Line 2,569:
++$z for 1 .. $value;
return $z;
}</
{{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).
<!--<
<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>
<!--</
{{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)</
=={{header|Python}}==
<
class Zeckendorf:
Line 3,219:
g = Zeckendorf("101010")
g = g + Zeckendorf("101")
print g</
{{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).
<
(define sqrt5 (sqrt 5))
Line 3,434:
(example '/ zeck-quotient 9876 1000)
(example '% zeck-remainder 9876 1000)
</syntaxhighlight>
{{output}}
Line 3,464:
less than '''ltz'''
<syntaxhighlight lang="raku"
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';</
'''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">
import scala.collection.mutable.ListBuffer
Line 3,950:
}
</syntaxhighlight>
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 -->
<
# Want to use alternate symbols? Change these
variable zero "0"
Line 4,065:
namespace export \[a-y\]*
namespace ensemble create
}</
Demonstrating:
<
puts [zeckendorf sub "10100" "1010"]
puts [zeckendorf mul "10100" "1010"]
puts [zeckendorf div "10100" "1010"]
puts [zeckendorf div [zeckendorf mul "10100" "1010"] "1010"]</
{{out}}
<pre>
Line 4,083:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 4,280:
End Sub
End Module</
{{out}}
<pre>Addition:
Line 4,299:
=={{header|Vlang}}==
{{trans|Go}}
<
const (
dig = ["00", "01", "10"]
Line 4,482:
g.plus_assign(new_zeck("101"))
println(g)
}</
{{out}}
Line 4,505:
{{trans|Kotlin}}
{{libheader|Wren-trait}}
<
class Zeckendorf is Comparable {
Line 4,659:
g = Z.new("101010")
g.plusAssign(Z.new("101"))
System.print(g)</
{{out}}
|