The periodic table gets its shape from the arrangement of elements into blocks:
An element is assigned its block based on what type of orbital holds its valence electron(s). For instance, the sole valence electron of neutral hydrogen occupies the 1s orbital in the 1s subshell, and so hydrogen belongs to the s-block.
The least energetic subshell is the 1s subshell, followed by 2s, 2p, 3s, etc. Electrons fill orbitals in less-energetic subshells first, in accordance with the following chart:
The diagonal arrows give the diagonal rule its name.
When finding the block of an element, there are some important facts to consider:
Each subsequent element has one more electron than its predecessor.
Every new type of subshell (s, p, d . . . ) can hold four more electrons than its predecessor as a consequence of the presence of two additional orbitals.
All subshells (1s, 2s . . . ) have an odd number of orbitals, each of which can hold two electrons. As such, all subshells can hold twice an odd number of electrons.
Subshells beyond s, p, d and f have names reserved for their eventual observation: after f comes the rest of the alphabet, starting with g and omitting j.
The Challenge
Given the atomic number of an element, output its block (a letter).
Because one can tell how many electrons are held by theorized, not-yet-observed subshells, input will be less than or equal to 17296 (found with the sum \2ドル\sum_{n=1}^{23}\sum_{\ell=0}^{n-1}2\left(2\ell+1\right)\$), the atomic number of the last element with valence electrons in the 46s subshell. Letters beyond z would be needed for elements past that point, and so they will not be included in the challenge. An extended version of the diagonal line chart above can be seen here.
Rules
This is code-golf so the shortest answer in bytes in each language wins
For the purpose of this challenge, when s and p are reached a second time after f, they will be capitalized. This means letters are sequenced as follows:
s p d f g h i k l m n o P q r S t u v w x y z. Additionally, it means output is case-sensitive.Assume only valid input is given.
There is a discrepancy between the periodic table above and the expected output: in reality, lanthanum and actinium (57 and 89) are in the d-block, and lutetium and lawrencium (71 and 103) are in the f-block. The diagonal rule implies that lanthanum and actinium are in the f-block, and that lutetium and lawrencium are in the d-block, and so that is what the program should state.
Test Cases
In: 1
Out: s
In: 5
Out: p
In: 21
Out: d
In: 57
Out: f
In: 2784
Out: l
In: 17296
Out: s
In: 15181
Out: z
In: 12024
Out: q
3 Answers 3
Charcoal, 59 bytes
Nθ⊞υ2W›θ⊗Συ«≧−⊗Συθ⊞υ+4⌈υ»§">-◧βYTB↷p{ï⭆Σ]¬∕↷›"LΦυ‹%⊖θΣυΣ✂υκ
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input the atomic number.
⊞υ2
Start with just an s-shell.
W›θ⊗Συ«
Repeat while the number of electrons left will fill all the shells twice...
≧−⊗Συθ
... remove those electrons, and...
⊞υ+4⌈υ
... add another shell which can hold 4 more electrons than the previous.
»§">-◧βYTB↷p{ï⭆Σ]¬∕↷›"LΦυ‹%⊖θΣυΣ✂υκ
Count how many of the remaining shells have no electrons and look that up in the compressed string of block letters. (Because of the way Sum works in Charcoal, the calculation is adjusted slightly to avoid a sum of no elements, which ends up offsetting the result by 1, which is compensated by moving the z to the beginning of the string where it can be cyclically indexed.)
-
\$\begingroup\$ @KevinCruijssen No, because that breaks for equal values. But it turns out I had to do that anyway because the previous code was buggy and I had to subtract 1 to fix that (costing a byte) but then fortunately I do now want
Lessinside the filter, thus balancing the bug fix. \$\endgroup\$Neil– Neil2020年07月13日 10:52:38 +00:00Commented Jul 13, 2020 at 10:52 -
\$\begingroup\$ Yeah, I deleted my comment when I noticed that. Btw, you forgot to update your TIO after your fix just yet. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年07月13日 10:54:08 +00:00Commented Jul 13, 2020 at 10:54
-
1\$\begingroup\$ @KevinCruijssen Yes, I noticed when I went to see whether it affected
17296or2784. BTW that fix was for12, which was resulting inpinstead ofs. \$\endgroup\$Neil– Neil2020年07月13日 10:56:55 +00:00Commented Jul 13, 2020 at 10:56 -
\$\begingroup\$ Hmm,
12024now results inqinstead ofP. Again, not sure if the test cases are wrong or our implementations.. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年07月13日 11:07:45 +00:00Commented Jul 13, 2020 at 11:07
J, 64 bytes
-4 for 12024 being q
('spdfghiklmnoPqrStuvwxyz'{~]{~[I.~[:+/2円+4*])&(;2#</.99|i.2#99)
How it works
99|i.2#99
Create a 99x99 matrix where each row counts up: 0 1 2 3 ...
;2#</.
Take each diagonal 0;1 0;2 1 0 two times: 0 0 1 0 1 0 2 1 0 2 1 0 ....
[:+/2円+4*]
Multiply each number by 4, add 2, add each number left to it: 2 4 10 12 18 20 30 ...
[I.~
Use this as range boundaries to find the index for the input, e.g. 13.
]{~
Get the corresponding shell at that index, e.g. 1.
'spdfghiklmnoPqrStuvwxyz'{~
Retrieve the shell name.
-
\$\begingroup\$ make it 60 \$\endgroup\$FrownyFrog– FrownyFrog2020年07月15日 19:55:24 +00:00Commented Jul 15, 2020 at 19:55
-
\$\begingroup\$ 58 \$\endgroup\$FrownyFrog– FrownyFrog2020年07月15日 23:05:07 +00:00Commented Jul 15, 2020 at 23:05
05AB1E, 46 bytes
[N4*̈ ̄O·-Ð1‹#U}A3.$„sp©Du‡®ì„ejмÁ ̄.sOX< ̄O%›Oè
Port of @Neil's Charcoal answer, so make sure to upvote him as well!
Try it online or verify all test cases.
Explanation:
[ # Start an infinite loop:
N4* # Multiply the (0-based) loop-index by 4
Ì # Increase it by 2
ˆ # Pop and add this value to the global array
̄O # Get the sum of the global array
· # Double this sum
- # Subtract it from the current integer,
# which will be the (implicit) input in the first iteration
Ð # Triplicate this new value
1‹ # If it's 0 or negative:
# # Stop the infinite loop
U # Pop and store the triplicated value in variable `X`
# (which is 1 by default, which doesn't matter for inputs ≤ 4)
}A # After the infinite loop: push the lowercase alphabet
3.$ # Remove the leading "abc"
„sp # Push string "sp"
© # Store it in variable `®` (without popping)
D # Duplicate "sp"
u # Uppercase the copy: "SP"
‡ # Transliterate "s" to "S" and "p" to "P"
®ì # Prepend "sp" from variable `®`
„ejм # Remove "e" and "j"
Á # Rotate it once towards the right
# (we now have the string "zspdfghiklmnoPqrStuvwxy")
̄.s # Get a list of suffices of the global array
O # Sum each inner suffix
X< # Push value `X`, and decrease it by 1
̄O% # Modulo it by the sum of the global array
› # Check for each prefix-sum if it's larger than this value
# (1 if truthy; 0 if falsey)
O # Get the amount of truthy values by summing
è # And use it to (0-based) index into the string
# (after which the resulting character is output implicitly)
2784bel? \$\endgroup\$12024isqlike you pointed out elsewhere \$\endgroup\$