Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.52 KB | None | 0 0
  1. # Calculate the sum of all multiples of 3, and all multiples of 5, but not multiples of both, up to 1000.
  2.  
  3. lim = 1000
  4.  
  5. fstMult = 0
  6. fstInc = 3
  7.  
  8. sndMult = 0
  9. sndInc = 5
  10.  
  11. bothMult = 0
  12. bothInc = 15
  13.  
  14. sum = 0
  15.  
  16. while ( fstMult < (lim - fstInc) )
  17.     fstMult = fstMult + fstInc
  18.     sum = sum + fstMult
  19. end
  20.  
  21. while ( sndMult < (lim - sndInc) )
  22.     sndMult = sndMult + sndInc
  23.     sum = sum + sndMult
  24. end
  25.  
  26. while ( bothMult < (lim - bothInc) )
  27.     bothMult = bothMult + bothInc
  28.     sum = sum - bothMult
  29. end
  30.  
  31. puts sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement