- Messages
- 5,508
- Reaction score
- 35
- Points
- 48
PHP:
<?php
//1. How many more loops does the program do to find all primes to 200?
$maxArr = 100;
$testRuns = 0;
$noPrimes = 0;
$jrun = 0;
for($i = 0; $i < $maxArr;$i++) {
$primArr[$i] = $i + 1;
}
$primArr[0] = -1;
for($i = 1; $i < $maxArr;$i++) {
$prim = $primArr[$i];
if($prim != -1) {
for($j = ($i + 1); $j < $maxArr; $j++){ //3c. How many numbers does this loop go trought. EXPLAIN WHY
if(($primArr[$j] % $prim) == 0){ //3d. what does this if do?
$primArr[$j] =-1;
}
$testRuns = $testRuns +1;
}
}
}
for($i = 0; $i < $maxArr; $i++){
if($primArr[$i] != -1) {
echo "$primArr[$i]<br />";
$noPrimes = $noPrimes+1;
}
}
echo "Antal loopar: $testRuns<br />";
echo "Antal primtal funna: $noPrimes<br />";
?>
Explain plox. I don't get it...