Skip to content

Commit

Permalink
Integer#stirling2: Add
Browse files Browse the repository at this point in the history
  • Loading branch information
runpaint committed Mar 22, 2010
1 parent 9fed2cb commit f5cf7c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/numb/stirling2.rb
@@ -0,0 +1,9 @@
# coding: utf-8
class Integer
def stirling2(m)
n = self
return 1 if (n.zero? and m.zero?) or m == n
return 0 if m > n or m.zero? or n.zero?
(n-1).stirling2(m-1) + m * (n-1).stirling2(m)
end
end
23 changes: 23 additions & 0 deletions spec/numb/stirling2_spec.rb
@@ -0,0 +1,23 @@
# coding: utf-8
describe Integer, "#stirling2" do
@seq = [
[1],
[1, 1],
[1, 3, 1],
[1, 7, 6, 1],
[1, 15, 25, 10, 1],
[1, 31, 90, 65, 15, 1],
[1, 63, 301, 350, 140, 21, 1],
[1, 127, 966, 1701, 1050, 266, 28, 1],
[1, 255, 3025, 7770, 6951, 2646, 462, 36, 1],
[1, 511, 9330, 34105, 42525, 22827, 5880, 750, 45, 1]
]

@seq.to_enum.with_index(1).each do |row, n|
row.to_enum.with_index(1).each do |s, m|
it "returns #{s} as the #{n}#{n.ordinal} Stirling number of the 2nd kind (m = #{m})" do
n.stirling2(m).should == s
end
end
end
end

0 comments on commit f5cf7c4

Please sign in to comment.