Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Project Euler: Help, Why wont this work?

Hey guys, I just recently started to learn Ruby. And as a way I started doing the Project Euler challenges. However, when I thought I had solved the first problem it tells me it's the wrong answer. Can someone please explain what's wrong with my code. Also here is a link to the problem: https://projecteuler.net/problem=1

and here is my code:

sum = 0
0.step(1000, 3) {|n| sum += n}
0.step(1000, 5) {|s| sum += s}
prints sum

Thanks!

3 Answers

I think looping up by each multiple and adding it all together is double counting. For example, 15 will be included in both additions. Same with 30, etc.

Try looking at the scenarios where the modulus of 3 & (logical AND) 5 are zero first; add those. That covers off the duplicates so then do the mod of 3, then of 5. Add those to the mix.

I don't know Ruby well enough to do the code for you but here's some mega-clunky Swift that illustrates the point I'm trying to make:

func fizzbuzz(#number: Int){
if number % 3 == 0 && number % 5 == 0 {
    // add 'number' to a variable
} else if number % 5 == 0 {
    // add 'number' to a variable
} else if number % 3 == 0 {
    // add 'number' to a variable
} else {
    // do nothing
}
}

for var i = 1; i < 1000; i++ {
    fizzbuzz(number: i)
}

Loop from 1 to 1000 and perform the three tests ONLY if the previous test failed. If the test passes, accumulate the current inde of the loop in a variable.

I hope that helps!

Steve.

I'm sorry, but I feel more confused now. What is fizzbuzz. and how come you have 3 different if statements, and three else statements. waht do they do? Are you turning fizzbuzz into a method? I'm sorry.! :(

Sorry to confuse you. But I think you went straight to the code rather than reading my points about duplicate counting.

Fizzbuzz is a game - it is also a coding challenge on the Swift track in Treehouse.

There are three different if statements to cover the three scenarios that you need to identify.

One way of getting your challenge completed is to use a loop to iterate through all numbers between 1 and 1000. At each pass, test the three scenarios that you need:

  • The current number is divisible by 3 AND 5
  • The current number is divisible by 5
  • The current number is divisible by 3

If you apply those tests in order (to avoid double counting) and add up each number that passes, you will get to your answer.

Ignore the function name and, indeed, the function - just put it all inside a loop:

  • loop an index number from 1 to 1000 and for each pass:
  • if index is divisible by 3 AND 5 - add index to the stack
  • if index is divisible by ONLY 3 not 5 - add index to the stack
  • if index is divisible by ONLY 5 not 3 - add index to the stack
  • loop the next number

That is how to approach your code - I don't know Ruby, as I said, but you clearly do, so you need to make a loop to go through the range 1 to 1000 and to test each number, as above, and avoid double counting. 15 and 30 are divisible by 5 and 3. So your code counts them in each pass. My suggested solution pulls out scenarios where the number is divisible by both 3 AND 5 first, then tests only for the 3 and 5 division separately so there can be no double counting.

I hope that's a little clearer!

Steve.

Andrew D.
Andrew D.
2,937 Points

I don't know a darn thing about Ruby, but I imagine you meant to put 'print sum' rather than 'prints sum'.

Hope that helps!

Thank you. But I have no problem printing out the sum. the problem is that when it does, it gives me the answer 267333. and according to Project Euler it's wrong.

Andrew D.
Andrew D.
2,937 Points

Dang, sorry! Project Euler is great though, I plan on using it for Java when I get the syntax down. I believe the solutions are posted throughout the Internet, if you get truly stuck and want to peek at what you did wrong.

Double counting some numbers as they can be included in both sets of data, I think. Answered below - and I've suggested a change of approach.

OHHHH! Gee! I get it now thank you so much Steve!

Hope you get it fixed.

I'm on a one-day course looking at Ruby on Rails tomorrow but I doubt it'll go into this much - it'll be more about web stuff. Link is here.

Still - it is always worth looking around at what your local Tech guys are up to - there's often something useful to go to!

Steve.