Sign In

Communications of the ACM

Last byte

Puzzled: Solutions and Sources

three cards

Credit: Merchant of Magic Ltd.

1. Three cards

Even though it would seem not too many things can be done with just three cards, there are actually 1.64 × 1018 memoryless algorithms to try, with most not working. You need brainpower. With some reasoning and experimentation, you may find that moving the cards mostly in one direction (such as to the right, including "around the corner" from the right stack to the left stack) helps avoid looping. Moreover, you generally want to expose more cards, except perhaps in some positions from which you might win. Of the several algorithms that do work, I like the following one, as suggested by my mathematics Ph.D. student Ewa Infeld, given by these four rules in priority order:

  1. Seeing 2,1,–, place 1 on 2;
  2. Otherwise, seeing two cards, move one card right (around the corner if necessary) to the open place;
  3. Seeing just one card, move that card to the left; and
  4. Seeing three cards, move the middle-rank card to the right.

2. Any number of cards.

This same algorithm works for any number of cards, numbered 1 through n. It takes quadratic time in the worst or random case, or approximately a constant times n2 steps.


Marc Auslander

I believe the following is quadratic and has not more than one card in the middle -

Call the stacks (and their top cards) A, B, and C

If B is empty:
if C is empty move A to B
elIf A is empty or A > C move C to A
else move C to B

If B is not empty:
If A is empty move B to A
If C is not empty and A > B > C move B to A
else move C to A

Intuitively, search left to right for an out of order card, then search right to left for a place to insert it.
Once all are sorted, the left to right search forces everything into the leftmost stack.

Displaying 1 comment

Log in to Read the Full Article

Sign In

Sign in using your ACM Web Account username and password to access premium content if you are an ACM member, Communications subscriber or Digital Library subscriber.

Need Access?

Please select one of the options below for access to premium content and features.

Create a Web Account

If you are already an ACM member, Communications subscriber, or Digital Library subscriber, please set up a web account to access premium content on this site.

Join the ACM

Become a member to take full advantage of ACM's outstanding computing information resources, networking opportunities, and other benefits.

Subscribe to Communications of the ACM Magazine

Get full access to 50+ years of CACM content and receive the print version of the magazine monthly.

Purchase the Article

Non-members can purchase this article or a copy of the magazine in which it appears.
Sign In for Full Access
» Forgot Password? » Create an ACM Web Account