Skip to content

Commit

Permalink
Integer#super_catalan?: Add
Browse files Browse the repository at this point in the history
  • Loading branch information
runpaint committed Mar 23, 2010
1 parent 7514b79 commit 7d0f3c1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/numb/super_catalan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# coding: utf-8
class Integer
def super_catalan
n = self
return 1 if n <= 2
Rational(
3 * ((2 * n) - 3) * (n - 1).super_catalan - (n - 3) * (n - 2).super_catalan,
n
).to_i
end

memoize :super_catalan

def super_catalan?
in_sequence?(seq: :super_catalan)
end
end
29 changes: 29 additions & 0 deletions spec/numb/super_catalan_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8
# A001003
SUPER_CATALAN = [1,1,3,11,45,197,903,4279,20793,103049,518859,
2646723,13648869,71039373,372693519,1968801519,
10463578353,55909013009,300159426963,
1618362158587,8759309660445,47574827600981,
259215937709463,1416461675464871]

describe Integer, "#super_catalan" do
SUPER_CATALAN.to_enum.with_index(1).each do |s, n|
it "returns #{s} as the #{n}#{n.ordinal} super-Catalan number" do
n.super_catalan.should == s
end
end
end

describe Integer, "#super_catalan?" do
SUPER_CATALAN.each do |n|
it "returns true for super-Catalan number #{n}" do
n.should be_super_catalan
end
end

SUPER_CATALAN.to_seq.invert.sample(100).each do |n|
it "returns false for non-super-Catalan number #{n}" do
n.should_not be_super_catalan
end
end
end

0 comments on commit 7d0f3c1

Please sign in to comment.