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?
Solution
# 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?
#I had to shorten this to fit on the blog
#correctly so
#r = result
#e = element
answer = [16,9,5,7,11,13,17,19].inject(1) {|r, e| r * e }
puts answer
Discussion
Well, that was easy. I really enjoy inject style methods for operating on arrays..
If you have questions, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).