From were the formula 2n square to determine the number of electrons derived?

Actually it is quite simple. If you are familier with the basics of the quantum theory you can see that the number of orbitals for any given principle quantum number is n^2. Since one orbital can carry a maximum of 2 electrons, the total number of electrons for a principle quantum number is 2 x n^2 = 2n^2

If you are unfamilier with quantum theory. It can be simplified like so - we know that electrons are present in three dimensional energy levels (orbitals). Each of these energy levels have may many sub orbitals. for example for the 1st energy level the number of sub orbitals is n^2 (n square) that is 1 x 1 = 1. then the total number of electrons for this energy level is 2n^2 = 2 x 1^2 = 2.