The "perfect shuffles" puzzle (solved in Haskell)
Posted by Tom Moertel Thu, 23 Mar 2006 21:51:00 GMT
I ran across a fun programming puzzle (via Raganwald):
Given a deck of n unique cards, cut the deck i cards from top and perform a perfect shuffle. A perfect shuffle begins by putting down the bottom card from the top portion of the deck followed by the bottom card from the bottom portion of the deck followed by the next card from the top portion, etc., alternating cards until one portion is used up. The remaining cards go on top. The problem is to find the number of perfect shuffles required to return the deck to its original order. Your function should be declared as:
static long shuffles(int nCards,int iCut);Please send the result of shuffles(1002,101) along with your program and your resume to ‘resume’ at nextag.com.
It’s a fun problem, so give it a try before reading on.
Warning: small spoilers ahead
Read more...
readers

