Euler 005 ruby Solution

Smallest multiple

Problem

https://projecteuler.net/problem=5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Answer: 232792560

Solution

euler005.rb

#!/usr/bin/env ruby
def will_divide(test_me)
  20.downto(1) do |i|
    return false if test_me % i != 0
  end
  true
end

def euler005_solution
  answer = 20
  loop do
    break if will_divide(answer)
    answer += 20
  end
  answer
end

puts euler005_solution if __FILE__ == $PROGRAM_NAME

See Also

# cpp go java php ruby rust javascript
1
2
3  
4    
5    
6      
7          
8          
9          
10          
11          
12          
# cpp ruby
13
14
15
16
17
18
19
20  
21  
22  
23  
24