euclidean algorithm


See On Github

Data

Contributor

Generic placeholder thumbnail

by mitogh

in ruby

Tags

Source Code

def gcd_division(a, b)
  while b != 0 do
    a, b = b, a % b
  end
  a
end

def gcd_substraction(a, b)
  while(a != b) do 
    a = a - b if a > b
    b = b - a if a < b
  end
  a
end

def gcd_recursive(a, b) 
  (b == 0) ? a : gcd_recursive(b, a % b)
end
require './euclidean.rb'

describe "#gcd_divsion" do
  context "Test GCD of 210 and 45" do
    it{ gcd_division(210,45).should eq(15) }
  end
  context "Test GCD of 0 and 0" do
    it{ gcd_division(0,0).should eq(0) }
  end
  context "Test GCD of 20 and 97" do
    it{ gcd_division(20,77).should eq(1) }
  end
end

describe "#gcd_substraction" do
  context "Test GCD of 210 and 45" do
    it{ gcd_substraction(210,45).should eq(15) }
  end
  context "Test GCD of 0 and 0" do
    it{ gcd_substraction(0,0).should eq(0) }
  end
  context "Test GCD of 20 and 97" do
    it{ gcd_substraction(120,77).should eq(1) }
  end
end

describe "#gcd_divsion" do
  context "Test GCD of 210 and 45" do
    it{ gcd_recursive(210,45).should eq(15) }
  end
  context "Test GCD of 0 and 0" do
    it{ gcd_recursive(0,0).should eq(0) }
  end
  context "Test GCD of 20 and 97" do
    it{ gcd_recursive(20,77).should eq(1) }
  end
end