How to calculate the GCD (greatest common divisor) of two integers in Ruby

2 Answers

0 votes
a = 12
b = 20

g = a.gcd(b)
puts g   


=begin
run:
 
4
 
=end

 



answered Jul 3 by avibootz
0 votes
def gcd(a, b)
  while b != 0
    a, b = b, a % b
  end
  a
end

puts gcd(12, 20)  



=begin
run:
 
4
 
=end

 



answered Jul 3 by avibootz
...