Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

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

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:
i am using ActivePerl-
windows xp

In reply to ulam's spiral too slow by orange

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-07-29 17:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found