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.
You can determine how many valence electrons an atom has by what family the element of the atom is in. For instance, if the element is in family 8A, the number of valence electrons will be 8. Or, if the element is in family 2A, the number of valence electrons for the atom will be 2. So, whatever number family the atom is in, the number of valence electrons equals that.