Today will be as actionpacked as last time was leisurely. We're going to prove just what numbers are in $S_2$!
We will begin by using an identity from last time in a surprising way to talk about how many ways we can write some numbers as a sum of squares.
Sidebar: We will connect sums of squares to factorization. Remember that the BrahmaguptaFibonacci identity says that if two numbers are in $S_2$, so is their product. Remarkably, we can sort of do this backwards: $$\text{If an odd number }N\in S_2\text{ in two different (positive) ways, then }N=yz\text{ where }y,z>1\text{ and }y,z\in S_2\; .$$ That is, not a situation like $$13=3^2+2^2=2^2+3^2$$ but where $$25=5^2+0^2=3^2+4^2\; .$$ Assuming that $$N = a^2+b^2 = c^2+d^2\text{ with }a,c\text{ odd and }b,d\text{ even,}$$then let $$k=gcd(ac,db)\text{ and }n=gcd(a+c,d+b)\text{ (both are even)}$$ and $$\ell=\frac{ac}{k}=\frac{d+b}{n}\text{ and }m=\frac{a+c}{n}=\frac{db}{k}$$ and we get that $$N=\left[\left(\frac{k}{2}\right)^2+\left(\frac{n}{2}\right)^2\right]\left(m^2+\ell^2\right)$$ There are some details here, especially in terms of verifying all these numbers exist, but they are mostly just the definition of gcd and parity.
So for instance for $N=25$, $k=gcd(2,4)=2$, $n=gcd(8,4)=4$ which means $\ell=1$ and $m=2$, yielding $$25=\left[\left(\frac{2}{2}\right)^2+\left(\frac{4}{2}\right)^2\right]\left(1^2+2^2\right)=5\cdot 5$$ and so $25$ is a product of numbers themselves writeable as a sum of two squares.
Fact: It is now trivial to prove that a prime is writeable in zero or one (positive) way as a sum of two squares.
Proof: This is clear for $p=2$, and we proved it above if $p\equiv 3\text{ mod }(4)$. If $p\equiv 1\text{ mod }(4)$, then if $p$ is writeable in two different ways, it factors. But prime numbers don't factor nontrivially. So there is just one way to do it.
We can see this visually, in that each of circles with radius square root a prime either has no lattice points, or has only positive lattice points that are $(a,b)$ and $(b,a)$ for one $a$ and $b$.
Click to the left again to hide and once more to show the dynamic interactive window 
Next, we'll start out with our formal investigation of what numbers are in $S_2$ by taking a look at a lemma seemingly unrelated to writing things as sums of squares.
Recall: We say that a number $a$ has a square root modulo $n$ if there is some number $x$ with $$x^2\equiv a\text{ mod }(n)\; .$$
We discussed this a fair amount, such as finding square roots of $\pm 1$ earlier. Here is a fact we mentioned before.
Fact: For an odd prime $p$, then the only way there is a square root of $1$ modulo $p$ is if $p\equiv 1\text{ mod }(4)$.
Remember, this means there can't be one if $p\equiv 3\text{ mod }(4)$, and that there might be one if $p\equiv 1\text{ mod }(4)$.
This should be at least vaguely plausible as being connected to sums of squares, because last time we saw that square roots of negative one in $\mathbb{Z}$ (not $\mathbb{Z}_n$) were connected to sums of squares.
There are a lot of ways to prove this fact, but the easiest is to use our knowledge of groups. If $$u^2\equiv 1\text{ mod }(p)$$ then then order of $u$ in $U_p$ is four, since $$u^4=(u^2)^2\equiv (1)^2=1\; .$$ We know that the order $$\mid U_p \mid =p1$$ but then Lagrange's Theorem (the group theory one) says that four divides $p1$. So the only possible such prime $p$ is one that is of the form $4k+1$. (We used the fact that $p$ is odd because then $1\not\equiv +1$.)
Now we get to our lemma.
Lemma: If $p\equiv 1\text{ mod }(4)$, then there actually does exist a square root of $1$ modulo $p$.
That is, there is a $u$ such that $$u^2\equiv 1\text{ mod }(p)\; .$$
Proof: This is something we could have done immediately after Wilson's Theorem, so we'll start with the proof of that.
Recall that in Wilson's Theorem, we showed that $(p1)!\equiv 1$ mod ($p$) by pairing up all the numbers from $2$ to $p2$ in pairs of multiplicative inverses mod ($p$), thus: $$(p1)!=1\cdot 2\cdot 2^{1}\cdot 3\cdot 3^{1}\cdots (p1) \equiv (p1)\equiv 1\text{ mod (}p)\, .$$ Now, assuming that Wilson's Theorem is true, we will pair up the numbers from $1$ to $p1$ in a different way, in pairs of additive inverses mod ($p$): $$(p1)!=1\cdot (p1)\cdot 2\cdot (p2)\cdot 3\cdot (p3)\cdots \frac{p1}{2}\cdot\frac{p+1}{2}=\left[1\cdot 2\cdot 3\cdots \frac{p1}{2}\right]\cdot \left[(p1)\cdot (p2)\cdots \frac{p+1}{2}\right]\, .$$ This makes sense because $(p1)/2$ is an integer halfway between $1$ and $p$, as $p$ is odd.
But if we rethink things mod ($p$), we can rewrite this in a more suggestive way. Let $\left(1\cdot 2\cdot 3\cdots \frac{p1}{2}\right)$ be called $f$ (this is also $\left(\frac{p1}{2}\right)!$, of course): $$\left[1\cdot 2\cdot 3\cdots \frac{p1}{2}\right]\cdot \left[(p1)\cdot (p2)\cdots \frac{p+1}{2}\right]\equiv f \cdot \left[1\cdot 2\cdot 3\cdot \frac{p1}{2}\right]$$ $$\equiv f\cdot (1)^{\frac{p1}{2}}\left[1\cdot 2\cdot 3\cdots \frac{p1}{2}\right]\equiv (1)^{\frac{p1}{2}}f^2\, .$$ Now, when $p\equiv 1$ mod (4), then $p=4k+1$ so $\frac{p1}{2}=2k$ is even. That means: $$1\equiv f^2\text{ or }f^2\equiv 1\text{ mod }(p)$$ so in fact there precisely two square root of negative one (as Lagrange's Theorem suggests), $\pm \left(\frac{p1}{2}\right)!$  somehow a satisfying answer.
We can check that these really are square roots of $1$.
Click to the left again to hide and once more to show the dynamic interactive window 
We will use this lemma to prove our conjecture from last time  that every odd prime of the form $p=4k+1$ can be represented as the sum of two squares! Then we'll combine it with the observation about primes of the form $p=4k+3$ to see exactly which numbers can be thus represented.
First, let's look at the following diagram.
Click to the left again to hide and once more to show the dynamic interactive window 
As you can see, I am plotting certain points on the circle $x^2+y^2=n$, with $n=5$ to begin. I have done some 'magic' to turn the square root of $1\text{ mod }(n)$ into these points. Before telling you the magic, let's get ready to see it.
How are we defining the points?
Proof: Now we want to prove that every prime of this form can be written as a sum of squares. Here is the strategy.
For instance, with $p=5$, we have that $k=\left(\frac{51}{2}\right)!=2!=2$, so we need to find a point $(a,2a+5b)$ such that $a^2+(2a+5b)^2<2p$. Guess and check with $a=1$ and $b=0$ gives us $$N(1,2\cdot 1 +5\cdot 0)=1^2+(2\cdot 1+5\cdot 0)^2=5<2\cdot 5=10$$ so this point should work, and this does give the correct statement that $$5=1^2+2^2\; .$$
So what remains to be shown is that there actually IS such a blue dot.
Click to the left again to hide and once more to show the dynamic interactive window 
We need to prove there is a blue dot (somewhere) that is not at the origin but also has norm smaller than $2p$ (remember the inequality above). The bigger circle is the one we care about now  it has formula $x^2+y^2=2p$, so radius $\sqrt{2p}$. If we find a blue point inside that circle but not at the origin, then the above argument proves it must be on the smaller circle.
Key idea: Area!
It will turn out that the best way to do this is by considering the areas of the various circles, and showing that they are so big you just must have a blue point in it (but not at the origin).
The area of the bigger circle, which has radius $\sqrt{2p}$, is $\pi (\sqrt{2p})^2=2\pi p$. Since $\pi >2$, we have that $2\pi>2(2)=4$, which mean that the area of the bigger circle is bigger than $4p$.
Click to the left again to hide and once more to show the dynamic interactive window 
What we do now is to create a sublattice of the blue dots. Take all blue dots, and just double their coordinates; this will give you the green dots above. (Naturally, underneath each green dot is a blue dot.) Now we take a look at the triangles made by the green dots.
This proof is very visual, so before we move on, make sure you believe all of this. Then we will analyze the exact areas involved more closely to finish. Remember, we are trying to prove that there is a blue point inside the bigger blue circle, but away from the origin.
Click to the left again to hide and once more to show the dynamic interactive window 
Let's look at the picture again.
That's the proof! Hooray! Whew!
Here is a picture of how to find the blue point in the circle. The black points are $v$, $w$, and $w$, and you see the midpoint of the line is indeed blue.

There are lots of other points of view of this theorem, which indicates its importance.
Unfortunately, Sage does not have this implemented in a userfriendly format. This is mostly because the naive things one might do are tricky to get to work nicely with the notsonaive things professional number theorists need factorization in general algebraic structures coming from the complex numbers to do.
Traceback (click to the left of this block for traceback) ... TypeError: unsupported operand parent(s) for 'xgcd': 'Integer Ring' and 'Symbolic Ring' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/karldietercrisman/.sage/sage_notebook/worksheets/admin/69/code/5.py", line 7, in <module> xgcd(_sage_const_2 ,_sage_const_3 +_sage_const_3 *i) File "/Users/karldietercrisman/Desktop/sage3.4/local/lib/python2.5/sitepackages/SQLAlchemy0.4.6py2.5.egg/", line 1, in <module> File "/Users/karldietercrisman/Desktop/sage3.4/local/lib/python2.5/sitepackages/sage/rings/arith.py", line 1434, in xgcd return a.xgcd(b) File "element.pyx", line 1951, in sage.structure.element.PrincipalIdealDomainElement.xgcd (sage/structure/element.c:11756) File "coerce.pyx", line 740, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:5848) TypeError: unsupported operand parent(s) for 'xgcd': 'Integer Ring' and 'Symbolic Ring' 
There is one loose end. Namely, why is it that there are some composite numbers of the form $4k+1$ which are not in $S_2$  and which even numbers are in $S_2$?
As a result, we have proved a Theorem:
$N$ is in $S_2$ precisely if it has only even powers (including zeroth powers) of any primes of the form $4k+3$.
3 * 7 Traceback (click to the left of this block for traceback) ... ValueError: 21 is not a sum of two squares 3 * 7 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_35.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# * coding: utf8 *\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZmFjdG9yKDIxKTt0d29fc3F1YXJlcygyMSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/private/var/folders/Yy/YytEJm5VEB0+pBRD7JNLe++++TQ/Tmp/tmpizm9tE/___code___.py", line 3, in <module> exec compile(u'factor(_sage_const_21 );two_squares(_sage_const_21 ) File "", line 1, in <module> File "/Applications/MathApps/sage/local/lib/python2.6/sitepackages/sage/rings/arith.py", line 4350, in two_squares raise ValueError, "%s is not a sum of two squares"%n ValueError: 21 is not a sum of two squares 
The homework, which is last time's plus some new ones:
