summaryrefslogtreecommitdiffstats
path: root/lib/rational.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-02-20 00:55:49 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-02-20 00:55:49 +0000
commitf652cadb5a053a5b9df51e03bb23cefac92b4576 (patch)
treeed8b03ae45efee919bcf778b0c456748a0cbcc59 /lib/rational.rb
parent18d616be64b2d3d826fe449178d0b50237f47479 (diff)
downloadruby-f652cadb5a053a5b9df51e03bb23cefac92b4576.tar.gz
ruby-f652cadb5a053a5b9df51e03bb23cefac92b4576.tar.xz
ruby-f652cadb5a053a5b9df51e03bb23cefac92b4576.zip
* lib/rational.rb (Integer::gcd): replaced by gcd4 in
[ruby-core:07390]. [ruby-core:07377] * eval.c: initial value for block_unique must be 1. [ruby-talk:180420] git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@9965 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rational.rb')
-rw-r--r--lib/rational.rb36
1 files changed, 7 insertions, 29 deletions
diff --git a/lib/rational.rb b/lib/rational.rb
index 3f15cfafa..5220f319f 100644
--- a/lib/rational.rb
+++ b/lib/rational.rb
@@ -436,36 +436,14 @@ class Integer
# The result is positive, no matter the sign of the arguments.
#
def gcd(n)
- m = self.abs
- n = n.abs
-
- return n if m == 0
- return m if n == 0
-
- b = 0
- while n[0] == 0 && m[0] == 0
- b += 1; n >>= 1; m >>= 1
- end
- m >>= 1 while m[0] == 0
- n >>= 1 while n[0] == 0
- while m != n
- m, n = n, m if n > m
- m -= n; m >>= 1 while m[0] == 0
- end
- m << b
- end
-
- def gcd2(int)
- a = self.abs
- b = int.abs
-
- a, b = b, a if a < b
-
- while b != 0
- void, a = a.divmod(b)
- a, b = b, a
+ min = self.abs
+ max = other.abs
+ while min > 0
+ tmp = min
+ min = max % min
+ max = tmp
end
- return a
+ max
end
#