japhy has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to calculate the number of rolls possible. Everything from "1 1 1 1 1 1" to "6 6 6 6 6 6". However, the roll "1 1 1 1 1 2" is no different from "1 1 1 2 1 1". I'm trying to determine the mathematical approach to calculating the number of rolls possible. I can produce them (there are 462), but I can't figure out a generic mathematical formula to get that number.
From a regex standpoint, it's /^(?=.{6}$)1*2*3*4*5*6*/. (This is extraneous, but it defines the strings that match. "111112" is valid, "121111" is not,, because it's "identical".)
Here's my Perl code. It looks eerily familiar, like I've seen this somewhere before, but I don't know where offhand:
sub unordered_sets { my ($in, $out, $left, $idx, $curr) = @_; push @$out, [@$curr] and return if $left == 0; $idx ||= 0; for ($idx .. $#$in) { push @$curr, $in->[$_]; unordered_sets($in, $out, $left-1, $_, $curr); pop @$curr; } } my @sets; unordered_sets [1..6], \@sets, 6;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: unordered sets of N elements
by Limbic~Region (Chancellor) on Jul 13, 2004 at 13:36 UTC | |
by dragonchild (Archbishop) on Jul 13, 2004 at 13:48 UTC | |
by japhy (Canon) on Jul 13, 2004 at 13:50 UTC | |
Re: unordered sets of N elements
by dragonchild (Archbishop) on Jul 13, 2004 at 13:42 UTC | |
by japhy (Canon) on Jul 13, 2004 at 13:47 UTC | |
by dragonchild (Archbishop) on Jul 13, 2004 at 13:50 UTC | |
by Limbic~Region (Chancellor) on Jul 13, 2004 at 14:39 UTC | |
Re: unordered sets of N elements
by kvale (Monsignor) on Jul 13, 2004 at 15:32 UTC | |
Re: unordered sets of N elements
by hv (Prior) on Jul 13, 2004 at 20:55 UTC |