<? // prime number finder v1.0 // returns an array of prime numbers up to the specified amount // for example, if 10 is specified this would return 2, 3, 5, 7 function primes($max) { // none below 2 if ($max < 2) $primes = array(); // just want 2 else if ($max == 2) $primes = array(2); else { // add the first two so a prime sieve can be used $primes = array(2, 3); if ($max >= 5) { for ($a = 5; $a < $max; $a+=2) { $is_prime = true; foreach($primes as $prime) { if ($prime > sqrt($a)) break; if (!($a % $prime)) { $is_prime = false; break; } } if ($is_prime) $primes[] = $a; if ($switch) $a+=2; $switch = !$switch; } } } // return the array of prime numbers return $primes; } echo implode(', ', primes(100));?>