TheAlgorithms-Ruby/discrete_mathematics/euclidean_gcd.rb

15 lines
282 B
Ruby
Raw Permalink Normal View History

2021-02-07 08:05:54 +01:00
# https://en.wikipedia.org/wiki/Euclidean_algorithm
2017-10-01 15:36:02 +02:00
def euclidean_gcd(a, b)
while b != 0
t = b
b = a % b
a = t
end
2021-02-07 08:05:54 +01:00
a
2017-10-01 15:36:02 +02:00
end
2021-02-07 08:05:54 +01:00
puts 'GCD(3, 5) = ' + euclidean_gcd(3, 5).to_s
puts 'GCD(3, 6) = ' + euclidean_gcd(3, 6).to_s
puts 'GCD(6, 3) = ' + euclidean_gcd(6, 3).to_s