Note: there is a new update make it 650 times faster
A HUGE success from Mgccl the great 
500*500 prime spiral can easily use more than 60 seconds with the previous code about spiral generation, but this one, only 12!
As a tradition, I have to introduce to you the "how" did I make it so fast so it produce the picture below:

1. Find all the primes with the PHP implementation of sieve of Eratosthenes
2. For each prime number, find where the a number locates1
3. Draw the point.
one and three are simple, 2 is HELL lot of work.
I first use some of my precious spear time2 on checking how to divide the spiral into pieces that I can operate on. Soon, I got it where all the perfect squares are going to locate, and the number's relationship to it.

So now I successfully divide the board into small half rings.
Then, I further break the half rings into two smaller pieces, one with the same x position, the other with the same y position. To find the difference, which is where the number lay related to the origin (0,0) point (where 1 is). I have to find each piece's point that lay on either x axis or y axis, so I can use simple add and subtraction to get the relative coordinates, which is the location we wanted
$size = 500; $x_adj = ~ $size & 1; $prime = esprime($size*$size+1); $image = imagecreate($size,$size); imagecolorallocate($image,255,255,255); $color = imagecolorallocate($image,0,0,0); $size2=$size/2; $n = 2;$t = true;$sn=4; $midu=2;$midl=4;$midd=6;$midr=8; while($n<=$size){ if($prime[0]<=$sn){ $p = array_shift($prime); if($t){//even if($p <= $sn-$n){ $x = $n/2; $y = $midu - $p; }else{ $x = $midl -$p; $y = -$n/2; } }else{//odd if($p <= $sn-$n){ $x = (-$n/2)+0.5; $y = $p - $midd; }else{ $x = $p - $midr; $y = ($n/2)-0.5; } } imagesetpixel($image,$x+$size2-$x_adj,$y+$size2,$color); }else{ ++$n; $sn = $n * $n; $t = !$t; if($t){ $midu = $sn-$n*1.5+1; $midl = $sn-$n*0.5+1; }else{ $midd = $sn-$n*1.5+1.5; $midr = $sn-$n*0.5+0.5; } } } header("Content-type: image/png"); imagepng($image);
Comments
Funny! I will try it!
Funny! I will try it! thanks.
alert('no');
alert('no');
Post new comment