diff options
Diffstat (limited to 'lib/rational.rb')
-rw-r--r-- | lib/rational.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/rational.rb b/lib/rational.rb index 87c5d3f111..b12bf7ef38 100644 --- a/lib/rational.rb +++ b/lib/rational.rb @@ -15,3 +15,35 @@ class Bignum alias rpower ** end + +class Integer + + def gcd(other) + min = self.abs + max = other.abs + while min > 0 + tmp = min + min = max % min + max = tmp + end + max + end + + def lcm(other) + if self.zero? or other.zero? + 0 + else + (self.div(self.gcd(other)) * other).abs + end + end + + def gcdlcm(other) + gcd = self.gcd(other) + if self.zero? or other.zero? + [gcd, 0] + else + [gcd, (self.div(gcd) * other).abs] + end + end + +end |