Rosetta Code

Greatest common divisor

Compute greatest common divisors with the iterative Euclidean algorithm on a fixed sample set.

Intro View source
Source rosettacode/popular/greatest_common_divisor.vibe
# title: Greatest common divisor
# source: https://rosettacode.org/wiki/Greatest_common_divisor
# category: Rosetta Code
# difficulty: Intro
# summary: Compute greatest common divisors with the iterative Euclidean algorithm on a fixed sample set.
# tags: popular, math, euclid, loops
# vibe: 0.2

def gcd(a, b)
  left = a.abs
  right = b.abs

  while right != 0
    remainder = left % right
    left = right
    right = remainder
  end

  left
end

def run
  {
    zero_zero: gcd(0, 0),
    zero_ten: gcd(0, 10),
    zero_negative_ten: gcd(0, -10),
    nine_six: gcd(9, 6),
    six_nine: gcd(6, 9),
    negative_six_nine: gcd(-6, 9),
    eight_forty_five: gcd(8, 45),
    large_pair: gcd(84, 30)
  }
end
Output
Press run to execute run from this example.
rosetta-code popular math euclid loops browser-runner