Euler Project Problem 1

Hi All,

I’m working on solving the first problem on ProjectEuler.net and I just
can’t seem to figure out why the following code doesn’t output the
correct answer. I’ve found other solutions, but I want to make mine
work rather than copy someone else’s. Any ideas?

Here’s the problem: 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.

And my (wrong) solution:

c = 0
d = 0

c.step(999, 3) { |number| c += number }

d.step(999, 5) { |number| d += number }

puts (c + d)

Thanks,
Ryan

There are lot of numbers that are divisible by both 5 and 3, and in your
sulution you add each of them twice (first as divisible by 3, second as
divisible by 5).

That was it! Can’t believe I missed it, but thank you for pointing it
out.

I added an ugly line stepping through in increments of 15 and subtracted
the result.

Most solutions I’ve seen have been looking for numbers divisible by 3 or
5 with no remainder, but I wanted to try something different.

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs