While we wait until Goldbug is done with his calendar judging, I thought I'd throw in a quick contest for anyone interested.
Contest Name: A Prime Example
Contest ends: 6/11/2004, midnight
Task: Create a prime number checker. Write a function that determines whether a number is prime or not. A prime number is any number that is only divisible by 1 and itself (such as 7, or 4447).
Goal: SPEED! I will test your function 5 times on my server, and choose the highest number of primes it finds.
Restrictions:
All coding must be in PHP. You must use the shell php page I am supplying, and only write the check_prime() function.
Your code must correctly identify any number passed to it as a prime number.
Go here to view your code in action, as well as everyone else's.
Here is the php code I will be using, for your testing purposes. Note that I am passing only odd numbers to your function. You may not take advantage of that, but must make sure that any number passed to your function validates a prime number correctly. The PHP code I use to test may change, but it will be the same code for every entry tested.
<?php
if(isset($_GET['time'])) {
$time = $_GET['time'];
} else {
$time=3;
}
set_time_limit(0);
function GetTime() {
list($msec, $sec) = explode(" ", microtime());
return ((float)$msec + (float)$sec);
}
function check_prime ($num) {
// Your function goes here. It must return TRUE if the number is
// prime, and FALSE if it is not.
}
set_time_limit(0);
$x = 0;
$timediff=0;
$time_start = GetTime();
while ($timediff < $time) {
if (check_prime($x,$primes)) {
$primes[] = $x;
}
$x++;
$time_end = GetTime();
$timediff = $time_end - $time_start;
}
Echo "Found " . count($primes) . " prime numbers in $time seconds:<br><br>";
foreach ($primes as $value) {
echo $value . " ";
}
?>
Please include only your function as a reply to this message, not the above php code, and make sure you enclose it in [php] and [/php] tags.
Good Luck, and have fun!