In summary:
- If n is even, divide by 2
- If n is 3 or its least significant bits are 01, subtract.
- If n's least significant bits are 11, add.
Repeat these operations on n until you reach 1, counting the number of operations performed. This is guaranteed to give the correct answer.
As an alternative to the proof from @trincot, here is one that has less cases and is hopefully more clear:
Proof:
Case 1: n is even
Let y be the value of the number after performing some operations on it. To start, y = n.
- Assume that dividing n by 2 is not the optimal approach.
- Then either add or subtract an even number of times
- Mixing addition and subtraction will result in unnecessary operations, so only either one is done.
- An even number must be added/subtracted, since stopping on an odd number would force continued adding or subtracting.
- Let 2k, where k is some integer, be the number of additions or subtractions performed
- Limit k when subtracting so that n - 2k >= 2.
- After adding/subtracting, y = n + 2k, or y = n - 2k.
- Now divide. After dividing, y = n/2 + k, or y = n/2 - k
- 2k + 1 operations have been performed. But the same result could have have been achieved in 1 + k operations, by dividing first then adding or subtracting k times.
- Thus the assumption that dividing is not the optimal approach was wrong, and dividing is the optimal approach.
Case 2: n is odd
The goal here is to show that when faced with an odd n, either adding or subtracting will result in less operations to reach a given state. We can use that fact that dividing is optimal when faced with an even number.
We will represent n with a partial bitstring showing the least significant bits: X1, or X01, etc, where X represents the remaining bits, and is nonzero. When X is 0, the correct answers are clear: for 1, you're done; for 2 (0b10), divide; for 3 (0b11), subtract and divide.
Attempt 1: Check whether adding or subtracting is better with one bit of information:
- Start: X1
- add: (X+1)0, divide: X+1 (2 operations)
- subtract: X0, divide: X (2 operations)
We reach an impass: if X or X+1 were even, the optimal move would be to divide. But we don't know if X or X+1 are even, so we can't continue.
Attempt 2: Check whether adding or subtracting is better with two bits of information:
- Start: X01
- add: X10, divide: X1
- add: (X+1)0, divide: X+1 (4 operations)
- subtract: X0, divide: X (4 operations)
- subtract: X00, divide: X0, divide: X (3 operations)
- add: X+1 (possibly not optimal) (4 operations)
Conclusion: for X01, subtracting will result in at least as few operations as adding: 3 and 4 operations versus 4 and 4 operations to reach X and X+1.
- Start: X11
- add: (X+1)00, divide: (X+1)0, divide: X+1 (3 operations)
- subtract: X (possibly not optimal) (4 operations)
- subtract: X10, divide: X1
- add: (X+1)0, divide: X+1 (4 operations)
- subtract: X0, divide: X (4 operations)
Conclusion: for X11, adding will result in at least as few operations as subtracting: 3 and 4 operations versus 4 and 4 operations to reach X+1 and X.
Thus, if n's least significant bits are 01, subtract. If n's least significant bits are 11, add.