In terms of the original problem:
The city boy loved the farmers daughter.The farmer said city boys were not very smart but he would give his daughter hand if he could do the following: Go up the hill to the apple tree and pick a certain number of apples.
As he came back down the hill he would pass throught three gates.
At each gate he must leave half his apples plus, half an apple.
A whole number of apples must be left at each gate.
How many apples must he pick?
It is not said how many apples he has left when he reaches the bottom, which determines how many he started with.
If after passing through the last gate he has no apples left, he left 1 apple at the last gate, arriving with 1 apple and leaving half his apples, 1/2, plus 1/2 an apple equaling 1 whole apple.
Since he had 1 apple left after leaving the 2nd gate, he must have had (1 1/2)x2 = 3 apples when he arrived at the 2nd gate, leaving half his apples, 1 1/2, plus 1/2 an apple equaling 2 whole apples.
Since he had 3 apple left after leaving the 1st gate, he must have had (3 1/2)x2 = 7 apples when he arrived at the 1st gate, leaving half his apples, 3 1/2, plus 1/2 an apple equaling 4 whole apples.
Thus, if no apples are left after the 3rd gate, he started out with 7 apples.
For any N apples left after leaving the last gate, you would start out with
[[(N + 1/2)2 + 1/2]2 + 1/2]2 = 8N + 7 apples
Starting out with X apples, he has X/2 - 1/2 left after the first gate or (X - 1)/2.
After the second gate he has (X - 1)/4 - 1/2 left or (X - 3)/4.
After third third gate he has (X - 7)/8 left.
If he was to have none left, (X - 7)/8 = 0 and X = 7.
If he was to have 1 apple left, (X - 7)/8 = 1 and X = 15.
Starting with X and ending with N, X = 8N + 7, the same as given you earlier.