
Untitled
By: a guest on
May 7th, 2012 | syntax:
PHP | size: 0.53 KB | hits: 17 | expires: Never
<?php
echo "Find the sum of all the multiples of 3 or 5 below 1000. <br />";
$input = array(3, 5, 15); //15 is here so we can remove repeats (3 x 5 = 15)
$output = 0;
foreach($input as $key => $value){
$l = 2;
while($value < 1000) {
if($key == 2){ //Removes repeats from multiples of 5 and 3.
$output = $output - $value;
}else{
$output = $output + $value;
}
$value = $input[$key] * $l;
$l++;
}
}
echo "Answer: $output"
?>