Is there a good mathematical explanation for why orbital lengths in the periodic table are perfect squares doubled? $\DeclareMathOperator\SO{SO}\newcommand{\R}{\mathbb{R}}\newcommand{\S}{\mathbb{S}}$The periodic table of elements has row lengths $2, 8, 8, 18, 18, 32, \ldots $, i.e., perfect squares doubled. The group theoretic explanation for this that I know (forgive me if it is an oversimplification) is that the state space of the hydrogen atom is made of functions on $\R^3$, which we can decompose as functions on $\S^2$ times functions on $\R^+$. Then $\SO(3)$ acts on the functions on $\S^2$ and commutes with the action of the Hamiltonian, so we can find pure states inside irreducible representations of $\SO(3)$. The orbital lengths depend on how these representations line up by energy, which is a function on $\R^+$. It happens that the spaces with the same energy have the form $(V_0 \oplus V_2 \oplus V_4 \oplus \cdots ) \otimes W$, where $V_i$ is the irreducible representation of $\SO(3)$ of dimension $i+1$, and $W$ is a $2$-dimensional space that represents the spin. So the orbital length is the dimension of $V_0 \oplus V_2 \oplus V_4 \oplus \cdots$ is the sum of the first $k$ odd numbers, which is a perfect square, and you double it because of the spin.
So, in short, the perfect squares arise as the sums of the first $k$ odd numbers, and the invariant subspaces arrange themselves into energy levels that way because... well, here I get stuck. Factoring out the spin, which explains the doubling, can anyone suggest a more conceptual (symmetry-based?) explanation for why perfect squares arise here?
 A: 
So, in short, the perfect squares arise as the sums of the first $k$
odd numbers, and the invariant subspaces arrange themselves into
energy levels that way because... well, here I get stuck.

To get "unstuck", the following consideration may help:
The key property to consider is the number $N$ of nodes of the electronic wave function. Wave functions with the same number of nodes have approximately the same energy. We say that states with the same $N$ form a "shell". (The integer $n=N+1$ is called the principal quantum number.)
The number of states ("orbitals") in a shell now follows by counting the number $\sum_{l=0}^{N}(2l+1)=
(N+1)^2$ of eigenfunctions of the angular momentum operator with at most $N$ nodes – "at most" because the radial wave function can provide the remaining nodes. Including spin the number of states in a shell is then $2n^2$.

So the $2n^2$ rule applies to shells, labeled by the principal quantum number $n$. The statement that "states with the same $n$ have the same energy" is only an approximation, which is why the rows of the periodic table do not strictly follow the $2n^2$ rule. For example, the $n=3$ row has only 8 elements, not 18, because the $n=4,l=0$ state has lower energy than the $n=3,l=2$ states.
More accurate considerations, see Theoretical justification of Madelung's rule, show that the energy is an increasing function of the number
$$W=n+l-\frac{l}{l+1}.$$
The physics here is that the $n$-dependence of the energy accounts for the attraction of electrons to the core, while the $l$-dependence accounts for their mutual repulsion. In atomic hydrogen, which has a single electron, the energy is only dependent on $n$, without any $l$-dependence (at least if we neglect relativistic effects).
If we approximate $W\approx n+l$ (the socalled "$n+l$ rule") we obtain the length $L_n$ of the $n$-th row in the periodic table as
$$L_n=2\sum_{l=0}^{\text{Int}\,[n/2]}(2l+1)=2\left(1+\text{Int}\,[n/2]\right)^2$$
$$\qquad=2, 8, 8, 18, 18, 32,32,\;\;\text{for}\;\;n=1,2\ldots 7.$$
So this explains why the initial "2" appears only once and the subsequent numbers appear twice (Dan Romik's question).

Bottom line:
Q: "Is there a good mathematical explanation for why orbital lengths in the periodic table are perfect squares doubled?"A: I don't think so, Madelung's $n+l$ rule requires explicit consideration of the electrostatics of the problem.
However, there have been symmetry based attempts to obtain that rule, as described in 
Ordinal explanation of the periodic system of chemical elements. and in Some evidence about the dynamical group SO(4,2) symmetries of the periodic table of elements.
A: In this answer, I'm going to crib from this presentation by @JohnBaez and the paper On the Regularization of the Kepler Problem. Milnor's paper includes a lot of the same information.
First, I'm going to state a few facts without proof. One can compose the stereographic projection of $\mathbb{R}^3$ to $S^3$ with the symplectomorphism swapping $p$s and $q$s on $T^*(\mathbb{R}^3)$ to get a symplectomorphism from the punctured $T^*(S^3)$ to $T^*(\mathbb{R}^3)$.
Furthermore, one can show that the Hamiltonian flow of $p^2$ on a a constant energy surface in $T^*(S^3)$ maps to the Hamiltonian flow of the Kepler potential on $T^*(\mathbb{R}^3)$. This maps the constant energy classical mechanics of a negative energy state in the Kepler potential to a free particle on $S^3$ with fixed energy. This also exhibits the $SO(4)$ symmetry as rotations on $S^3$. Thus (and I'm still undecided if there's some handwaving here), the energy eigenstates in the quantum theory should be irreps of $SO(4)$. You can also exhibit the $SO(4)$ symmetry directly in the quantum theory, so any handwaving isn't a problem.
To see what the representations are, the $SO(4)$ action on $S^3$ can be exhibited by the two $SU(2)$ factors in $\operatorname{Spin}(4)$ acting on both sides of $S^3 \cong SU(2)$. The element $(-I,-I) \in SU(2) \times SU(2)$ acts trivially, so you get an $SO(4)$ action.
With this, we can decompose a la the Peter–Weyl theorem:
$$
L^2(S^3) \cong \bigoplus_i \rho_i \otimes \rho_i^\star
$$
Each $\rho_i$ is an irrep of $SU(2)$, and those irreps can be labelled by an integer $n$. Thus, the problem decomposes into $n^2$-dimensional irreps of $SO(4)$, which explains the question asked.
[N.B. -- I'd be interested in understanding if this can be done "all at once" as opposed to working with constant energy surfaces and arguing by scaling as I see in the references. If I have time, I'd also want to show that the different irreps of SO(4) have different energies, or maybe I'm missing something obvious. This can all be done by looking at the symmetry explicitly in the quantum theory, I'm sure, but it would be nice to see it geometrically.]
