Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-

Coding with your own knowledge to learn about algorithms. Day 1 I don't know if it will continue

What is Euclidean algorithm?

One of the most famous algorithms. No more explanation needed. Finding the greatest common divisor for two natural numbers a and b. The method finds the remainders of a and b. Next, b and the remainder of the remainder are obtained. Then, the remainder of the surplus and the surplus of the surplus of the surplus are obtained. The divisor when repeated until the remainder becomes 0 is the common divisor of a and b.

Once you understand how it works, code it immediately

Euclid.rb

def euclid(a, b)
  while b != 0
    mod = a % b
    a = b
    b = mod
  end
  a
end

print "Value of a:"
a = gets.to_i
print "value of b:"
b = gets.to_i

puts "The greatest common divisor of a and b is#{euclid(a,b)}is"                                         

Enter a and b and use them as method arguments.

Substitute the remainder of a and b for mod Substitute b for a Substitute mod for b

Repeat this only when b is greater than 0

Probably no problem

Finally

It's a simple code so there should be no mistake What we want from experts -Code simplification ・ Reduction of processing speed I don't know if it's feasible because I've come up with it, but thank you.

Tomorrow we will implement bubble sort.

Recommended Posts

Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
Implement the algorithm in Ruby: Day 3 -Binary search-
Implement the algorithm in Ruby: Day 4-Linear search-
Implement the algorithm in Ruby: Day 2 -Bubble sort-
I tried to implement the Euclidean algorithm in Java
Try to implement Yubaba in Ruby
Implement a gRPC client in Ruby
The ruby version is managed in the .rbenv / version file
Class in Ruby
[Ruby] Code to display the day of the week
Heavy in Ruby! ??
Implement the Like feature in Ajax with Rails.
How Docker works ~ Implement the container in 60 lines
How to build the simplest blockchain in Ruby
How to implement Pagination in GraphQL (for ruby)
I want to get the value in Ruby
Count the number of occurrences of a string in Ruby
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby] The role of subscripts in learning elements in arrays
Examine the elements in the array using the [Ruby] includes? Method
About the difference between classes and instances in Ruby
Calculate the difference between numbers in a Ruby array
[Ruby / Rails] Set a unique (unique) value in the class
Get the URL of the HTTP redirect destination in Ruby
Output triangle in Ruby
Implement CustomView in code
Variable type in ruby
About the [ruby] operator
Fast popcount in Ruby
Implement markdown in Rails
[Ruby] Count an even number in an array using the even? Method
Get the next business day after the specified date in JAVA
Determine that the value is a multiple of 〇 in Ruby
[Ruby on Rails] Quickly display the page title in the browser
About the difference between "(double quotation)" and "single quotation" in Ruby
How to retrieve the hash value in an array in Ruby
[Ruby] Display today's day of the week using Date class
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Implement the same function as C, C ++ system ("cls"); in Java
How to derive the last day of the month in Java