Sha256: e7937716137278c25d33ad8a0b864d64f23d2ea43880975849df598abd509fbe

Contents?: true

Size: 360 Bytes

Versions: 18

Compression:

Stored size: 360 Bytes

Contents

class Integer
  def binomial?(exp=4)
    x = self
    return true if (0..2).include? x
    (2..exp).each do |n|
      (1...x).each do |a|
        an = a**n
        sign, *terms = an > x ? [:-, an, x] : [:+, x, an]
        b = (terms.reduce(:-))**(1.0/n.to_f)
        return true if b.integer? and x == an.send(sign, b**n)
      end
    end
    false
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
numb-0.186.0 lib/numb/binomial.rb
numb-0.185.0 lib/numb/binomial.rb
numb-0.184.0 lib/numb/binomial.rb
numb-0.181.0 lib/numb/binomial.rb
numb-0.170.0 lib/numb/binomial.rb
numb-0.152.0 lib/numb/binomial.rb
numb-0.138.0 lib/numb/binomial.rb
numb-0.125.0 lib/numb/binomial.rb
numb-0.114.0 lib/numb/binomial.rb
numb-0.111.0 lib/numb/binomial.rb
numb-0.109.0 lib/numb/binomial.rb
numb-0.99.0 lib/numb/binomial.rb
numb-0.96.0 lib/numb/binomial.rb
numb-0.89.0 lib/numb/binomial.rb
numb-0.84.0 lib/numb/binomial.rb
numb-0.77.0 lib/numb/binomial.rb
numb-0.72.1 lib/numb/binomial.rb
numb-0.72.0 lib/numb/binomial.rb