TheAlgorithms-Ruby/discrete_mathematics/lcm.rb

24 lines
466 B
Ruby
Raw Normal View History

# LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers.
2021-02-07 08:05:54 +01:00
p 'Least Common Multiple'
2021-02-07 08:05:54 +01:00
p 'Enter first number'
value_one = gets.chomp.to_i
2021-02-07 08:05:54 +01:00
p 'Enter second number'
value_two = gets.chomp.to_i
def gcd(first, second)
if second != 0
2021-02-07 08:05:54 +01:00
gcd(second, first % second)
else
first
end
end
def lcm(first, second)
2021-02-07 08:05:54 +01:00
(first * second) / gcd(first, second)
end
2021-02-07 08:05:54 +01:00
p "Least Common Multiple is: #{lcm(value_one, value_two)}"