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,


In reply to Re: Can this be explained in layman's terms? by Athanasius
in thread [Answered; thanks.] Can this be explained in layman's terms? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.