# An efficient way to numerically compute Stirling numbers of the second kind?

Is there an efficient way to numerically compute Stirling numbers of the second kind?

An approximate (not exact) method would suffice. Something similar to the connection between factorials and gamma functions would work for me.

You can use the following recurrence relation

$\mathop{S}\nolimits\left(n,k\right)=k\mathop{S}\nolimits\left(n-1,k\right)+\mathop{S}\nolimits\!\left(n-1,k-1\right)$

and build the (triangular) table.

• Is there some relationships between Stirling numbers of the second kind and some analytical functions? I can use an approximate value in my case. – Tianyang Li Mar 8 '12 at 23:17
• there are asymptotics available. For these, you can also consult the DLMF or works by Nico Temme (N.M. Temme, "Asymptotic estimates of Stirling numbers", Stud. Appl. Math. 89, p233-243, 1993) – GertVdE Mar 9 '12 at 8:13

The accepted answer takes $O(kn)$ multiplications and additions. There is also a method using $O(k\log n)$ additions, multiplications and divisions, using the below identity: $$S(n,k)=\frac1{k!}\sum_{j=0}^k(-1)^j\binom{k}j(k-j)^n$$

Pseudocode:

accum ← 0
k_choose_j ← 1
for j = 0,1,...,k-1:
accum ← accum + (-1)^j * k_choose_j * (k - j)^n
k_choose_j ← k_choose_j * (k-j-1) / (j+1)
return accum / factorial(k)