Sha256: 3282ab75c343365cfd3026ba1455ab054e92d27185b8aea735ccaeaca1f3a4d0

Contents?: true

Size: 535 Bytes

Versions: 20

Compression:

Stored size: 535 Bytes

Contents

module EbisuConnection
  class GreatestCommonDivisor
    class << self
      def calc(set)
        self.new(set).calc
      end
    end

    def initialize(set)
      @set = set.sort.uniq
    end

    def calc
      n = @set.shift
      return n if n == 1 || @set.empty?

      while !@set.empty?
        m = @set.shift
        n = gcd_euclid(m, n)
      end
      n
    end

    def gcd_euclid(m, n)
      m, n = n, m if m < n
      while n != 0
        work = m % n
        m = n
        n = work
      end
      m
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ebisu_connection-3.1.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-3.0.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.2 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.1 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.0.rc4 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.0.rc3 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.0.rc2 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.4.0.rc1 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.3.1 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.3.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.2.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.1.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-2.0.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-1.0.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-0.3.1 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-0.3.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-0.2.0 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-0.1.1 lib/ebisu_connection/greatest_common_divisor.rb
ebisu_connection-0.1.0 lib/ebisu_connection/greatest_common_divisor.rb