Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
hi
ulam spiral is the prime numbers spiral, you can find info how to draw it in:
Ulam_spiral

each white dot represent a prime number. in the result we will see that the primes have some limited patterns. the following code draw the spiral (a square spiral),

#!/usr/local/bin/perl use strict; use Tk; my ($order,$pntcycle,$pnts,$b,$num,$p) = (0); my $lastnumber; my $NumOfPrimes=0; my ($i, $colr, $counter); my($o, $s) = (250, 20); my($pi, $x, $y) = (3.1415926, 0, 0); print "type the maximum number for the prime spiral \n"; my $lastnumber = <>; print "please wait \n"; my $mw = MainWindow->new; my $c = $mw->Canvas(-width => 500, -height => 500); $c->pack; $c->createLine(50, 250, 450, 250); $c->createText(10, 250, -fill => 'blue', -text => 'X'); $c->createLine(250, 50, 250, 450); $c->createText(250, 10, -fill => 'blue', -text => 'Y'); OUTER: for ($i= 1; $i <= 250; $i += 1) { $order=$order+1; if ($order == 5){$order = 1;} $pntcycle = $pntcycle + 1; if ($pntcycle == 3) {$pntcycle = 1; $pnts = $pnts + 1;} $counter=0; while ($counter <= $pnts ) { $counter++; $num = $num + 1; if ($num == $lastnumber+1) {last OUTER;} &is_prime(); if ($b != 0) {$colr = "white"; $NumOfPrimes = $NumOfPrimes + 1;} if ($b == 0) {$colr = "black";} if ($order == 1) {$x = $x + 1;} elsif ($order == 2) {$y = $y - 1;} elsif ($order == 3) {$x = $x - 1; } elsif ($order == 4) {$y = $y + 1; } $c->createText( $x+$o, $y+$o, -fill => "$colr", -text => '.'); } } MainLoop; sub is_prime { $p=0; $b=0; my $j=0; my @prime_; for($j=1; $j <= $num; $j++) { if($num % $j==0) { $prime_[$p] = "$j"; $p++; } if ($prime_[1] == $num) { $b = 1; } } }
but i have two problems, it is very slow, in my celeron 2 Ghz i draw the points up to 5000 in about 20 seconds. the algorithm for the checking the primality of numbers i have copied it from somewhere in this forum with a small variation.
the other problem is that the final plot appear after the calculation have finished, i can't see the process of drawing dot by dot.

for comparison here is a visual basic 6 code and an executable file to demonstrate the defference:
http://mihd.net/z58lv6
i am using ActivePerl-5.8.8.817-MSWin32-x86
windows xp


In reply to ulam's spiral too slow by orange

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found