USAMTS $4/2/17$ Duck Goose Goose Problem This is the problem on USAMTS:

A teacher plays the game “Duck-Goose-Goose” with his class. The game is played as
  follows: All the students stand in a circle and the teacher walks around the circle. As he
  passes each student, he taps the student on the head and declares her a ‘duck’ or a ‘goose’.
  Any student named a ‘goose’ leaves the circle immediately. Starting with the first student,
  the teacher tags students in the pattern: duck, goose, goose, duck, goose, goose, etc., and
  continues around the circle (re-tagging some former ducks as geese) until only one student
  remains. This remaining student is the winner.
  For instance, if there are $8$ students, the game proceeds as follows: student $1$ (duck),
  student $2$ (goose), student $3$ (goose), student $4$ (duck), student $5$ (goose), student $6$ (goose),
  student $7$ (duck), student $8$ (goose), student $1$ (goose), student $4$ (duck), student $7$ (goose)
  and student $4$ is the winner. Find, with proof, all values of $n$ with $n > 2$ such that if the
  circle starts with $n$ students, then the $n$
  th student is the winner.

I've seen the case for $n=2$ in a Numberphile video which if expressed in base $2,$ just take the first digit and put it on the bottom, also called the Josephus problem.
How can I apply the same logic here?
Currently, I figured out that $3^n$ has position $1$ win every time, so I suspect the pattern still holds, but I can't prove it.
 A: In the first passing around the circle, student $i$ $(i$ ranging from $1$ to $n)$, will be excluded if and only if $i \equiv 0$ or $i\equiv 2\pmod 3$.
In other words, for the $n$-th student to avoid exclusion, we must have $n\equiv 1 \pmod 3$.
Suppose hence that $n = 3k+1$.
After tagging each student exactly once, we're down to a circle with $k+1$ students, so if $k=0$, the game ends right after this first pass.

Suppose hence that $k>0$, so the game will continue.
In this case, the $n$-th student is now the $(k+1)$-th.
Now, the teacher's pattern begins with Goose-Goose-Duck.
This means that if $k+1\leqslant 3$, the $n$-th student will survive as a result of all others being tagged Goose before him.

If $k+1>3$, then we'll do a full pass around the circle.
It follows that student $i$ $(i$ ranging from $1$ to $k+1)$ will be excluded if and only if $i \equiv 1$ or $i\equiv 2\pmod 3$.
Hence, in order for the $(k+1)$-th student to avoid exclusion, we must have $k+1 \equiv 0 \iff$ $k \equiv2 \pmod 3$.
Suppose hence that $k = 3j+2$, so we had $3j+3$ students.
Remember that to reach this point, we had to assume $k+1>3$, so necessarily $j>0$.

We're now reduced to $j+1$ students and the $n$-th student is now the $(j+1)$-th.
The pattern once again beings with Goose-Goose-Duck, so this goes on exactly like before:
either $j+1\leqslant 3$ and the $n$-th students survives from others begin tagged Goose before him; or else we must have $j\equiv 2 \pmod 3$ in order for $n$ to avoid exclusion.
Of course, this pattern will repeat.

Our possible $n$ hence are


*

*$1$ for $k=0$

*$4,7$ for $k=1,2$ with $k+1\leqslant 3$

*$16, 25$ for $j=1,2$ with $j+1 \leqslant 3$


and if we continue in this fashion we'll now have to add $27=3^3$ to $25$ obtaining $n=52$, and then $52+27 = 79$.
Proceeding we'll have $79 + 3^4 = 160$ and then $160 + 81 = 241$, and so on and so forth.

A compact way to consider the possible values of $n$ is hence to consider the infinite sum
\begin{align}
\begin{array}{}&1&+&3&+&3&+&9&+&9&+&27&+&27&+&81&+&81&+&\dots\\
=&3^0&+&3^1&+&3^1&+&3^2&+&3^2&+&3^3&+&3^3&+&3^4&+&3^4&+&\dots\end{array}
\end{align}
Any $n$ can be obtained from this infinite sum by truncating it.
This does suggest a perhaps more formal representation in terms of ternary base representation:
$$n = \sum_{i=0}^k\,a_i\,3^i,$$
where $k\geqslant 0$ and $a_0 = 1$, $a_j = 2$ for all $0<j<k$ and $a_k \in \{1,2\}$.
A: We write the numbers from $0$ to $N:=n-1$ in basis three. The first number is 00...000.
Let $k$ be the number of digits of $n-1$.
Let D stay for "duck" and G for "go". The last, $n$.th student wins then with one of the following last words of the teacher:
either D = GGD = DGGD,  or DG = DGGDG  .
Then for a fixed $k$ there is only a corresponding winning $N$ which wins as D, respectively DG namely
22...220 respectively
12...220

(with $k$ digits).
Proof. 
We call a step, a round the elimination procedure for each group of students, remained among them, labelled from one to $n$, and we pass to the new step, when the label drops.
We assume the student $n$ wins, getting a necessary condition to be verified.
After the first round in the circle there survive the first student, labeled 00...000, and all those of the shape *0, so the $n$.th student is in this shape. Now there are only the students *0 in the list as the teacher pronounces the "duck" on the last student in the "first round", he has now the "remainder" two, pronounces goose-goose, the first student remained in the second round is 00...020, and thus only the *20 students survive. So the $n$.th student is such a student, becomes D again, so the first D in the next round is 00...0220, so there survive all *220 students. This goes on, round for round, there remain in the competition only
*0      after 1.st round
*20     after 2.nd round
*220    after 3.rd round
*2220   after 4.th round
*22220  after 5.th round

and so on, of course each time if there were so many rounds.
Now it remains to analyze what happens when the first digit in base three becomes relevant. So we know that $N$ is of the shape 
122...220 or
222...220

and see that both cases are winning for the $n$.th student. (As DG, respectively DGGD = D = DGG.) This check also clears the converse implication. (We were assuming all the time that $n$ wins.)
Adding $3=$10${}_3$ to the above numbers we get 200...000 and 1000...000. 
We have thus $N=2\cdot 3^{k-1}-3$ or $N=3\cdot 3^{k-1}-3$. Adding one, we have $n=2\cdot 3^{k-1}-2$ or $n=3^k-2$.

Computer check, here sage, slow code:
def winner(n):
    L = [1..n]
    while len(L) > 3:
       L = L[3:] + [L[0]]
    return L[0]

for n in [2..3^7]:
    if n == winner(n):
       print "Solution n = %4s with digits %s" % (n, n.digits(base=3))

Results (with digits coming in reverse order):
Solution n =    4 with digits [1, 1]
Solution n =    7 with digits [1, 2]
Solution n =   16 with digits [1, 2, 1]
Solution n =   25 with digits [1, 2, 2]
Solution n =   52 with digits [1, 2, 2, 1]
Solution n =   79 with digits [1, 2, 2, 2]
Solution n =  160 with digits [1, 2, 2, 2, 1]
Solution n =  241 with digits [1, 2, 2, 2, 2]
Solution n =  484 with digits [1, 2, 2, 2, 2, 1]
Solution n =  727 with digits [1, 2, 2, 2, 2, 2]
Solution n = 1456 with digits [1, 2, 2, 2, 2, 2, 1]
Solution n = 2185 with digits [1, 2, 2, 2, 2, 2, 2]

