Archive - May 7, 2007

Date

The most optimal PHP prime spiral generator yet

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);
  1. 1. This is the inverse of the Number Spiral algorithm from Steve Krenzel, where it find the number in a particular location.
  2. 2. Every minute of my day...

Notes for May 7

in

I found out the "That's what she said" phrase, it's totally awesome!
Check out the ones I made up:
Person #1: SAT is long and hard, I feel sore after doing it for 3 hours and 45 minutes.
Person #2: That's what she said!(I posted this one to wikipedia ). Btw what SAT do you mean?

Person #1: No one is online to play LF2 with me, seems like I have to play with myself.
Person #2: That's what she said!
Person #1: You wana watch or play too?
Person #2: ...Dude... this is getting REALLY weird.

Bots get the Honey Pot--bees will do the justice

in

Harvesters and Comment Spam bots? Time for them to get pwned!
Akismet, Bad Behavior are doing their part, but one more recruit won't hurt.
Project Honey Pot just planted a pot in my site and waiting for bots to fly in and get screwed ...screwed well
The project let user downloads a small honey pot script and upload on your website. Then, after setting up, put a link to the script in the page, but can't be seen by normal visitors so only the evil bots will click on it and wala...
This is what happens to harvesters
This is what happens to spam bots

Blog owners who does not have PHP or other server side script support, you can sign up for a link from the project site so you don't have to host it.

Honey Pot that kill bots