perlquestion
orange
hi <br/>
ulam spiral is the prime numbers spiral, you can find info how to draw it in:
<br/>
[wp://Ulam_spiral]
<br/>
<p>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),
<code>
#!/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;
}
}
}
</code>
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. <br/>
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. <br/>
<p>for comparison here is a visual basic 6 code and an executable file to
demonstrate the defference: <br/>
http://mihd.net/z58lv6 <br/>
i am using ActivePerl-5.8.8.817-MSWin32-x86 <br/>
windows xp