Babylonian spiral: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 43: Line 43:
Implementation:
Implementation:


<syntaxhighlight lang=J>
<lang J>
require'stats'
require'stats'
bspir=: {{
bspir=: {{
Line 57: Line 57:
end.
end.
}}
}}
</syntaxhighlight>
</lang>


Task example:
Task example:


<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
21j17 26j13 29j7 29 28j_7 24j_13 17j_15 10j_12 4j_7 4j1
21j17 26j13 29j7 29 28j_7 24j_13 17j_15 10j_12 4j_7 4j1
5j9 7j17 13j23 21j26 28j21 32j13 32j4 31j_5 29j_14 24j_22
5j9 7j17 13j23 21j26 28j21 32j13 32j4 31j_5 29j_14 24j_22
</syntaxhighlight>
</lang>


Also:
Also:


<syntaxhighlight lang=J>
<lang J>
require'plot'
require'plot'
plot bspir 40
plot bspir 40
plot bspir 1e4
plot bspir 1e4
</syntaxhighlight>
</lang>


There's an online example of these two cases [https://jsoftware.github.io/j-playground/bin/html2/#code=require'plot%20stats'%0Abspir%3D%3A%20%7B%7B%0A%20%20r%3D.%200%0A%20%20e%3D.%202%3E.%3C.%2B%3A%25%3Ay%0A%20%20for_qd.%0A%20%20%20%20%28y-1%29%7B.%28%3C%2F.~%20*%3A%40%7C%29%20%28%2F%3A%7C%29%20%28%23~%20%28%3D%3C.%29%40%3A*%3A%40%3A%7C%29%20j.%2F%221%20%282%20comb%20e%29%2C%2C.~1%2Bi.e%0A%20%20do.%0A%20%20%20%20d%3D.%20~.%28%2C%2B%29%28%2C-%29%28%2Cj.%29%3Bqd%0A%20%20%20%20ar%3D.%2012%20o.%20-~%2F_2%7B.r%0A%20%20%20%20ad%3D.%20%28-%202p1%20*%20%3E%3A%26ar%29%2012%20o.%20d%0A%20%20%20%20-r%3D.%20r%2C%20%28%7B%3Ar%29%2Bd%7B~%20%28i.%20%3E.%2F%29%20ad%0A%20%20end.%0A%7D%7D%0A%0Aplot%20bspir%2040%0Aplot%20bspir%201e4 here] (follow the link, select the right pane, then hit control-R or select Edit Run &gt; All Lines from the menu, this might take a couple seconds, depending on your machine, and this url may need to be updated in the future).
There's an online example of these two cases [https://jsoftware.github.io/j-playground/bin/html2/#code=require'plot%20stats'%0Abspir%3D%3A%20%7B%7B%0A%20%20r%3D.%200%0A%20%20e%3D.%202%3E.%3C.%2B%3A%25%3Ay%0A%20%20for_qd.%0A%20%20%20%20%28y-1%29%7B.%28%3C%2F.~%20*%3A%40%7C%29%20%28%2F%3A%7C%29%20%28%23~%20%28%3D%3C.%29%40%3A*%3A%40%3A%7C%29%20j.%2F%221%20%282%20comb%20e%29%2C%2C.~1%2Bi.e%0A%20%20do.%0A%20%20%20%20d%3D.%20~.%28%2C%2B%29%28%2C-%29%28%2Cj.%29%3Bqd%0A%20%20%20%20ar%3D.%2012%20o.%20-~%2F_2%7B.r%0A%20%20%20%20ad%3D.%20%28-%202p1%20*%20%3E%3A%26ar%29%2012%20o.%20d%0A%20%20%20%20-r%3D.%20r%2C%20%28%7B%3Ar%29%2Bd%7B~%20%28i.%20%3E.%2F%29%20ad%0A%20%20end.%0A%7D%7D%0A%0Aplot%20bspir%2040%0Aplot%20bspir%201e4 here] (follow the link, select the right pane, then hit control-R or select Edit Run &gt; All Lines from the menu, this might take a couple seconds, depending on your machine, and this url may need to be updated in the future).
Line 82: Line 82:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
<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 139: Line 139:


lines(babylonianspiral(10_000), linewidth = 1)
lines(babylonianspiral(10_000), linewidth = 1)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The first 40 Babylonian spiral points are:
The first 40 Babylonian spiral points are:
Line 150: Line 150:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use feature <say state>;
use feature <say state>;
Line 196: Line 196:
my @points = map { sprintf "(%3d,%4d)", @$_ } B_spiral(40);
my @points = map { sprintf "(%3d,%4d)", @$_ } B_spiral(40);
say "The first 40 Babylonian spiral points are:\n" .
say "The first 40 Babylonian spiral points are:\n" .
join(' ', @points) =~ s/.{1,88}\K/\n/gr;</lang>
join(' ', @points) =~ s/.{1,88}\K/\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 40 Babylonian spiral points are:
<pre>The first 40 Babylonian spiral points are:
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.
<!--<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 353: Line 353:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 364: Line 364:


=={{header|Python}}==
=={{header|Python}}==
<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 421: Line 421:
axis('scaled')
axis('scaled')
show()
show()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The first 40 Babylonian spiral points are:
The first 40 Babylonian spiral points are:
Line 431: Line 431:
=== 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.
<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 475: Line 475:
plt.plot(*zip(*list(islice(positions(), 100000))), lw=0.4)
plt.plot(*zip(*list(islice(positions(), 100000))), lw=0.4)
plt.gca().set_aspect(1)
plt.gca().set_aspect(1)
plt.show()</lang>
plt.show()</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 481: Line 481:


===Translation===
===Translation===
<lang perl6>sub babylonianSpiral (\nsteps) {
<syntaxhighlight lang=raku lines>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 525: Line 525:
],
],
],
],
);</lang>
);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 540: Line 540:
Exact same output; about one tenth the execution time.
Exact same output; about one tenth the execution time.


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


sub next-interval (Int $int) {
sub next-interval (Int $int) {
Line 580: Line 580:
],
],
],
],
);</lang>
);</syntaxhighlight>
{{out|Same output}}
{{out|Same output}}


Line 591: Line 591:
{{libheader|Wren-plot}}
{{libheader|Wren-plot}}
Generates an image similar to the OEIS one.
Generates an image similar to the OEIS one.
<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
Line 685: Line 685:
}
}


var Game = Main.new()</lang>
var Game = Main.new()</syntaxhighlight>


{{out}}
{{out}}