I like to shuffle poker chips while I work. It gives me something to do while pages are loading or while I'm thinking that doesn't involve checking Twitter or Hacker News.
I got curious
how long it took for chip stacks of various heights to return to their shuffled state. However right now I can only shuffle stacks of 18 chips (9 red, 9 blue) before they come tumbling back to the floor, out of my hand. So I wrote a short program to simulate chip shuffles and determine how long it would take to get the stacks back to their shuffled state.
Here are the results:
Number of Red Chips Total Shuffles 1 2 2 4 3 3 4 6 5 10 6 12 7 4 8 8 9 18 10 6 11 11 12 20 13 18 14 28 15 5 16 10 17 12 18 36 19 12 20 20
That's a pretty interesting pattern. I'm sure there's a bunch of cool math behind it; I'll look it up soon and then update this post if I find any. Note that if the number of shuffles is divisible by two, the chips reverse order in the stack (ex. go from all red on the bottom to all red on the top) before returning to their original state.
Update: I found the pattern behind the poker chip shuffle sequence, on a site that lets you plug in any integer sequence and try to find the pattern that matches. You can find the pattern here. Glad I found that page, because I'm not sure I would have guessed the pattern. Here is more detail:
In other words, least m such that 2n+1 divides 2^m-1. Number of riffle shuffles of 2n+2 cards required to return a deck to initial state. A riffle shuffle replaces a list s(1), s(2), ..., s(m) by s(1), s((i/2)+1), s(2), s((i/2)+2), ... a(1) = 2 because a riffle shuffle of [1, 2, 3, 4] requires 2 iterations [1, 2, 3, 4] -> [1, 3, 2, 4] -> [1, 2, 3, 4] to restore the original order.
Here's the example code, in Haskell:
To output numbers, save that code as a file calledchips.hsand run:
$ ghci GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :l chips.hs [1 of 1] Compiling Main ( chips.hs, interpreted ) Ok, modules loaded: Main. *Main> zip [1..20] (map getNumberOfShuffles [1..20])
Liked what you read? I am available for hire.

I found the algorithm right away, which was to do a binary search and always move toward the region that contained the most points. However, this turned out to be tricky to implement in practice. Ultimately this problem was really difficult and only a few groups solved it correctly (the winning Harvey Mudd team solved seven of eight problems and this was the one problem they didn't solve). Trying it was a tactical mistake on my part, and we might have been able to solve a fourth if we'd attempted a different problem.
Ultimately it was a long day and as my teammate Jacob noted, "the nerdiest thing I've ever done." It was good I guess, considering I was the only person on our team who'd practiced before the contest and we were the first team ever from CMC to go to the competition. I'd wanted to join the competition because I show my resume to people and they say "Great you're an economics major, so, marketing?" and I wanted to show that I could code a little bit. It would have been great to do better in the competition but them's the breaks.
Among other things this shows how wide the differences between programmers can be; it's estimated that the best programmers are 10 times as efficient as average programmers, and looking at the times to completion it's easy to see how this is true. By the time I solved Problem 1 the Harvey Mudd team had already solved three problems, and there were some teams there that didn't even solve a single problem. These are hard to measure, which is why it's so important to actually look at someone's code to see how they do things, or ask them to write sample code.
All of my practice code can be found