Find the greatest common divisor and least common multiple of any number of integers in Ruby

To get the greatest common divisor and least common multiple of the integers ʻaandb` in Ruby, Integer # gcd, respectively. /i/gcd.html), Integer # lcm

#Greatest common divisor (GCD: Greatest Common Divisor)
a.gcd(b)

#Least common multiple (LCM: Least Common Multiplier)
a.lcm(b)

To do.

For example, the greatest common divisor and least common multiple of 4 and 6 are

puts 4.gcd(6) # => 2
puts 4.lcm(6) # => 12

And so on.

Then, what are the greatest common divisors and least common multiples of the three integers ʻa, b, and c`? Since the greatest common divisor of $ a $, $ b $, and $ c $ is "the greatest common divisor of $ a $ and $ b $" and the greatest common divisor of $ c $.

a.gcd(b).gcd(c)

Obtained at. The same applies to the least common multiple,

a.lcm(b).lcm(c)

Obtained at.

So what if a set of integers was given as an array? You can write as follows.

numbers = [30, 20, 15]

#Greatest common divisor
puts numbers.inject(:gcd) # => 5

#Least common multiple
puts numbers.inject(:lcm) # => 60

Enumerable # inject had a usage to give a symbol without giving a block.

Recommended Posts

Find the greatest common divisor and least common multiple of any number of integers in Ruby
Find the greatest common divisor and least common multiple with JAVA
Sample source code for finding the least common multiple of multiple values in Java
Count the number of occurrences of a string in Ruby
Determine that the value is a multiple of 〇 in Ruby
[Ruby] Questions and verification about the number of method arguments
Find the number of days in a month with Kotlin
Set the number of seconds for fast forward and rewind in ExoPlayer
[Ruby basics] About the role of true and break in the while statement
How to change the maximum and maximum number of POST data in Spark
How to find the total number of pages when paging in Java
Find the maximum and minimum of the five numbers you entered in Java
How to insert processing with any number of elements in iterative processing in Ruby
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
Find the difference from a multiple of 10
Could not find coderay-1.1.3 in any of the sources (Bundler :: GemNotFound) during co-development
How to find the cause of the Ruby error
I checked the number of taxis with Ruby
Get the value from the array and find out what number it is included in
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Ruby] The role of subscripts in learning elements in arrays
[Ruby] How to find the sum of each digit
About the difference between classes and instances in Ruby
Set the maximum number of characters in UITextField in RxSwift
[Technical memo] About the advantages and disadvantages of Ruby
Find the approximate value of log (1 + x) in Swift
[Ruby] Class nesting, inheritance, and the basics of self
Get the URL of the HTTP redirect destination in Ruby
Handling of date and time in Ruby. Use Date and Time properly.