Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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";

In reply to Re: Spiraling integers by oakbox
in thread Spiraling integers by Elijah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2019-10-15 07:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?