Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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,


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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found