# Project Euler: Problem #1

Although I’ve been learning Ruby and Rails for the past several months, I still feel like I need a better understanding of programming with respect to the more algorithmic side of things. While searching for resources last night, I came across Project Euler. Project Euler is a tool used to sharpen math skills through writing computer programs. In an effort to become really good at programming in Ruby, I’m going to try to solve a problem a week, and write about my thinking process behind the solution.

The following is the description of 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.

After looking through the Ruby documentation for methods to help me, I came up with this solution: ``` ruby problem1.rb def multiples sum = (1..999).select {|num| num % 3 == 0 || num % 5 == 0}.reduce(:+) puts sum end

multiples() ```

First, we have to look at the range. Since the problem states that we have to find the sum of all the multiples BELOW 1000, the range has to be from `1..999`

. At first, I used `1..1000`

and couldn’t understand why I kept getting a wrong answer when I submitted it. It helps to read the problem carefully.

The key to this solution is the `select`

method. This method picks `num`

out of the range with the criteria of being multiples of 3 or of 5.

Once that those numbers are selected from the range, the `reduce`

method adds them all up. When using the reduce method, all you have to do is specify the operation with a symbol, for example, `:+`

is used for the addition of all the selected values.

Click here to read more about the `select`

method and here to read more about the `reduce`

method.

### Comparison

After I solved the problem, I looked at other peoples solutions on Project Euler to see how they went about it. I decided to compare the speed of my solution to that of another with Benchmark. Here’s the other solution: ``` ruby Other Solution class MultipleSum def self.add_sums ( number ) @belowNumber = number add_multiples end

```
def self.add_multiples
sum = 0
find_multiples.each { |t| sum += t }
return sum
end
def self.find_multiples
multiples = Array.new
(1...@belowNumber).each do |value|
multiples << value if value % 3 == 0 || value % 5 == 0
end
return multiples
end end
```

puts MultipleSum.add_sums ( 1000 )
```
The other solution is a lot longer and in my opinion, less straightforward, so I expected mine to be faster. To my surprise, that wasn't the case:
```

plain
#My solution
0.000000 0.000000 0.000000 ( 0.000205)

#Other solution 0.000000 0.000000 0.000000 ( 0.000190) ``` Although it is slightly faster, the speed difference is negligible. I think that the shorter method is better simply for the sake of concise and clear code. If another developer needs a method like this(the chances are unlikely), a shorter method is more portable and easier to add into an existing project.