We don't bite newbies here... much PerlMonks

### Re: Can this be explained in layman's terms?

by Athanasius (Archbishop)
 on Jan 14, 2017 at 07:41 UTC ( #1179560=note: print w/replies, xml ) Need Help??

Hello BrowserUk,

A Stirling number of the second kind, S(n,k), gives the number of ways in which a set of n elements can be partitioned into exactly k non-empty subsets. It can be calculated by an explicit formula:

S(n,k) = 1/k!.SUM[j=0 to k]( (-1)^(k-j) . kCj . j^n )

where kCj is a binomial coefficient (“ k select j ”). But if you’re calculating successive values of S, it will be more efficient to use the recurrence relation:

S(n+1,k) = k.S(n,k) + S(n,k-1)

Here’s a simple implementation:

```use strict;
use warnings;

my (\$n, \$k) = (20, 11);

print "S(\$n, \$k) = ", S_formulaic(\$n, \$k), "\n";
print "S(\$n, \$k) = ", S_recursive(\$n, \$k), "\n";

sub S_recursive
{
my (\$n, \$k) = @_;

return 1 if \$n == 0 && \$k == 0;
return 0 if \$k == 0;
return 1 if \$k == 1;
return 1 if \$n == \$k;

return S_recursive(\$n - 1, \$k - 1) + \$k * S_recursive(\$n - 1, \$k);
}

sub S_formulaic
{
my (\$n, \$k) = @_;
my  \$sum    = 0;

for my \$j (0 .. \$k)
{
\$sum += (-1) ** (\$k - \$j) * C(\$k, \$j) * \$j ** \$n;
}

return (1 / fact(\$k)) * \$sum;
}

sub C
{
my (\$n, \$k) = @_;

my \$p  = 1;
\$p *= \$_ for (\$n - \$k + 1) .. \$n;

return \$p / fact(\$k);
}

sub fact
{
my (\$n) = @_;
my  \$p  = 1;
\$p *= \$_ for 2 .. \$n;

return \$p;
}

Output:

```17:17 >perl 1740_SoPW.pl
S(20, 11) = 1900842429486
S(20, 11) = 1900842429486

17:17 >

I would say, “hope that helps,” but I’m sure you already know all of the above. So my best hope is that it will help you to clarify what you mean by “tangible explanation” and “transcribing the above description into English.” Then again, if what you’re really after is an explanation of the maths behind the formulae, you’ll need an actual mathematician. ;-)

Cheers,

 Athanasius <°(((>< contra mundum Iustus alius egestas vitae, eros Piratica,

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-08-15 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?