Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • mathn.rb

Class/Module Index [+]

Quicksearch

Integer

Public Class Methods

from_prime_division(pd) click to toggle source
 
               # File mathn.rb, line 37
def Integer.from_prime_division(pd)
  value = 1
  for prime, index in pd
    value *= prime**index
  end
  value
end
            

Public Instance Methods

gcd2(int) click to toggle source
 
               # File mathn.rb, line 19
def gcd2(int)
  a = self.abs
  b = int.abs
  a, b = b, a if a < b
  
  pd_a = a.prime_division
  pd_b = b.prime_division
  
  gcd = 1
  for pair in pd_a
    as = pd_b.assoc(pair[0])
    if as
      gcd *= as[0] ** [as[1], pair[1]].min
    end
  end
  return gcd
end
            
prime_division() click to toggle source
 
               # File mathn.rb, line 45
def prime_division
  raise ZeroDivisionError if self == 0
  ps = Prime.new
  value = self
  pv = []
  for prime in ps
    count = 0
    while (value1, mod = value.divmod(prime)
           mod) == 0
      value = value1
      count += 1
    end
    if count != 0
      pv.push [prime, count]
    end
    break if prime * prime  >= value
  end
  if value > 1
    pv.push [value, 1]
  end
  return pv
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus