| 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(other) min = self.abs max = other.abs while min > 0 tmp = min min = max % min max = tmp end max 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 473 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 457 def lcm(other) if self.zero? or other.zero? 0 else (self.div(self.gcd(other)) * other).abs end end
ruby-doc.org is hosted and run by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.