Project Euler with Ruby on WSL [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?
私訳
1から20までのすべての数で割り切れる、最小の正整数はなにか
つまり、最小公倍数は?
解答方針
で なので、ごく単純に lcm を取っていく
解答
# Project Euler # problem 5 def gcd(a, b) # greatest common divisor if a > b return gcd(b, a) end r = b % a if r == 0 return a end gcd(r, a) end def lcm(a, b) # least common multiplier a * b / gcd(a,b) end def lcm_list(list) list.reduce(1){|result, item| lcm(result, item)} end puts lcm_list(1..20)