Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 19th, 2010, 05: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

#2
January 19th, 2010, 06:17 PM
 L7Sqr
Contributing User

Join Date: Jan 2004
Location: Constant Limbo
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
I think you are a little mixed up by what you are asking and what you are calculating. Consider inserting tracer bullets into your program to see what it is outputting:
Code:
```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
puts "Found number %3 or %5"
print "old sum %d" % sum
sum = sum.to_i + 1
puts " old sumtotal %d" % sumtotal
sumtotal = sumtotal.to_i + sum
print "new sum %d" % sum
puts " new sumtotal %d" % sumtotal
end
sum = sum.to_i + 1
puts " ++sum   (%d)" % sum
end
sumtotal = sumtotal.to_i - 1000
puts sumtotal```

Gives me:
Code:
```...
Found number %3 or %5
old sum 996 old sumtotal 231638
new sum 997 new sumtotal 232635
++sum   (998)
++sum   (999)
Found number %3 or %5
old sum 999 old sumtotal 232635
new sum 1000 new sumtotal 233635
Found number %3 or %5
old sum 1000 old sumtotal 233635
new sum 1001 new sumtotal 234636
++sum   (1002)
233636```

See the value you are adding there at the end? So you can change the control to be an if instead of a while (to avoid an infinite loop when you remove the inner counter) and you can now get
Code:
```puts 'Following is the sum of all mutliples of 3 or 5 below 1000:'
sum = 0
sumtotal = 0
while sum <= 999
if sum % 3 == 0 || sum % 5 == 0
puts "Found number %3 or %5"
print "old sum %d" % sum
puts " old sumtotal %d" % sumtotal
sumtotal = sumtotal.to_i + sum
print "new sum %d" % sum
puts " new sumtotal %d" % sumtotal
end
sum = sum.to_i + 1
puts " ++sum   (%d)" % sum
end
sumtotal = sumtotal.to_i - 1000
puts sumtotal```
which yeilds
Code:
```...
++sum   (997)
++sum   (998)
++sum   (999)
Found number %3 or %5
old sum 999 old sumtotal 232169
new sum 999 new sumtotal 233168
++sum   (1000)
232168```

I'm not entirely sure why you started subtracting the 1000 at the end but it doesnt make sense (I can only guess it was due to your loop control issue).

One final remark: while what you provided is perfectly valid and produces a correct answer (once bugs are removed) you are likely to see code like this written in a more 'rubyish' way. There is a trend in the community to use iterators over loops and other similar constructs; do not get discouraged by this. It is ultimately only a matter of style in most cases but you will catch the occasional criticism for writing things the way you did.
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

#3
January 19th, 2010, 10:51 PM
Registered User

Join Date: Jan 2010
Posts: 3
Time spent in forums: 2 h 15 m 32 sec
Reputation Power: 0
Thanks for the reply, it's much appreciated!

Yeah, I had to subtract the 1000 because the way I was originally doing was producing radically different (and consequently radically incorrect results). Basically, I was including sum % 1000 != 0 to try to exclude the 1000, but of course it excluded more than just the 1000.

I appreciate your help in figuring out what I had wrong. As I said, I am new to Ruby -- just been teaching myself since Saturday since a matter of fact, so my limited three-day knowledge probably only succeeds in making life more difficult for me. I just thought it would be fun to try that as a fun little project.

Hopefully I'll be able to improve so that problems such as this pose no problem at all.

#4
January 20th, 2010, 08:26 AM
 L7Sqr
Contributing User

Join Date: Jan 2004
Location: Constant Limbo
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
If that is what you have after 3 days you have nothing to worry about. Congrats on figuring out how to do things yourself and taking the initiative to generate your own projects to do so.

#5
July 14th, 2010, 03:08 AM
 KDavid01
Registered User

Join Date: Jun 2010
Posts: 7
Time spent in forums: 1 h 30 m 47 sec
Reputation Power: 0
Re:Sum of Multiples of 3 and 5 below 1000

Useful codes.Thanks for sharing.

David.

#6
November 22nd, 2010, 11:04 AM
Registered User

Join Date: Nov 2010
Posts: 7
Time spent in forums: 1 h 24 m 16 sec
Reputation Power: 0
A Rubyish solution

(1..999).select { |n| n % 3 == 0 || n % 5 == 0 }.inject { |sum, n| sum + n }

Take all numbers less than 1000. Keep only the multiples of 3 or 5. Add them up using inject.

 Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Sum of Multiples of 3 and 5 below 1000