如何在php中打印前100个素数并在此代码中解释我的错误
<?php
for($i=1;$i<=100;$i++){
for($j = 1; $j<=$i; $j++){
$temp = $i%$j;
if($temp == 1){
echo "is a prime number ".$j."<br>";
}
}
}
?>
答案 0 :(得分:2)
对于素数这里的基本答案
function primeno($n){
for($i=1;$i<=$n;$i++){ //numbers to be checked as prime
$counter = 0;
for($j=1;$j<=$i;$j++){ //all divisible factors
if($i % $j==0){
$counter++;
}
}
//prime requires 2 rules ( divisible by 1 and divisible by itself)
if($counter==2){
print $i." is Prime <br/>";
}
}
}
primeno(100); //find prime numbers from 1-100