## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

###### jordanresendiz

Courses Plus Student 2,356 Points# 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

###### Steve Hunter

57,696 PointsI 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.

###### Andrew D.

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

Hope that helps!

###### jordanresendiz

Courses Plus Student 2,356 PointsThank 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.

2,937 PointsDang, 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.

###### Steve Hunter

57,696 PointsDouble 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.

###### jordanresendiz

Courses Plus Student 2,356 PointsOHHHH! Gee! I get it now thank you so much Steve!

###### Steve Hunter

57,696 PointsHope 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.

## jordanresendiz

Courses Plus Student 2,356 Points## jordanresendiz

Courses Plus Student 2,356 PointsI'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.! :(

## Steve Hunter

57,696 Points## Steve Hunter

57,696 PointsSorry 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:

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:

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.