From fe280737635486293c8fb99c05851019d4df2bb7 Mon Sep 17 00:00:00 2001 From: ievax Date: Mon, 1 Oct 2018 22:10:51 +0300 Subject: [PATCH] Added the fifth problem from project Euler --- Project Euler/Problem 5/problem5_sol1.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Project Euler/Problem 5/problem5_sol1.rb diff --git a/Project Euler/Problem 5/problem5_sol1.rb b/Project Euler/Problem 5/problem5_sol1.rb new file mode 100644 index 0000000..46997f0 --- /dev/null +++ b/Project Euler/Problem 5/problem5_sol1.rb @@ -0,0 +1,23 @@ +# 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? + + +# Euclid's algorithm for the greatest common divisor +def gcd(a, b) + b.zero? ? a : gcd(b, a % b) +end + +# Calculate the LCM using GCD +def lcm(a, b) + (a * b) / gcd(a, b) +end + +result = 1 + +20.times do |i| + result = lcm(result, i + 1) +end + +p result \ No newline at end of file