Hubert Jakubiak - 1 year ago 55

Ruby Question

As I'm a beginner Ruby developer. I wonder how more advanced Ruby developers would solve this issue. I'm looking here for the smallest number that is divided by numbers from 1 to 10 without any remainder.

*More formally this is defined as the Least Common Multiple of two or more integers, in this case the numbers 1 to 10.*

My current attempt:

`max_divider = 10`

nums = (1..max_divider).to_a

found_count = 0

x = 0

while found_count == 0

x += 1

nums.each do |num|

if x % num == 0

found_count += 1

end

end

if found_count != max_divider

found_count = 0

end

end

p x #=> 2520

Answer Source

```
MAX_DIVIDER = 10
DIVIDERS = (1..MAX_DIVIDER)
number = MAX_DIVIDER
number += 1 until DIVIDERS.all? { |divider| number % divider == 0 }
number #=> 2520
```

You can also use `lcm`

method Integer#lcm that will help you find the least common multiple.

```
DIVIDERS.reduce(:lcm) #=> 2520
```