mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-15 03:43:22 +01:00
Added the fifth problem from project Euler
This commit is contained in:
parent
7175100e8c
commit
fe28073763
1 changed files with 23 additions and 0 deletions
23
Project Euler/Problem 5/problem5_sol1.rb
Normal file
23
Project Euler/Problem 5/problem5_sol1.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue