Babylonian spiral: Difference between revisions

Content added Content deleted
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 42: Line 42:
Implementation:
Implementation:


<syntaxhighlight lang=J>
<syntaxhighlight lang="j">
require'stats'
require'stats'
bspir=: {{
bspir=: {{
Line 60: Line 60:
Task example:
Task example:


<syntaxhighlight lang=J> 4 10$bspir 40
<syntaxhighlight lang="j"> 4 10$bspir 40
0 0j1 1j2 3j2 5j1 7j_1 7j_4 6j_7 4j_10 0j_10
0 0j1 1j2 3j2 5j1 7j_1 7j_4 6j_7 4j_10 0j_10
_4j_9 _7j_6 _9j_2 _9j3 _8j8 _6j13 _2j17 3j20 9j20 15j19
_4j_9 _7j_6 _9j_2 _9j3 _8j8 _6j13 _2j17 3j20 9j20 15j19
Line 69: Line 69:
Also:
Also:


<syntaxhighlight lang=J>
<syntaxhighlight lang="j">
require'plot'
require'plot'
plot bspir 40
plot bspir 40
Line 81: Line 81:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
<syntaxhighlight lang=ruby>""" Rosetta Code task rosettacode.org/wiki/Babylonian_spiral """
<syntaxhighlight lang="ruby">""" Rosetta Code task rosettacode.org/wiki/Babylonian_spiral """


using GLMakie
using GLMakie
Line 150: Line 150:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature <say state>;
use feature <say state>;
Line 208: Line 208:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Babylonian_spiral.htm here]. Use left/right arrow keys to show less/more edges.
You can run this online [http://phix.x10.mx/p2js/Babylonian_spiral.htm here]. Use left/right arrow keys to show less/more edges.
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo/rosetta/Babylonian_spiral.exw
-- demo/rosetta/Babylonian_spiral.exw
Line 364: Line 364:


=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang=python>""" Rosetta Code task rosettacode.org/wiki/Babylonian_spiral """
<syntaxhighlight lang="python">""" Rosetta Code task rosettacode.org/wiki/Babylonian_spiral """


from itertools import accumulate
from itertools import accumulate
Line 434: Line 434:
=== With priority queue ===
=== With priority queue ===
Use a priority queue to generate all x, y combinations. The advantage is that we don't need to do any real math, and it is much faster.
Use a priority queue to generate all x, y combinations. The advantage is that we don't need to do any real math, and it is much faster.
<syntaxhighlight lang=python>from itertools import islice, count
<syntaxhighlight lang="python">from itertools import islice, count
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import heapq
import heapq
Line 484: Line 484:


===Translation===
===Translation===
<syntaxhighlight lang=raku lines>sub babylonianSpiral (\nsteps) {
<syntaxhighlight lang="raku" line>sub babylonianSpiral (\nsteps) {
my @squareCache = (0..nsteps).hyper.map: *²;
my @squareCache = (0..nsteps).hyper.map: *²;
my @dxys = [[0, 0], [0, 1]];
my @dxys = [[0, 0], [0, 1]];
Line 543: Line 543:
Exact same output; about one tenth the execution time.
Exact same output; about one tenth the execution time.


<syntaxhighlight lang=raku lines>my @next = { :x(1), :y(1), :2hyp },;
<syntaxhighlight lang="raku" line>my @next = { :x(1), :y(1), :2hyp },;


sub next-interval (Int $int) {
sub next-interval (Int $int) {
Line 594: Line 594:
{{libheader|Wren-plot}}
{{libheader|Wren-plot}}
Generates an image similar to the OEIS one.
Generates an image similar to the OEIS one.
<syntaxhighlight lang=ecmascript>import "dome" for Window
<syntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color
import "graphics" for Canvas, Color
import "./trait" for Indexed, Stepped
import "./trait" for Indexed, Stepped