Add double dispatch to Integer

Methods

denominator   from_prime_division   gcd   gcd2   gcd2   gcdlcm   lcm   numerator   prime_division   to_bn   to_r  

Public Class methods

[Source]

    # File mathn.rb, line 37
37:   def Integer.from_prime_division(pd)
38:     value = 1
39:     for prime, index in pd
40:       value *= prime**index
41:     end
42:     value
43:   end

Public Instance methods

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

[Source]

     # File rational.rb, line 417
417:   def denominator
418:     1
419:   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.

[Source]

     # File rational.rb, line 438
438:   def gcd(n)
439:     m = self.abs
440:     n = n.abs
441: 
442:     return n if m == 0
443:     return m if n == 0
444: 
445:     b = 0
446:     while n[0] == 0 && m[0] == 0
447:       b += 1; n >>= 1; m >>= 1
448:     end
449:     m >>= 1 while m[0] == 0
450:     n >>= 1 while n[0] == 0
451:     while m != n
452:       m, n = n, m if n > m
453:       m -= n; m >>= 1 while m[0] == 0
454:     end
455:     m << b
456:   end

[Source]

    # File mathn.rb, line 19
19:   def gcd2(int)
20:     a = self.abs
21:     b = int.abs
22:     a, b = b, a if a < b
23:     
24:     pd_a = a.prime_division
25:     pd_b = b.prime_division
26:     
27:     gcd = 1
28:     for pair in pd_a
29:       as = pd_b.assoc(pair[0])
30:       if as
31:         gcd *= as[0] ** [as[1], pair[1]].min
32:       end
33:     end
34:     return gcd
35:   end

[Source]

     # File rational.rb, line 458
458:   def gcd2(int)
459:     a = self.abs
460:     b = int.abs
461: 
462:     a, b = b, a if a < b
463: 
464:     while b != 0
465:       void, a = a.divmod(b)
466:       a, b = b, a
467:     end
468:     return a
469:   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]

[Source]

     # File rational.rb, line 495
495:   def gcdlcm(other)
496:     gcd = self.gcd(other)
497:     if self.zero? or other.zero?
498:       [gcd, 0]
499:     else
500:       [gcd, (self.div(gcd) * other).abs]
501:     end
502:   end

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
479:   def lcm(other)
480:     if self.zero? or other.zero?
481:       0
482:     else
483:       (self.div(self.gcd(other)) * other).abs
484:     end
485:   end

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

[Source]

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

[Source]

    # File mathn.rb, line 45
45:   def prime_division
46:     ps = Prime.new
47:     value = self
48:     pv = []
49:     for prime in ps
50:       count = 0
51:       while (value1, mod = value.divmod(prime)
52:              mod) == 0
53:         value = value1
54:         count += 1
55:       end
56:       if count != 0
57:         pv.push [prime, count]
58:       end
59:       break if prime * prime  >= value
60:     end
61:     if value > 1
62:       pv.push [value, 1]
63:     end
64:     return pv
65:   end

[Source]

    # File openssl/bn.rb, line 31
31:   def to_bn
32:     OpenSSL::BN::new(self)
33:   end

Returns a Rational representation of this integer.

[Source]

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

Search

Google

Ruby API Docs

Links