Class | Integer |
In: |
rational.rb
|
Parent: | Object |
In an integer, the denominator is 1. Therefore, this method returns 1.
# File rational.rb, line 417 def denominator 1 end
Returns the greatest common denominator of the two numbers (self and n).
Examples:
72.gcd 168 # -> 24 19.gcd 36 # -> 1
The result is positive, no matter the sign of the arguments.
# File rational.rb, line 438 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
# File rational.rb, line 458 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 end return a end
Returns the GCD and the LCM (see gcd and lcm) of the two arguments (self and other). This is more efficient than calculating them separately.
Example:
6.gcdlcm 9 # -> [3, 18]
# File rational.rb, line 495 def gcdlcm(other) gcd = self.gcd(other) if self.zero? or other.zero? [gcd, 0] else [gcd, (self.div(gcd) * other).abs] end end
Returns the lowest common multiple (LCM) of the two arguments (self and other).
Examples:
6.lcm 7 # -> 42 6.lcm 9 # -> 18
# File rational.rb, line 479 def lcm(other) if self.zero? or other.zero? 0 else (self.div(self.gcd(other)) * other).abs end end