-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |