find the number of the last locker opened 
Person A walks through a row of n lockers that are all initially closed. A opens the first locker, then skips the next one, and alternates until A reaches the end. At the end, A opens the first closed locker, and alternates skipping and opening lockers until A reaches the beginning. Find a formula for the number of the last locker A opens. For instance, if $n=4,$ A opens lockers in the order $1,3,4,2,$ and A makes 3 passes, where the second pass involves opening locker 4 and the last one involves opening locker 2.

I can solve the problem if $n$ is a power of $2$. Let $L_k$ be the number of the last locker opened if $n=2^k$. To obtain a recurrence, assume $k\ge 1.$ Consider renumbering the lockers $1,2,\cdots, 2^{k-1}$, starting from locker number $2^k$ and preceding downwards. Then locker number $i$ is renumbered as $\frac{1}2 (2^k - i) + 1.$ In particular, $L_{k-1}$, the number in the new numbering that corresponds to $L_k$ in the original numbering, satisfies $L_{k-1} = \frac{1}2 (2^k - L_k) + 1,$ from which we deduce the recurrence $L_k = 2^k - 2 (L_{k-1} - 1)$. Expanding further by one index, we see $L_k = 2^k - 2(2^{k-1} - 2(L_{k-2} - 1) - 1) = 4L_{k-2} - 2.$ One can also deduce an explicit formula for $L_k$.

But how would one solve the problem for general n?

 A: This is the so called "spectator-first Tantalizer problem".
The sequence of solutions is A34735 at OEIS. There's the recursive formula
$$a(n) = 2 \left( \left \lfloor \frac{n}{2} \right\rfloor + 1 - a\left(\left \lfloor \frac{n}{2}\right\rfloor \right) \right)$$
and a link to the paper where a formula in terms of the binary representation of $n$ is derived.
A: If $A_n$ is the last when starting with $n,$ then $A_{2m}$ leaves the $m$ lockers $2,4,6,\dots,2m$ open after the first pass. The remainder of the game is like the game with $m$ lockers, only with the lockers $1,2,\dots,m$ relabeled $2m,2(m-1),\dots,2.$ So locker $k$ is relabeled $2(m+1-k).$
So you get the recurrence $A_{2m}=2(m+1-A_m).$
Similarly, you can get a recursion for $A_{2m+1}.$ It is essentially the same recurrence, since you are left after the first pass being $2,\dots,2m$ again, and you get, for general $n,$ $$A_1=1, A_{n}=2\left(\lfloor n/2\rfloor+1-A_{\lfloor n/2\rfloor}\right).$$
You might be able to get a formula of the form:
$$\left(2\lfloor n/2\rfloor -4\lfloor n/4\rfloor +8\lfloor n/8\rfloor -\cdots \right)+\left(2-4+\cdots\pm 2^{\lfloor \log n\rfloor}\right)\mp2^{\lfloor \log_2 n\rfloor}A_1$$
or something like:
$$A_n=(-2)^{\lfloor\log_2 n\rfloor}+\sum_{1\leq k\leq \log_2n}(-1)^{k-1}2^k\left(\left\lfloor\frac n{2^k}\right\rfloor +1\right)$$ for $n\geq 1.$
