Problem: For each even positive integer $x$ , let $g(x)$ denote the greatest power of 2 that divides $x.$ For example, $g(20)=4$ and $g(16)=16.$ For each positive integer $n,$ let $S_n=\sum_{k=1}^{2^{n-1}}g(2k).$ Find the greatest integer $n$ less than 1000 such that $S_n$ is a perfect square

Solution: At first, this problem looks kind of daunting, but we can easily solve this problem by finding patterns, recursion and algebraic manipulations.
We first simplify all the messy notation in the $S_n$ term. Note that the problem asks us to find the smallest value of $n<1000$ such that there exists an integer $k$ that satisfies
\[k^2 = g(2) + g(4) + \cdots + g(2^n)\]
Since there is no obvious way to approach this problem, we start by experimenting with small values of $n$ to evaluate some $S_n$
We play with these values:
\[S_1 = g(2) = 2\]
\[S_2 = g(2) + g(4) = 2+4 = 6\]
\[S_3 = g(2) + g(4) + g(6) + g(8) = 16\]
\[S_4 =  g(2) + g(4) + g(6) + g(8) + g(10) +g(12)+g(14)+g(16) = 40\]
We are certainly not going to expand all of this out... so let's look for patterns from these $4$ values!
Using a little bit of ingenuity, we note that
\[S_2 = 2+4 = S_1 + 4\]
\[S_3 = 2+4+2+8 = 8+8 = S_2 + S_1 + 8\]
\[S_4 = 2+4+2+8+2+4+2+16 = S_3 + S_2 + S_1 + 16\]
Aha! We see powers of two in each of our terms! Therefore, we can say that
\[S_2 = S_1 + 2^2\]
\[S_3 = S_2+S_1 + 2^3\]
\[S_4 = S_3 + S_2 + S_1 + 2^4\]
Hooray! We have a recursion! Realistically, we would want to prove that the recursion works, but I currently don't know how to prove it.
On the actual AIME, go with whatever patterns you see, because most likely those are the patterns that the test-makers want the students to see.
So we may generalize a formula for $S_n$
\[S_n = 2^n + S_{n-1} + S_{n-2} + \cdots + S_2 + S_1\]
Uh oh... this formula is not in closed form. Looks like we'll have to use our recursion to develop one manually. We do so by using our recursion for $S_{n-1}$
\[S_n = 2^n + (2^{n-1} + S_{n-2} + S_{n-3} + \cdots + S_2 + S_1) + S_{n-2} + S_{n-3} + \cdots + S_2 + S_1\]
\[S_n = 2^n + 2^{n-1} + 2 (S_{n-2} + S_{n-3} + \cdots + S_2 + S_1\]
Let's simplify a bit further, where we use our recursion for $S_{n-2}$
\[S_n = 2^n + 2^{n-1} +(2S_{n-2}) + 2(S_{n-3} + S_{n-4} + \cdots + S_2 + S_1)\]
\[S_n = 2^n + 2^{n-1} + 2(2^{n-2}) + 2(S_{n-3} + S_{n-4} + \cdots + S_2 + S_1) + 2(S_{n-3} + S_{n-4} + \cdots + S_2 + S_1)\]
\[S_n = 2^n + 2^{n-1} + 2^{n-1} + 4(S_{n-3} + S_{n-4} + \cdots + S_2 + S_1)\]
We now see a pattern! Using the exact same logic, we can condense this whole messy thing into a closed form:
\[S_n = 2^n + \underbrace{2^{n-1}}_{n-2} + 2^{n-2}(S_1)\]
\[S_n = 2^n + 2^{n-1}(n-2) + 2^{n-1}\]
\[S_n = 2^n + 2^{n-1}(n-1)\]
\[S_n = 2^{n-1}(2 + (n-1)\]
\[S_n = 2^{n-1}(n+1)\]
We have our closed form, so now we can find the largest value of $n$ such that $S_n$ is a perfect square.
In order for $S_n$ to be a perfect square, we must have $n-1$ even and $n+1$ be a perfect square.
Since $n<1000$ , we have $n+1 < 1001$ . We first try $n+1 = 31^2 = 961$ (since it is the smallest square below $1000$ , which gives us $n=960$ . But $n-1$ isn't even, so we discard this value.
Next, we try the second smallest value, which is $n = 30^2 = 900$ , which tells us that $n=899$ $n-1$ is indeed even, and $n+1$ is a perfect square, so the largest value of $n$ such that $S_n$ is a perfect square is $899$
Our answer is $\boxed{899}$

Answer:  899