Created
September 24, 2013 11:55
-
-
Save AlexVPopov/6683652 to your computer and use it in GitHub Desktop.
Project Euler 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def devisible?(num, limit) | |
devisible = true | |
limit.downto(1) do |i| | |
devisible = false if num % i != 0 | |
break if !devisible | |
end | |
devisible | |
end | |
def smallest_multiple(limit) | |
smallest_multiple = limit | |
while true | |
return smallest_multiple if devisible?(smallest_multiple, limit) && smallest_multiple != limit | |
smallest_multiple += limit | |
end | |
end | |
p smallest_multiple(20) | |
# much faster solution: | |
# require 'rational' | |
# num = (1..20).inject(1) { |result, n| result.lcm n } | |
# puts "Smallest evenly divisible number is #{ num }." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment