Class: Integer

Class | Integer
In: | rational.rb
Parent: | Object

### Methods

denominator gcd gcd2 gcdlcm lcm numerator to_r

### Public Instance methods

denominator()

In an integer, the denominator is 1. Therefore, this method returns 1.

[Source]

    # File rational.rb, line 417
      def denominator
        1
      end

gcd(n)

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.

[Source]

    # 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

gcd2(int)

[Source]

    # 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

gcdlcm(other)

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]

[Source]

    # 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

lcm(other)

Returns the lowest common multiple (LCM) of the two arguments (`self` and `other`).

Examples:

      6.lcm 7        # -> 42
      6.lcm 9        # -> 18

[Source]

    # 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

numerator()

In an integer, the value is the numerator of its rational equivalent. Therefore, this method returns `self`.

[Source]

    # File rational.rb, line 410
      def numerator
        self
      end

to_r()

Returns a Rational representation of this integer.

[Source]

    # File rational.rb, line 424
      def to_r
        Rational(self, 1)
      end

[Validate]