Раздел «Язык Ruby».GcdAndLcm:

Methods gcd and lcm

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

def lcm(a,b)
  a*b/gcd(a,b)
end

if $0 == __FILE__
  require 'test/unit'
  class GcdTest < Test::Unit::TestCase
    def test_gcd
      # puts methods.grep(/assert/)
      assert_equal( 13, gcd(13,13) )
      assert_equal( 1, gcd(13,11) )
      assert_equal( 2, gcd(14,1024) )
      assert_equal( 8, gcd(8,256) )
      assert_equal( 13, gcd(13,13**10) )
      assert_equal( 134, gcd(0, 134) )
    end

    def test_lcm
      assert_equal(12, lcm(6,4))
      assert_equal(1, lcm(1,1))
    end
  end
end

-- ArtemVoroztsov - 25 Apr 2008