Problems? Is your data what you think it is? PerlMonks

### Re: Spiraling integers

by oakbox (Chaplain)
 on Nov 16, 2012 at 23:07 UTC ( #1004261=note: print w/replies, xml ) Need Help??

in reply to Spiraling integers

I was just playing with this for funsies after seeing a youtube video from Vi Hart about Ulam's Spiral. http://www.youtube.com/embed/Yhlv5Aeuo_k

So, I wrote the following spiral generator that writes the output to a SVG file. It takes about 10 seconds on my laptop to generate a graph of all primes between 1 and 10,000,000.

I see now that lot's of other people have taken a crack at this, but TMTOWTDI :)

```#!/usr/bin/perl

my \$x = 1;
my \$y = 1;

use Math::Prime::XS qw(is_prime);

my \$maxcount = 1000000;

open(WRT,">primegrid.svg");

print WRT q(<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="1"
height="1"
id="svg2"
version="1.1"
inkscape:version="0.48.3.1 r9886"
sodipodi:docname="New document 1">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="1"
inkscape:pageshadow="2"
inkscape:zoom="42.250643"
inkscape:cx="8.8460039"
inkscape:cy="2.865365"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1073"
inkscape:window-height="740"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="0" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>);

my \$gridholder;
my \$direction;

foreach my \$number ( 1 ... \$maxcount ){
print "\$number\r";
if(\$direction eq ""){
\$gridholder->{\$x}->{\$y} = \$number;
markprime(\$x,\$y,\$number);
\$direction = 'r';
next;
}
if(\$direction eq 'r'){
\$x++;
\$gridholder->{\$x}->{\$y} = \$number;
markprime(\$x,\$y,\$number);
my \$yp = \$y + 1;
if(\$gridholder->{\$x}->{\$yp} eq ""){
\$direction = 'd';
}
next;
}
if(\$direction eq 'd'){
\$y++;
\$gridholder->{\$x}->{\$y} = \$number;
markprime(\$x,\$y,\$number);
my \$xp = \$x - 1;
if(\$gridholder->{\$xp}->{\$y} eq ""){
\$direction = 'l';
}
next;
}
if(\$direction eq 'l'){
\$x--;
\$gridholder->{\$x}->{\$y} = \$number;
markprime(\$x,\$y,\$number);
my \$yp = \$y - 1;
if(\$gridholder->{\$x}->{\$yp} eq ""){
\$direction = 'u';
}
next;
}
if(\$direction eq 'u'){
\$y--;
\$gridholder->{\$x}->{\$y} = \$number;
markprime(\$x,\$y,\$number);
my \$xp = \$x + 1;
if(\$gridholder->{\$xp}->{\$y} eq ""){
\$direction = 'r';
}
next;
}

}

sub markprime {
my (\$x, \$y, \$number) = @_;

if(is_prime(\$number)){
print WRT qq(    <rect
style="fill:#000000;fill-opacity:1;stroke:none"
id="rect2985"
width="1"
height="1"
x="\$x"
y="\$y"
ry="0" />
);
}
return();
}

print WRT q(
</svg>);

print "done\n";
[download]```

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2019-10-23 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (57 votes). Check out past polls.

Notices?