Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 19th, 2010, 04:36 PM
SlinkyABC SlinkyABC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 3 SlinkyABC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 19th, 2010, 05:17 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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

Reply With Quote
  #3  
Old January 19th, 2010, 09:51 PM
SlinkyABC SlinkyABC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 3 SlinkyABC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 20th, 2010, 07:26 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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.

Reply With Quote
  #5  
Old July 14th, 2010, 02:08 AM
KDavid01 KDavid01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 7 KDavid01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old November 22nd, 2010, 10:04 AM
robert_head robert_head is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 7 robert_head User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Sum of Multiples of 3 and 5 below 1000

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap