Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: PHP  |  size: 0.53 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. echo "Find the sum of all the multiples of 3 or 5 below 1000. <br />";
  3.  
  4. $input = array(3, 5, 15); //15 is here so we can remove repeats (3 x 5 = 15)
  5. $output = 0;
  6.  
  7. foreach($input as $key => $value){
  8.     $l = 2;
  9. while($value < 1000) {
  10.    
  11.     if($key == 2){ //Removes repeats from multiples of 5 and 3.
  12.        
  13.         $output = $output - $value;
  14.  
  15.     }else{
  16.        
  17.         $output = $output + $value;
  18.         }
  19.    
  20.     $value = $input[$key] * $l;
  21.     $l++;
  22.        
  23.     }
  24. }
  25.  
  26. echo "Answer: $output"
  27.  
  28. ?>