Problem 986. Penny Flipping: Reverse subsets of a sequence of coins until you recover the original configuration
The original Penny Flipping Problem (PF1) starts with a stack of N pennies arranged with all the coins heads up. The first operation is to flip the top coin. The second operation is to take the top two coins, invert this substack, and replace the substack on the main stack. The Nth operation is to invert the entire stack. The (N+1) operation is the same as the first operation. This sequence of reversals continues until the stack is returned to an all heads up configuration. Here is the sequence for N=3, with 0 corresponding to heads up and 1 corresponding to heads down:
0 1 2 3 4 5 6 7 8 9  0 1 1 1 0 0 1 0 1 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 1 1 1 0
Nine operations are required to regain the all heads up configuration for N=3. The problem for PF1 is to find the number of operations to regain all heads up, so PF1(3) = 9.
Note: Inverting the substack performs two distinct operations on the coins. The orientations of the coins are reversed, and the positions of the coins in the substack are also exchanged across the middle of the substack.
The alternating Penny Flipping Problem (PF2) is the same as for PF1 except that successive operations alternate from the top and the bottom of the stack, instead of just from the top, as with PF1. Here is the PF2 sequence for N=3:
0 1 2 3  0 1 1 0 0 0 1 0 0 0 1 0
So PF2(3) = 3.
Write a function that returns PF2(N), for N a positive integer.
I have posted a plot of PF2 for N=1:50 at this Google link:
Solution Stats
Problem Comments

5 Comments
I don't understand the first description. For me we obtain :
0 1 2 3 4 5 6

0 1 0 1 0 1 0
0 0 1 0 0 1 0
0 0 0 1 1 1 0
So so PF1(3) = 6.
Did I miss something ?
JeanMarie: I think the phrase, "invert the substack" is the issue. For me, this means to take the substack and invert it as a unit. The coins exchange positions in the substack as well as inverting their values. It is a single physical action that translates into two separate operations on the coins. I may have done better to write, "flip the substack as a unit."
Thank you Everett.
Now I understand the second operation, but not the Nth operation (in the example) when you invert all the stack. It's OK between steps 5 and 6 and between steps 8 and 9, but not between steps 2 and 3 (1 0 0 > 0 1 1 ?).
JeanMarie: The transformation between steps 2 and 3 (1 0 0 > 1 1 0) is the same as for the one between steps 8 and 9; we are working with the entire stack of 3 coins. The first/top coin exchanges with the last/bottom coin and then all coins change their orientation.
Every ith step, imagine that we are picking the top icoins with our fingers and fliping them upside down. That's FP1. FP2 does similar, but, instead of always picking the top icoins, it alternates between top icoins and bottom i+1coins.
Solution Comments
Show commentsProblem Recent Solvers14
Suggested Problems

Project Euler: Problem 16, Sums of Digits of Powers of Two
131 Solvers

694 Solvers

Rotate and display numbered tile
343 Solvers

Create an nbyn null matrix and fill with ones certain positions
538 Solvers

Natural numbers in string form
1217 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!