...

php find a prime

person5
02-24-2010, 01:50 AM
<?php
$number = (a number you want to check);
$i=2;
While($i < $number / 2 ){
If( $number % $i = 0 ){
echo "nope";
}
else
{
$i++;
}
}
?>
might not work i didn't double check it but just don't change the modulus sign %

Cjwinnit
10-07-2011, 04:10 PM
Hope I don't get into too much trouble but I spotted a very quick way to speed up this code..

You only need to check up to the square root of the number in question, not half the value. Example, if you were checking a number that is roughly a million, that requires your script to do up to 500,000 increments. In reality it only needs to do about 1,000.

Does PHP have a square root function?

Inigoesdr
10-07-2011, 07:54 PM
Does PHP have a square root function?
Yeah, sqrt() (http://php.net/sqrt).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum