Skip to content

Instantly share code, notes, and snippets.

@AlexVPopov
Created September 24, 2013 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexVPopov/6683652 to your computer and use it in GitHub Desktop.
Save AlexVPopov/6683652 to your computer and use it in GitHub Desktop.
Project Euler 5
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