Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Combinatorics problem.

by Anonymous Monk
on Dec 12, 2015 at 00:13 UTC ( [id://1150085]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Combinatorics problem.
in thread Combinatorics problem. (Updated with more info.)

So we're trying to distribute 5 cards over 3 holes. Here're the holes:
|_|_|_|
Let's put the cards there. Well, they won't fit, because the '_' (underscore) occupies the whole cell, rather than just the bottom of it. Let's get rid of underscores. They're just for visials anyway.
| | | |
Now it fits. One way to do it:
1 1 |1|1|1|
or maybe like this:
1 1 |1|1|1|
First, the bottom row of ones is always the same. We don't actually need it to count combination. Lets just remove it (like in tetris). The number of ways to distribute 5 cards over 3 holes with BUK's constraint is the same as the number of ways to distribute 2 cards over 3 holes without this constraint.
1) |1|1| | 2) | |1|1| 1 3) | | |1| 4) and so on
That actually looks like a datastructure we can work with. The problem is it's two dimensional. Which is kind of a pain. But! we can flatten it. Like so:
1) | |1|1| 2) |1|1| | 3) |11| | | 4) | |11| | 5) and so on
Now there are two more things. The spaces in empty holes are not necessary. Also, the two '|' (pipes) at the left and right edges of our strings are not necessary either. That's also just visuals. Let's remove that stuff.
1) | | |11| => ||11 => 0 0 2 2) |1|1| | => 1|1| => 1 1 0 3) | |11| | => |11| => 0 2 0 4) etc
For no particular reason I used '0' instead of '|' in hacky solution :) (hm, I think that's because it reminded me of a recent thread). Like that:
1) 0011 => 0 0 2 2) 1010 => 1 1 0 3) 0110 => 0 2 0
Now we just want to count all combinations of such strings. The length of a string is $cards (which is the number of ones) + $holes - 1 (which is the number of zeros). That's our $n. And $r is obviously the number of cards. Think of it this way:
0000 - start with string of all zeros .00. - take some zeroes that way 1001 - and replace with ones 0..0 - take some zeroes that way 0110 - and replaces with ones 00.. - then that way 0011 - you got the idea
Hopefully now its clear that the number of ways we can produce such strings is n! / (r!(n-r)!), just like in a textbook.

And my hacky solution literally builds these strings and counts the length of sequences of ones :) Which is a bit of a nonsense, and GrandFather's method is a lot better, but hey! it works ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-24 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found