On this page
A waiter can slide a spatula under any pancake in a stack and flip everything above it, and nothing else. How many flips does it take to put the smallest on top and the largest at the bottom? The puzzle was posed in 1975 by the geometer Jacob E. Goodman under the pen name "Harry Dweighter" (harried waiter), as the OEIS entry for the pancake numbers records. Each flip is what mathematicians call a prefix reversal: you reverse the first k items of a list and leave the rest alone.
What is pancake sorting?
Pancake sorting is the problem of sorting a stack of different-sized pancakes when the only allowed move is to flip all the pancakes above a spatula, as Wikipedia's pancake sorting article defines it. Write the stack top to bottom as a list, 1 the smallest: flipping the top 3 of 2, 4, 1, 5, 3 gives 1, 4, 2, 5, 3.
How does the simple algorithm sort in at most 2n − 3 flips?
The simplest pancake sorting method performs at most 2n − 3 flips, a kind of selection sort described in Wikipedia's article. It goes like this:
Find the largest pancake that is not yet in its final place.
If it is not already on top, flip it to the top.
Flip the whole unsorted part, which sends that pancake down to its place.
Repeat with the smaller stack above it.
Why 2n − 3? Placing each pancake from size n down to size 3 costs at most two flips, 2(n − 2) = 2n − 4 in all. Then only pancakes 1 and 2 remain, and one flip of the top two fixes them if needed.
Worked example: a stack of 5
Start with 2, 4, 1, 5, 3, top to bottom. The simple algorithm goes:
Flip 4: 5, 1, 4, 2, 3 (the 5 is on top)
Flip 5: 3, 2, 4, 1, 5 (the 5 is home)
Flip 3: 4, 2, 3, 1, 5
Flip 4: 1, 3, 2, 4, 5
Flip 2: 3, 1, 2, 4, 5
Flip 3: 2, 1, 3, 4, 5
Flip 2: 1, 2, 3, 4, 5
That is seven flips, exactly 2 × 5 − 3. But five are enough: flip 4, 5, 2, 3, then 4, as the diagram above shows (5, 1, 4, 2, 3, then 3, 2, 4, 1, 5, then 2, 3, 4, 1, 5, then 4, 3, 2, 1, 5, then sorted).
Can you do it in four? No. Count the breaks: neighbouring pancakes whose sizes do not differ by 1, plus one more if the bottom pancake is not the largest. The stack 2, 4, 1, 5, 3 has five breaks: 2|4, 4|1, 1|5, 5|3 and 3 on the plate. A flip of the top k pancakes keeps every neighbouring pair except the one where the spatula went in, so it removes at most one break. A sorted stack has no breaks, so this stack needs at least five flips.
What is the pancake number P(n)?
The pancake number P(n) is the number of flips needed to sort the worst stack of n pancakes, using the fewest flips for each stack, as listed in OEIS sequence A058986. The known values for n = 1 to 19 are 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20 and 22. So P(5) = 5, and our stack above is one of the worst. Finding the shortest flip sequence for a given stack is NP-hard, as Laurent Bulteau, Guillaume Fertin and Irena Rusu proved in Pancake Flipping is Hard.
What did Bill Gates's 1979 paper prove?
Bill Gates worked on this problem with one of his Harvard instructors, Christos Papadimitriou, several years before Microsoft was founded, according to Wikipedia's article. Their paper, "Bounds for sorting by prefix reversal", appeared in Discrete Mathematics in 1979, volume 27, pages 47 to 57 (DOI record). It showed that (5n + 5)/3 flips always suffice and that, when n is a multiple of 16, some stacks need at least 17n/16, as the OEIS entry records. For 100 pancakes, that is at most 168 flips instead of the simple method's 197. The upper bound stood for about thirty years until Chitturi, Fahle, Meng, Morales, Shields, Sudborough and Voit lowered it to about 18n/11 in Theoretical Computer Science in 2009.
What is the burnt-pancake problem?
In the burnt version each pancake has one burnt side, and the stack must end sorted with every burnt side down, as defined in OEIS sequence A078941. A flip now also turns pancakes over. The worst-case values for n = 1 to 6 are 1, 4, 6, 8, 10 and 12 flips, and for n of 10 or more the answer lies between 3n/2 and 2n − 2, according to the same OEIS entry. It also records a surprise: the sorted stack with every burnt side up was suspected to be a worst case, but Josef Cibulka showed that for 15 pancakes the worst stack needs 25 flips and that one only 24.
How to practise prefix reversals
Write a random stack of 5 or 6 numbers and sort it with the simple algorithm.
Count the breaks, then hunt for a shorter sort that removes one break per flip.
Try burnt pancakes: write a minus sign for burnt side up and flip the signs too.
For more puzzles like this, see how stack sorting connects to pattern avoidance, why permutation parity makes one 15 puzzle impossible, and how a greedy algorithm writes fractions as unit fractions.
When a counting or permutation problem refuses to come out, the Fermi Problem Library has a step-by-step solver that explains the reasoning, so you can see where your method went wrong.



