primes/primes.php

26 lines
372 B
PHP
Executable File

#!/usr/bin/env php
<?php
# PHP version of a Prime Number Finder
# haplo@mindstab.net
$MAX = $argv[1];
if($MAX < 1)
{
print "primes.php <MAX NUM>\n";
exit;
}
for($num=3; $num<= $MAX; $num+=2)
{
$max = sqrt($num);
$isPrime = true;
for($test=3;$test <= $max; $test+=2)
{
if(! ($num % $test)) {
$isPrime= false;
break;
}
}
if($isPrime) print "$num\n";
}
?>