Find The Prime Number Using PHP

function prime($n){

//all numbers that will be evaluated as prime
for($i=1;$i<=$n;$i++){
$counter = 0;
//all probability of divisible factors
for($j=1;$j<=$i;$j++){
//$i is divisible by $j, increament the counter
if($i % $j==0){
$counter++;
}
}
/*how many greens? they have to be 2
( each prime number has exactly 2 (two) divisors)*/
if($counter==2){
print $i.” is Prime
“;
}
}
}

prime(100);  //find the Prime for 1-20 range

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s