~ubuntu-branches/ubuntu/trusty/ruby-facets/trusty

« back to all changes in this revision

Viewing changes to lib/standard/facets/math/gcd.rb

  • Committer: Package Import Robot
  • Author(s): Marc Dequènes (Duck)
  • Date: 2012-01-03 03:35:44 UTC
  • Revision ID: package-import@ubuntu.com-20120103033544-pq0yyio1675gfgw6
Tags: upstream-2.9.2
Import upstream version 2.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Math
 
2
 
 
3
  # Greatest common divisor of +m+ and +n+, +nil+ for non-positive
 
4
  # numbers - gcd is computed by means of the Euclidian algorithm.
 
5
  def self.gcd(m, n)
 
6
    m = Integer(m)
 
7
    n = Integer(n)
 
8
    if m <= 0 || n <= 0
 
9
      return nil
 
10
    end
 
11
    loop {
 
12
      if m < n
 
13
        m, n = n, m
 
14
      end
 
15
      if (l = m % n) == 0
 
16
        break
 
17
      end
 
18
      m = l
 
19
    }
 
20
    n
 
21
  end
 
22
 
 
23
end