Revision 0300c742-3b47-4bad-adcd-bc058eb5fde0 - Code Golf Stack Exchange
# CJam, <s>87</s> <s>83</s> <s>82</s> 80 bytes
0"hTH^Løìð^R¼Ä^X¿håêÛ¾ªÔ^G^Æ^Y^Ƽ^Q"256b6b5a/4a8**li<{[33:B_B*65_B*1]=\_B%8=B\#*+}/Bb0-`
The above code uses caret notation for the control characters in the string. It is equivalent to the following code, which uses an array of character codes instead:
0[104 84 72C248 236 240I188 196 24 191 104 229 234 219 190 170 212 7 134 25 134 188H]
256b6b5a/4a8**li<{[33:B_B*65_B*1]=\_B%8=B\#*+}/Bb0-`
You can try the second version in the [CJam interpreter](http://cjam.aditsu.net/).
### Background
To obtain the electron configuration of the *Nth* atom, we start with an atom without electrons and apply *N* transformations to it.
To reduce the byte count of the implementation, we represent the electron configuration of an atom as an integer. Each digit of that integer in base 33 corresponds to the number of electrons in a certain shells; the least significant digit represents the outer shell.
For example, the electron configuration of Molybdenum (42) is **[2 8 18 13 1]**. This corresponds to the integer **2 × 33<sup>4</sup> + 8 × 33<sup>3</sup> + 18 × 33<sup>2</sup> + 13 × 33 + 1 = 26,79,370**.
Palladium (48) is a special case, which we treat as **[2 8 18 18 0]** instead of **[2 8 18 18]**.
This convenient representation reduces the aforementioned transformations to simple arithmetic:
* `R += 1` (add an electron to the outer shell)
* `R += 33` (add an electron to the second outer shell)
* `R += 65` (add two electrons to the second outer shell; remove one from the first)
* `R += 1089` (add an electron to the third outer shell)
* `R += 2145` (add two electrons to the third outer shell; remove one from the second)
* `R *= 33, R += 1` (add a new shell containing a single electron)
All that's left is to somehow encode which transformation has to be applied to pass from a particular atom to the next. The differences of the integer representations of two consecutive atoms are as follows:
[1 1 65 1 1 1 1 1 1 1 2369 1 1 1 1 1 1 1 78401 1 33 33 33 65 1 33 33 33 65 1 1 1 1 1 1 1 2598017 1 33 33 65 33 1 65 33 65 1 1 1 1 1 1 1 1 85745345 1 33 1089 2145 1089 1089 1089 1089 33 2145 1089 1089 1089 1089 1089 33 33 33 33 33 33 33 65 33 1 1 1 1 1 1 1 2830095041 1 33 33 2145 1089 1089 2145 1089 33 2145 1089 1089 1089 1089 1089 65 1 33 33 33 33 33 33 65 1 1 1 1 1 1 1]
The unique differences in this array are the following:
[1 33 65 1089 2145 2369 78401 2598017 85745345 2830095041]
All but the first 5 correspond to instances when a new shell is added; these can be omitted since multiplying by 33 and adding 1 yields the same result as adding the difference.
Since we have to add a new shell if and only if the current atom has exactly eight electrons in its outer shell (with the exception of **He (2) ↦ Li (3)**, which can be encoded as *add 65*), we can encode those transformations as *add 1* and determine the need for multiplication on the fly.
Thus, if we define
X := 0
I := 0
L := [33 1089 65 2145 1]
T := [1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 3 1 0 0 0 3 1 1 1 1 1 1 1 1 1 0 0 3 0 1 3 0 3 1 1 1 1 1 1 1 1 1 1 0 2 4 2 2 2 2 0 4 2 2 2 2 2 0 0 0 0 0 0 0 3 0 1 1 1 1 1 1 1 1 1 0 0 4 2 2 4 2 0 4 2 2 2 2 2 3 1 0 0 0 0 0 0 3 1 1 1 1 1 1 1]
the electron configuration of the *Nth* atom can be computed as follows:
while(X < N):
R *= (33 ** (R % 33 == 8))
R += L[T[X]]
X += 1
### How it works
" Push 0, the initial value of R; convert the following array into an integer by
considering it a base 256 number, then back to the array of its digits in base 6. ";
0[104 84 72C248 236 240I188 196 24 191 104 229 234 219 190 170 212 7 134 25 134 188H]
256b6b
" Replace each 5 in the resulting array by [4 4 4 4 4 4 4 4]. This yields the array T
from the above section. ";
5a/4a8**
" Read an integer N from STDIN and discard all but the first N elements of T. ";
li<
" For each element Y of the remainder of T, do the following: ";
{
[33:B_B*65_B*1]=\ " Define B := 33, push L, retrieve L[Y] and swap it with R. ";
_B%8=B\#* " Execute R *= 33 ** (R % 33 == 8). ";
+ " Execute R += L[Y]. ";
}/
" Convert R into the array of its digits in base 33, remove eventual zeros (Palladium)
and replace the resulting array with its string representation. ";
Bb0-`
### Example run
$ base64 -d > electrons.cjam <<< MCJoVEgM+OzwErzEGL9o5erbvqrUB4YZhrwRIjI1NmI2YjVhLzRhOCoqbGk8e1szMzpCX0IqNjVfQioxXT1cX0IlOD1CXCMqK30vQmIwLWA=
$ cksum electrons.cjam
3709391992 80 electrons.cjam
$ for i in {1..118}; do LANG=en_US cjam electrons.cjam <<< $i; echo; done
[1]
[2]
[2 1]
[2 2]
[2 3]
[2 4]
[2 5]
[2 6]
[2 7]
[2 8]
[2 8 1]
[2 8 2]
[2 8 3]
[2 8 4]
[2 8 5]
[2 8 6]
[2 8 7]
[2 8 8]
[2 8 8 1]
[2 8 8 2]
[2 8 9 2]
[2 8 10 2]
[2 8 11 2]
[2 8 13 1]
[2 8 13 2]
[2 8 14 2]
[2 8 15 2]
[2 8 16 2]
[2 8 18 1]
[2 8 18 2]
[2 8 18 3]
[2 8 18 4]
[2 8 18 5]
[2 8 18 6]
[2 8 18 7]
[2 8 18 8]
[2 8 18 8 1]
[2 8 18 8 2]
[2 8 18 9 2]
[2 8 18 10 2]
[2 8 18 12 1]
[2 8 18 13 1]
[2 8 18 13 2]
[2 8 18 15 1]
[2 8 18 16 1]
[2 8 18 18]
[2 8 18 18 1]
[2 8 18 18 2]
[2 8 18 18 3]
[2 8 18 18 4]
[2 8 18 18 5]
[2 8 18 18 6]
[2 8 18 18 7]
[2 8 18 18 8]
[2 8 18 18 8 1]
[2 8 18 18 8 2]
[2 8 18 18 9 2]
[2 8 18 19 9 2]
[2 8 18 21 8 2]
[2 8 18 22 8 2]
[2 8 18 23 8 2]
[2 8 18 24 8 2]
[2 8 18 25 8 2]
[2 8 18 25 9 2]
[2 8 18 27 8 2]
[2 8 18 28 8 2]
[2 8 18 29 8 2]
[2 8 18 30 8 2]
[2 8 18 31 8 2]
[2 8 18 32 8 2]
[2 8 18 32 9 2]
[2 8 18 32 10 2]
[2 8 18 32 11 2]
[2 8 18 32 12 2]
[2 8 18 32 13 2]
[2 8 18 32 14 2]
[2 8 18 32 15 2]
[2 8 18 32 17 1]
[2 8 18 32 18 1]
[2 8 18 32 18 2]
[2 8 18 32 18 3]
[2 8 18 32 18 4]
[2 8 18 32 18 5]
[2 8 18 32 18 6]
[2 8 18 32 18 7]
[2 8 18 32 18 8]
[2 8 18 32 18 8 1]
[2 8 18 32 18 8 2]
[2 8 18 32 18 9 2]
[2 8 18 32 18 10 2]
[2 8 18 32 20 9 2]
[2 8 18 32 21 9 2]
[2 8 18 32 22 9 2]
[2 8 18 32 24 8 2]
[2 8 18 32 25 8 2]
[2 8 18 32 25 9 2]
[2 8 18 32 27 8 2]
[2 8 18 32 28 8 2]
[2 8 18 32 29 8 2]
[2 8 18 32 30 8 2]
[2 8 18 32 31 8 2]
[2 8 18 32 32 8 2]
[2 8 18 32 32 10 1]
[2 8 18 32 32 10 2]
[2 8 18 32 32 11 2]
[2 8 18 32 32 12 2]
[2 8 18 32 32 13 2]
[2 8 18 32 32 14 2]
[2 8 18 32 32 15 2]
[2 8 18 32 32 16 2]
[2 8 18 32 32 18 1]
[2 8 18 32 32 18 2]
[2 8 18 32 32 18 3]
[2 8 18 32 32 18 4]
[2 8 18 32 32 18 5]
[2 8 18 32 32 18 6]
[2 8 18 32 32 18 7]
[2 8 18 32 32 18 8]