http://www.perlmonks.org?node_id=610218

orange has asked for the wisdom of the Perl Monks concerning the following question:

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 = <>;
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_ == \$num)
{
\$b = 1;
}

}
}