Sha256: 50f9c80e10943d97b3361d3c197458e11bff20ee6436f416f9bc5ec7cf44ee88

Contents?: true

Size: 534 Bytes

Versions: 6

Compression:

Stored size: 534 Bytes

Contents

class Integer
  def delannoy?
    return true if self == 1
    max_a, max_b = self/2, self/2
    (1..max_a).each do |a|
      (1..max_b).each do |b|
        d = a.delannoy(b)
        return true if d == self
        if d > self
          max_b = b 
          max_a = a
          break
        end
      end
      break if a > max_a
    end
    false
  end

  def delannoy(b)
    a = self
    return 1 if b.zero? or a.zero?
    [(a - 1).delannoy(b), a.delannoy(b - 1), (a - 1).delannoy(b - 1)].reduce(:+)
  end
  memoize :delannoy
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
numb-0.186.0 lib/numb/delannoy.rb
numb-0.185.0 lib/numb/delannoy.rb
numb-0.184.0 lib/numb/delannoy.rb
numb-0.181.0 lib/numb/delannoy.rb
numb-0.170.0 lib/numb/delannoy.rb
numb-0.152.0 lib/numb/delannoy.rb