-->

## Monday, 4 March 2013

### Finding Hand Distributions with Scala

It's a good thing for a language to get out of your way and let you solve problems. It is even better when it helps you solve problems.

I was recently thinking about how many different hand distributions there are in bridge. This can be restated as dividing thirteen elements between four groups. My first thought was that you should make the condition that the any group should receive at least as many cards as the group before it. This meant that if you gave the first group three cards then all the other groups have at least three. With only one card left to distribute it has to go to the fourth group.

I was about to reach for pen and paper when I realized that scala will actually solve this problem for me and I quickly scratched out the following code:

for {

i <- 0 to 3

j <- i to (13-i)/3

k <- j to (13-i-j)/2

} yield (i,j,k,13-i-j-k)

-


So here are your 39 results:

(0,0,0,13), (0,0,1,12), (0,0,2,11), (0,0,3,10), (0,0,4,9), (0,0,5,8),(0,0,6,7), (0,1,1,11), (0,1,2,10), (0,1,3,9), (0,1,4,8), (0,1,5,7), (0,1,6,6), (0,2,2,9), (0,2,3,8), (0,2,4,7), (0,2,5,6), (0,3,3,7), (0,3,4,6), (0,3,5,5), (0,4,4,5), (1,1,1,10), (1,1,2,9), (1,1,3,8), (1,1,4,7), (1,1,5,6), (1,2,2,8), (1,2,3,7), (1,2,4,6), (1,2,5,5), (1,3,3,6), (1,3,4,5), (1,4,4,4), (2,2,2,7), (2,2,3,6), (2,2,4,5), (2,3,3,5), (2,3,4,4), (3,3,3,4)

The distribution ordered by cards in the lowest group is 1, 5,12 and 21. According to the Online Encyclopedia of Integer Sequences this coincides with the rounded partial sum of the atomic weights of the first n elements. It's probably just a coincidence.