
January 19th, 2010, 04:36 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 3
Time spent in forums: 2 h 15 m 32 sec
Reputation Power: 0
|
|
|
Sum of Multiples of 3 and 5 below 1000
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
I have been trying to solve this for about two hours now...I'm relatively new to this sort of thing, so I figured solving problems such as this would be a good way to improve.
The answer my program comes up with is 233636, but the correct answer is 233168. I don't really understand why my program isn't producing the correct answer. I know that it could be a lot more clean and a lot shorter, but I still feel like this should work. I would be greatly appreciate it if one of you could tell me where the flaw in my code is. Thanks!
Here is the code I've been using:
puts 'Following is the sum of all mutliples of 3 or 5 below 1000:'
sum = 0
sumtotal = 0
while
sum <= 999
while sum % 3 == 0 || sum % 5 == 0
sum = sum.to_i + 1
sumtotal = sumtotal.to_i + sum
end
sum = sum.to_i + 1
end
sumtotal = sumtotal.to_i - 1000
puts sumtotal
|