title: Mass of elements

text:
# Question
Title: Mass of elements
There are more than 100 elements in the modern periodic table. You challenge is to output the rounded mass of an element with respect to the input given.

# Input

* Input will contain an abbreviation of an element.
* Input can be taken from any one of the following:

+ `stdin`
    + command-line arguments
    + function arguments
* Input will contain a one or two letter abbreviation of an element. The first character is always an uppercase letter and the second character, if any, will be a lowercase letter.

# Output

* Output the mass of the element entered
* Output must be in the stdout or closest equivalent.
* Output may contain a trailing newline character

# Elements and their masses

Here is the list of abbreviation of elements and their masses:

```
Sl.No     Elements     Abbreviation     Mass

1         Hydrogen     H                1
2         Helium       He               4
3         Lithium      Li               7
4         Beryllium    Be               9
5         Boron        B                11
6         Carbon       C                12
7         Nitrogen     N                14
8         Oxygen       O                16
9         Fluorine     F                19
10        Neon         Ne               20
11        Sodium       Na               23
12        Magnesium    Mg               24
13        Aluminium    Al               27
14        Silicon      Si               28
15        Phosphorus   P                31
16        Sulfur       S                32
17        Chlorine     Cl               35
18        Argon        Ar               40
19        Potassium    K                39
20        Calcium      Ca               40
21        Scandium     Sc               45
22        Titanium     Ti               48
23        Vanadium     V                51
24        Chromium     Cr               52
25        Manganese    Mn               55
26        Iron         Fe               56
27        Cobalt       Co               59
28        Nickel       Ni               59
29        Copper       Cu               64
30        Zinc         Zn               65
31        Gallium      Ga               70
32        Germanium    Ge               73
33        Arsenic      As               75
34        Selenium     Se               79
35        Bromine      Br               80
36        Krypton      Kr               84
37        Rubidium     Rb               85
38        Strontium    Sr               88
39        Yttrium      Y                89
40        Zirconium    Zr               91
41        Niobium      Nb               93
42        Molybdenum   Mo               96
43        Technetium   Tc               98
44        Ruthenium    Ru               101
45        Rhodium      Rh               103
46        Palladium    Pd               106
47        Silver       Ag               108
48        Cadmium      Cd               112
49        Indium       In               115
50        Tin          Sn               119
51        Antimony     Sb               122
52        Tellurium    Te               128
53        Iodine       I                127
54        Xenon        Xe               131
55        Cesium       Cs               133
56        Barium       Ba               137
57        Lanthanum    La               139
58        Cerium       Ce               140
59        Praseodymium Pr               141
60        Neodymium    Nd               144
61        Promethium   Pm               145
62        Samarium     Sm               150
63        Europium     Eu               152
64        Gadolinium   Gd               157
65        Terbium      Tb               159
66        Dysprosium   Dy               163
67        Holmium      Ho               165
68        Erbium       Er               167
69        Thulium      Tm               169
70        Ytterbium    Yb               173
71        Lutetium     Lu               175
72        Hafnium      Hf               178
73        Tantalum     Ta               181
74        Tungsten     W                184
75        Rhenium      Re               186
76        Osmium       Os               190
77        Iridium      Ir               192
78        Platinum     Pt               195
79        Gold         Au               197
80        Mercury      Hg               201
81        Thallium     Tl               204
82        Lead         Pb               207
83        Bismuth      Bi               209
84        Polonium     Po               209
85        Astatine     At               210
86        Radon        Rn               222
87        Francium     Fr               223
88        Radium       Ra               226
89        Actinium     Ac               227
90        Thorium      Th               232
91        Protactinium Pa               231
92        Uranium      U                238
93        Neptunium    Np               237
94        Plutonium    Pu               244
95        Americium    Am               243
96        Curium       Cm               247
97        Berkelium    Bk               247
98        Californium  Cf               251
99        Einsteinium  Es               252
100       Fermium      Fm               257

```

Yes. Your code only needs to deal with elements having atomic number 0-100 as seen in the table above.

# Rules

# Test Cases

```
O    --> 16
Sn   --> 119
H    --> 1
Fm   --> 257

```

# Scoring

This is , so the shortest submission (in bytes) wins.

# Answer
> 28 votes
# CJam, ~~219~~ ~~213~~ 211 bytes

```
"
b$2#ÐÇ°aë#ëG0ÚÛ;½_+â¨9¨%ôº¯ó¾Ù""ÏÑ¯ÀcKÌA6ïõÃVå%x²XßM³0øUà°ÅæM¨Ã([©bþÚðÐÔ×¼&Và«òð×øùÚ¼5·¹IõÉðÑ§é°i9õÛ[&i÷jÍ¶û_<-c-uîá¹¹»D+èF"]256fb~26b'Af+2/'Jf-reua#__.0042*2.2+*i4-@Ab@=+

```

Try it online. Because of unprintables, the permalink might not work in all browsers. For the two long strings the code points are

```
[10 98 36 50 159 35 208 199 176 24 134 97 15 235 35 235 71 7 48 218 17 219 59 189 95 43 226 168 57 168 20 37 244 29 186 142 175 243 190 134 140 217]
[4 207 209 175 192 99 75 204 65 54 239 140 245 195 17 86 229 19 37 120 178 88 223 77 179 147 48 20 248 85 224 176 197 230 77 168 195 8 154 40 143 91 169 129 98 19 254 218 240 208 212 215 147 188 38 5 86 224 129 171 242 240 215 248 249 26 218 137 188 53 183 185 73 245 135 4 201 240 209 167 157 19 233 176 105 57 245 219 91 159 38 105 247 106 205 128 182 251 95 60 132 45 31 99 45 117 238 225 128 185 185 187 146 68 43 232 8 70]

```

respectively.

The program uses the quadratic regression `mass ~ floor(2.2(a-1) + 0.0042(a-1)^2)`, where `a` is atomic number. The reason for the `a-1` is because of 0-indexing. This regression gives an error range of `[-4, 5]`, which allows a base 10 encoding of the offsets.

Mathematica code for the regression:

```
data = {{0, 1}, {1, 4}, {2, 7}, {3, 9}, {4, 11}, {5, 12}, {6, 14}, {7, 16}, {8, 19}, {9, 20}, {10, 23}, {11, 24}, {12, 27}, {13, 28}, {14, 31}, {15, 32}, {16, 35}, {17, 40}, {18, 39}, {19, 40}, {20, 45}, {21, 48}, {22, 51}, {23, 52}, {24, 55}, {25, 56}, {26, 59}, {27, 59}, {28, 64}, {29, 65}, {30, 70}, {31, 73}, {32, 75}, {33, 79}, {34, 80}, {35, 84}, {36, 85}, {37, 88}, {38, 89}, {39, 91}, {40, 93}, {41, 96}, {42, 98}, {43, 101}, {44, 103}, {45, 106}, {46, 108}, {47, 112}, {48, 115}, {49, 119}, {50, 122}, {51, 128}, {52, 127}, {53, 131}, {54, 133}, {55, 137}, {56, 139}, {57, 140}, {58, 141}, {59, 144}, {60, 145}, {61, 150}, {62, 152}, {63, 157}, {64, 159}, {65, 163}, {66, 165}, {67, 167}, {68, 169}, {69, 173}, {70, 175}, {71, 178}, {72, 181}, {73, 184}, {74, 186}, {75, 190}, {76, 192}, {77, 195}, {78, 197}, {79, 201}, {80, 204}, {81, 207}, {82, 209}, {83, 209}, {84, 210}, {85, 222}, {86, 223}, {87, 226}, {88, 227}, {89, 232}, {90, 231}, {91, 238}, {92, 237}, {93, 244}, {94, 243}, {95, 247}, {96, 247}, {97, 251}, {98, 252}, {99, 257}};
parabola = Fit[data, {1, x, x^2}, x]
Show[ListPlot[data, PlotMarkers -> {Automatic, Tiny}], Plot[parabola, {x, 0, 100}], Frame -> True]

```

Which gives:

```
-0.019569 + 2.20617 x + 0.00417056 x^2

```

# Answer
> 12 votes
# CJam, ~~175~~ 174 bytes

```
0000000: 7232316222faaf6c3e227b693138302b257d2f296322  r21b"..l>"{i180+%}/)c"
0000016: 0221dce3cc4eb9561f953aaf640771c048ca3b9926c1  .!...N.V..:.d.q.H.;.&.
000002c: 4d033768588e2545069a202e128f0e238546134bdb31  M.7hX.%E.. ....#.F.K.1
0000042: c2e9e6924a7a8642dab33d6b789009112b5a8a652dd3  ....Jz.B..=kx...+Z.e-.
0000058: 7dbf0f0b27eb18e52808e857967bd7be4ce2545181b8  }...'...(..W.{..L.TQ..
000006e: 01101447504f4443c6392449222322f43a606ce335e0  ...GPODC.9$I"#".:`l.5.
0000084: 4356e76914357218e3942869eb8ab3f97e2e161ef061  CV.i.5r...(i....~....a
000009a: 59c310c6cbe922323538623762364365723e3a2b      Y....."258b7b6Cer>:+

```

The above is a reversible xxd dump. You can try the code online in the CJam interpreter.

If the permalink doesn't work in your browser, you can copy the code from this paste.

In supported browsers, you can verify all test cases at once.

### Idea

The actual weights are encoded in the same fashion as in Martin Büttner's answer, with three minor modifications:

* 12's are replaced with 6's instead of 7's.
* The encoding is done in base 7 instead of base 8.
* The order of the elements is reversed.

The difference between our answers lies in how the element abbreviations are stored.

In this approach, we hash all 100 element abbreviations into 8-bit characters, by doing the following:

* Take the element name (treated as the array of its code points) and apply base 21 conversion.

Examples: `"H"21b` yields **72**, `"He"21b` yields **1613 = 21 × 72 + 101**.
* Successively compute the residue of the division by 430, 355, 288 and 242.

Example: `1613 430% 355% 288% 242%` yields **35**.
* Increment and cast to character.

Example: `35)c` yields **$**.

### Code

```
r         e# Read a whitespace-separated token from STDIN.
21b       e# Convert to intger, using base 21 conversion.
"ú¯l>"    e# Push that string.
{i180+%}/ e# Cast each char to integer, add 180 and apply modular division.
)c        e# Add 1 and cast to character.

"!ÜãÌN¹V:¯dqÀHÊ;&ÁM7hX%E .#FKÛ1ÂéæJzBÚ³=kx  +Ze-Ó}¿'ëå(èW{×¾LâTQ¸GPODCÆ9$I"

          e# Push that string.
          e# "He", e.g., is hashed to '$', which is the second char from the right.

#         e# Compute the index of the character hash in the string.

"ô:`lã5àCVçi5rã(ië³ù~.ðaYÃÆËé"

          e# Push that string.

 258b7b   e# Convert from base 258 to base 7.
 6Cer     e# Replace 6's with 12's.
 >        e# Keep only weight differences before the hashes occurrence.
 :+       e# Push the sum of the weight differences.

```

# Answer
> 11 votes
# CJam, ~~201~~ 195 bytes

```
"üæ1Îe3©ü^<ùÉkIxX¯þÄäj¤ý¨,N8jËäVªw{®Ö_<U
Zká++ØÙ/ªQÒ?»<yÙ>K_þÉ«2´EªÄ¡8ÕþÀ!-,ñ©ZÒàF"256b26b2/Gf-qeu'Af-a#)"#ØmÔwXÍÒA5(Wâ£ogg5ÂßòtwÆÚ^ØW¶°¥"258b8b7Cer<:+

```

I'm sure there are a bunch of unprintable characters in there, so use this permalink for testing.

I used this script to encode the abbreviations and masses as two numbers, and this script to generate the base encoding. (These are mostly for myself if I need to change anything.)

## Explanation

Of course, the main compression comes from base encoding: to compress any array, interpret it as digits in some sufficiently large base to get a single number - then get the base-256 (or similar) digits of that number and turn them into characters. That process is reversible, such that we can recover the array from that string.

The above code contains two such base encodings: one for the abbreviations, and one for the masses. To make those two arrays more amenable to base encoding (i.e. to lower the base, as that makes the overall number and string significantly shorter), I did some preprocessing on the table.

First, I sorted the entire table by masses to get a non-decreasing sequence. Then I computed the successive differences between those, because the sequence is rising rather slowly. In fact, the steps are always less than 13. That allows me to use base 13 (and then recover the masses by truncating that array to the correct length and summing all elements). However, we can do better: there is only a single 12, and the next smaller step is 6. So we can replace that 12 by a 7 and use base 8, which saves quite a bit.

To encode the abbreviations, we pad the single-character elements with `Q` (which doesn't appear in any abbreviation), join them all together and take the difference with `A` or `a` (such that each letter becomes a number between `0` and `25` inclusive). This is then encoded in base 26. The abbreviations can be recovered by splitting the base-26 digits into pairs of 2 and removing all `16`s (which correspond to the `Q`s).

Here is the code:

```
e# Decode the abbreviations

"gibberish"256b26b
     e# Get the base-26 digits representing the letters.
2/   e# Split into pairs.
Gf-  e# Remove 16s (Qs).

e# Process the input

qeu  e# Read input and convert to upper case.
'Af- e# Subtract the characters from 'A' to get numbers.
a#)  e# Find the position in the decoded array.

e# Find the mass

"gibberish"258b8b
     e# Get the base-8 digits representing the mass increments.
7Cer e# Replace the 7 by a 12.
<    e# Truncate, based on the position of the input in the abbreviations.
:+   e# Sum all the increments.

```

# Answer
> 10 votes
# Bash + coreutils, 381

```
base64 -d<<<H4sICPGCjVUAA3B0LnR4dABNUMuShCAMvPcveQIEoUaQAtzdmf//kO2Ol7GKEJP0I2QAOTKcBfBMPALQgIu1hKZWc6gHb3dicgodkzGcDHjBjS04pZhEYmniB2Hwqg1JBOHaWrGRcOPTLDuEOdh107ARfhjhwPAqDLyBD2tNv5V+lvjHzZPlYif2MFJmKEY7Fac3hYIl8T+GYBqekqdDiOiSajt6fYzzivdji1zL8DvlM1UjZ5fNvVk/NZaTFtUCvzQjEVxUKGLti7aMK8vc0it1An3Z+gW38PUNvo8ww8FpN3SHla3VOh5DrqIrC3XzL9s2Ido6SBX/wOjUg78BAAA=|zcat|sed -n "/\b$1\b/I="

```

This is simply the elements represented in a file thus:

```
H

HE

LI

BE
...

```

Note the line number of the element corresponds with the mass. In cases where there are multiple elements with the same mass, the line looks like:

```
AR;CA

```

The file is gzipped and base64 encoded. Note the element names are all in upper case to improve compression. Interestingly gzip and bzip2 had identical compressed lengths, and xz was a little bit longer.

The `base64 -d` and `zcat` simply convert the base64 stream back to this original file. The `sed` then simply searches (case insensitively) for the input string and outputs its line number.

### Test output:

```
$ for e in O Sn H Fm Ni Co; do ./pt.sh $e; done
16
119
1
257
59
59
$

```

# Answer
> 8 votes
# Marbelous, 1426 bytes

```
}0}1
LNLn
=A
RLSS
=G=M=T=C=U=L=R=S
P1P2P2P2P1P2P4P7
P0P4P1P2P9P7P0P5
P8P3P0P7//PNPNPN
PNPNPNPN
}0}1
LNLn
=B
RLSS
=I=A=K=R=EP1
P2P1P2P8P9P1
P0P3P4P0PNPN
P9P7//PN
PNPN
}0}1
LNLn
=C
RLSS
=D=E=F=M=S=A=L=O=R=U
P1P1P2P2P1P4P3P5P5P6P1
P1P4P5P4P3P0P5P9P2P4P2
P2P0P1P7P3PNPNPNPNPNPN
PNPNPNPNPN
}0}1
LNLn
=E
RLSS
=R=S=U
P1P2P1
P6P5//
P7P2
PNPN
}0}1
LNLn
=F
RLSS
=M=R=E
P2P2P5P1
P5P2P6P9
P7P3PNPN
PNPN
}0}1
LNLn
=G
RLSS
=D=A=E
P1P7P7
P5P0P3
P7PNPN
PN
}0}1
LNLn
=H
RLSS
=F=G=O=E
P1P2P1P4P1
P7P0P6PNPN
P8P1P5
PNPNPN
}0}1
LNLn
=I
RLSS
P1
=N=R
P1P9P2
P5P2P7
PNPNPN
}0}1
LNLn
>J
<N
RLSS
=A=U=N=O=R=G=IP3
P1P1P5P9P8P2P7P9
P3P7P5P6P4//PNPN
P9P5PNPNPN
PNPN
}0}1
LNLn
=N
RLSS
=P=D=I=E=B=AP1
P2P1P5P2P9P2P4
P3P4P9P0P3//PN
P7P4PNPNPN
PNPN
}0}1
LNLn
=O
RLSS
P1
=S
P9P6
P0PN
PN
}0}1
LNLn
=P
RLSS
=U=A=O=B=T=M=D=R
P2P2P2P2P1P1P1P1
P4P3P0P0P9P4P0P4P3
P4P1P9P7P5P5P6P1//
PNPNPNPNPNPNPNPN
}0}1
LNLn
=R
RLSS
=N=H=U=A=E=B
P2P1P1P2P1P8
P2P0P0P2P8P5
P2P3P1P6//PN
PNPNPNPN
}0}1
LNLn
=S
RLSS
=M=B=N=E=C=R=IP3
P1P1P1P7P4P8P2P2
P5P2P1P9P5P8//PN
P0P2P9PNPNPN
PNPNPN
}0}1
LNLn
=T
RLSS
=H=L=A=E=B=M=C=I
P2P2P1P1P1P1P9P4
P3P0P8P2P5P6P8//
P2P4P1P8P9//PN
PNPNPNPNPN
}0}1
LNLn
>X
RLSS
=B=R=N
P1P9P6
P7P1P5
P3PNPN
PN
}0
LN
=Y=X=W=V=U=D
P8P1P1P5P2P1
P9P3P8P1P3P6
..P1P4..P8P3
PNPNPNPNPNPN
:P0
}0
{030
:P1
}0
{031
:P2
}0
{032
:P3
}0
{033
:P4
}0
{034
:P5
}0
{035
:P6
}0
{036
:P7
}0
{037
:P8
}0
{038
:P9
}0
{039
:PN
}0
\/0A
:LN
}0
-Z
-K
{0
:Ln
}0
-W
-Z
-K
{0
:RLSS
}1}0
{0\/

```

Input is provided as two command line parameters. Marbelous does not currently have a way to handle variable length input and halt, so the second parameter is required. For one-letter symbols, just pass in any character as the second parameter, as long as it doesn't make a valid two-letter symbol. I hope to improve the language in the future to remove this limitation.

Also, a bug in the python interpreter currently requires the `-m 0` parameter, as stdout is not being memoized correctly for functions.

```
# marbelous.py -m 0 mass.mbl H e
4

# marbelous.py -m 0 mass.mbl T a
181

```

# Answer
> 6 votes
# JavaScript ES6, 326

```
s=>eval(`/${s}(\\d+)/.exec(btoa\`QÞà¸»ïA×PµØÝx;^×Ó^ÛCZÛs Û%Û´¢ÛÃ÷Õ-ö
]ù¾4+BksSçP«çc'ç^ç ¨çÓbçÐ®ëgëïAïp,ïïÐkóB«ó[ó«óÆ<õýÔÖýÜÊ=é7=ñµÓTa×MÏw]:
tð'u×b'×^R]}I½vÙ7µÛÂ5ÛµÞ×}B³]÷wì¶µßÐ×¯^55Ýxàùµã¦×»^vÝyí6õçÐò×Ç£^¹½zí9µëÖ×½Ë»^ùý{ñ6µóUµó^×Î³_t"½}Øûu÷.×ÞÇm5N]´àööÓ°bÛOO£m=ÝµÑöÛakÛmÑkmºÍ¶í8vßcÚÛ}TÛ
§mû>í¸à ¶ãp¦ÛÁn;    ý¹ÔK6çafÛ\`)`)[1]

```

JSFiddle, the special charecters keep having issues

~~I hate to use an object and then search through it but I'll try to golf this more when I get access to a computer~~. Updated

Template Strings are the same length as regular strings so it doesn't matter. `.exec` actually stores the capture groups starting in `[1]` which adds another unavoidable 3 bytes, JavaScript's RegEx also doesn't support lookbehind but that wouldn't matter because that would be longer

I'm using `atob` which is used to convert from Base64 but if you use it the other way, you can chop off about a hundred bytes

# Answer
> 6 votes
### Excel VBA, 1587 bytes

So it isn't really a good golf attempt, I used a standard CASE statement, which looks like:

```
n = InputBox("n")
Select Case n
Case "H"
m = 1
Case "He"
m = 4
Case "Li"
m = 7
......
End Select

```

For all 100 elements specified in the question, it will read in the abbreviation and then display its weight in a message box.
But what was interesting is instead of spending ages typing it all out, I used another bit of VBA to generate the CASE statement:

```
FilePath = Application.DefaultFilePath & "\code.txt"
Open FilePath For Output As #2

For i = 3 To UsedRange.Rows.Count
sentence = sentence + "Case " & Chr(34) & Cells(i, 3) & Chr(34) & vbNewLine & "m = " & Cells(i, 4) & vbNewLine
Next i

Print #2, sentence
Close #2

```

I copy and pasted the helpful table used in the question, the macro then reads the needed columns and pieces together the entire CASE statement used above.

I used this guide to work out how to output to a text file: http://www.homeandlearn.org/write\_to\_a\_text\_file.html

# Answer
> 5 votes
# Python 2, 405 401 bytes

Call `w(e)` to print the weight of element e:

```
def w(e):import re;print reduce(lambda c,(_,d):c+int(d)-1,re.findall(r"(\D+)(\d+)",re.match(".*%s\d"%e,"H2He4Li4Be3B3C2N3O3F4Ne2Na4Mg2Al4Si2P4S2Cl4Ar6K0Ca2Sc6Ti4V4Cr2Mn4Fe2Co4Ni1Cu6Zn2Ga6Ge4As3Se5Br2Kr5Rb2Sr4Y2Zr3Nb3Mo4Tc3Ru4Rh3Pd4Ag3Cd5In4Sn5Sb4Te7I0Xe5Cs3Ba5La3Ce2Pr2Nd4Pm2Sm6Eu3Gd6Tb3Dy5Ho3Er3Tm3Yb5Lu3Hf4Ta4W4Re3Os5Ir3Pt4Au3Hg5Tl4Pb4Bi3Po1At2Rn13Fr2Ra4Ac2Th6Pa0U8Np0Pu8Am0Cm5Bk1Cf5Es2Fm6").group()),0)

```

Tests:

```
w("O")
w("Sn")
w("H")
w("Fm")

```

Output:

```
16
119
1
257

```

**Updated** \- now 401 bytes

I was sure those nested regexs were too wordy, so I've managed to drop one of them, and put in a beautifully convoluted lambda instead :-)

```
def w(e):import re;print reduce(lambda(r,m),(l,d):(r+m*(int(d)-1),m*(l!=e)),re.findall(r"(\D+)(\d+)","H2He4Li4Be3B3C2N3O3F4Ne2Na4Mg2Al4Si2P4S2Cl4Ar6K0Ca2Sc6Ti4V4Cr2Mn4Fe2Co4Ni1Cu6Zn2Ga6Ge4As3Se5Br2Kr5Rb2Sr4Y2Zr3Nb3Mo4Tc3Ru4Rh3Pd4Ag3Cd5In4Sn5Sb4Te7I0Xe5Cs3Ba5La3Ce2Pr2Nd4Pm2Sm6Eu3Gd6Tb3Dy5Ho3Er3Tm3Yb5Lu3Hf4Ta4W4Re3Os5Ir3Pt4Au3Hg5Tl4Pb4Bi3Po1At2Rn13Fr2Ra4Ac2Th6Pa0U8Np0Pu8Am0Cm5Bk1Cf5Es2Fm6"),(0,1))[0]

```

# Answer
> 5 votes
# Python 2 ~~364~~, ~~335~~, 324 bytes

## 3rd attempt

Last program, which beats all non cjam entries.

```
#coding:latin1
c="ôÜÅÌ¿ß*[hÌ¹IÊALu>-\ÏÃnay,ÃU[ŠáŠJðûC¢\rýÑÏR¬\0W¶°ò£w¿<M¹KŸ0I\fî1Ž[b¥!iÖœC>÷©€)rë>:öæö\nœèãbXñ0Èéà8J¯6sõ'ìA±+yï#»ÀÙ<Š=Ùï"
n=sum([ord(c[i])*256**i for i in range(145)])
w=raw_input()+'q'
s=1
while n and n%541!=ord(w[0])*21+ord(w[1])-1464:
    n/=541;s+=n%6;n/=6
print(s,s+7)[s>210]

```

## 2nd attempt

Here's a second tentative, but it's still not very statisfactory. I can get 335 bytes by first building a table 676 bits, then get the position of a pair of letters by counting the elements preceding it int the table, and finally get the mass using a 100 bytes table. Both tables are specified in latin1 in order to get values over 128.

```
#coding:latin1
w=raw_input()+'`'
o=ord
print(o("ájñ&IÐÃ    ÏõN\n&nù!õ92>¡¥ú6ÿÝDG°Ç£}qŸ%R5^[9ëŒåÍhÏÁòàSžeÜcx+MuV³`~æ.Ê§ì1¶W«?Y"[''.join(['{:08b}'.format(o(k))for k in "îóÃçuûüÇË/ÿÿÿßÿüŸ÷÷¿ìÿÿûÇ÷ÿýßÿÿÿýÿÿßßýÿïÏþ'~ÿßÿûø¿Öÿÿÿóo¿}.çÅÿ÷ÿÿþÿÿÿßÿÿÿßÿÿ_ÿÿÿÿ»ÿ"])[:o(w[0])*27+o(w[1])-1851].count('0')])+2,1)[w=='H`']

```

## 1st attempt 364

I think Python is a pretty good challenge for this game. I've tried to encode the different strings, but my decoder is always too big. And since python is very verbose and all characters appearing in the file have to be printable, it's pretty hard to get a small program. Here's my best solution so far:

```
w=raw_input()
x='HqHeLiBeBqCqNqOqFqNeNaMgAlSiPqSqClKqCaArScTiVqCrMnFeNiCoCuZnGaGeAsSeBrKrRbSrYqZrNbMoTcRuRhPdAgCdInSnSbIqTeXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWqReOsIrPtAuHgTlPbBiPoAtRnFrRaAcPaThNpUqAmPuBkCmCfEsFm'.find((w,w+'q')[len(w)<2])/2
print (1,8)[x>84]+sum(map(int,str(int('2zbca9fh3fq7op0ta54q928z3xc5l7hp7yyu1in8al1iis2fkjt91pxi0kkgw94f',36)))[:x])

```

which does not use big ideas, just the differences are encoded in base 6, then base 36, the maximum of int build-in.

# Answer
> 3 votes
# Python3, ~~856~~ 660 bytes

```
print(dict(H=1,He=4,Li=7,Be=9,B=11,C=12,N=14,O=16,F=19,Ne=20,Na=23,Mg=24,Al=27,Si=28,P=31,S=32,Cl=35,Ar=40,K=30,Ca=40,Sc=45,Ti=48,V=51,Cr=52,Mn=55,Fe=56,Co=58,Ni=58,Cu=64,Zn=65,Ga=70,Ge=73,As=75,Se=79,Br=80,Kr=84,Rb=85,Sr=88,Y=89,Zr=91,Nb=93,Mo=96,Tc=98,Ru=101,Rh=103,Pd=106,Ag=108,Cd=112,In=115,Sn=119,Sb=122,Te=128,I=127,Xe=131,Cs=133,Ba=137,La=139,Ce=140,Pr=141,Nd=144,Pm=145,Sm=150,Eu=152,Gd=157,Tb=159,Dy=163,Ho=165,Er=167,Tm=169,Yb=173,Lu=175,Hf=178,Ta=181,W=184,Re=186,Os=190,Ir=192,Pt=195,Au=197,Hg=201,Tl=204,Pb=207,Bi=209,Po=209,At=210,Rn=222,Fr=223,Ra=226,Ac=227,Th=232,Pa=231,U=238,Np=237,Pu=244,Am=243,Cm=247,Bk=247,Cf=251,Es=252,Fm=257)[input()])

```

Thanks to @Sp3000, I could save a whopping 196 bytes!

Test it here

---

Old version (856 bytes):

```
print({"H":1,"He":4,"Li":7,"Be":9,"B":11,"C":12,"N":14,"O":16,"F":19,"Ne":20,"Na":23,"Mg":24,"Al":27,"Si":28,"P":31,"S":32,"Cl":35,"Ar":40,"K":39,"Ca":40,"Sc":45,"Ti":48,"V":51,"Cr":52,"Mn":55,"Fe":56,"Co":59,"Ni":59,"Cu":64,"Zn":65,"Ga":70,"Ge":73,"As":75,"Se":79,"Br":80,"Kr":84,"Rb":85,"Sr":88,"Y":89,"Zr":91,"Nb":93,"Mo":96,"Tc":98,"Ru":101,"Rh":103,"Pd":106,"Ag":108,"Cd":112,"In":115,"Sn":119,"Sb":122,"Te":128,"I":127,"Xe":131,"Cs":133,"Ba":137,"La":139,"Ce":140,"Pr":141,"Nd":144,"Pm":145,"Sm":150,"Eu":152,"Gd":157,"Tb":159,"Dy":163,"Ho":165,"Er":167,"Tm":169,"Yb":173,"Lu":175,"Hf":178,"Ta":181,"W":184,"Re":186,"Os":190,"Ir":192,"Pt":195,"Au":197,"Hg":201,"Tl":204,"Pb":207,"Bi":209,"Po":209,"At":210,"Rn":222,"Fr":223,"Ra":226,"Ac":227,"Th":232,"Pa":231,"U":238,"Np":237,"Pu":244,"Am":243,"Cm":247,"Bk":247,"Cf":251,"Es":252,"Fm":257}[input()])

```

# Answer
> 3 votes
## JavaScript 344 337 chars

Code:
Since it has some special chars and will not work when adding to to SO as a snippet, here is a link: https://jsfiddle.net/stefnotch/gz3hu8bq/2/

Hexdump:

```
0000000: 733d 3e61 6c65 7274 2852 6567 4578 7028  s=>alert(RegExp(
0000010: 6024 7b73 7d28 5c5c 5729 6029 2e65 7865  `${s}(\\W)`).exe
0000020: 6328 2248 7b48 657e 4c69 c281 4265 c283  c("H{He~Li..Be..
0000030: 42c2 8543 c286 4ec2 884f c28a 46c2 8d4e  B..C..N..O..F..N
0000040: 65c2 8e4e 61c2 914d 67c2 9241 6cc2 9553  e..Na..Mg..Al..S
0000050: 69c2 9650 c299 53c2 9a43 6cc2 9d41 72c2  i..P..S..Cl..Ar.
0000060: a24b c2a1 4361 c2a2 5363 c2a7 5469 c2aa  .K..Ca..Sc..Ti..
0000070: 56c2 ad43 72c2 ae4d 6ec2 b146 65c2 b243  V..Cr..Mn..Fe..C
0000080: 6fc2 b54e 69c2 b543 75c2 ba5a 6ec2 bb47  o..Ni..Cu..Zn..G
0000090: 61c3 8047 65c3 8341 73c3 8553 65c3 8942  a..Ge..As..Se..B
00000a0: 72c3 8a4b 72c3 8e52 62c3 8f53 72c3 9259  r..Kr..Rb..Sr..Y
00000b0: c393 5a72 c395 4e62 c397 4d6f c39a 5463  ..Zr..Nb..Mo..Tc
00000c0: c39c 5275 c39f 5268 c3a1 5064 c3a4 4167  ..Ru..Rh..Pd..Ag
00000d0: c3a6 4364 c3aa 496e c3ad 536e c3b1 5362  ..Cd..In..Sn..Sb
00000e0: c3b4 5465 c3ba 49c3 b958 65c3 bd43 73c3  ..Te..I..Xe..Cs.
00000f0: bf42 61c4 834c 61c4 8543 65c4 8650 72c4  .Ba..La..Ce..Pr.
0000100: 874e 64c4 8a50 6dc4 8b53 6dc4 9045 75c4  .Nd..Pm..Sm..Eu.
0000110: 9247 64c4 9754 62c4 9944 79c4 9d48 6fc4  .Gd..Tb..Dy..Ho.
0000120: 9f45 72c4 a154 6dc4 a359 62c4 a74c 75c4  .Er..Tm..Yb..Lu.
0000130: a948 66c4 ac54 61c4 af57 c4b2 5265 c4b4  .Hf..Ta..W..Re..
0000140: 4f73 c4b8 4972 c4ba 5074 c4bd 4175 c4bf  Os..Ir..Pt..Au..
0000150: 4867 c583 546c c586 5062 c589 4269 c58b  Hg..Tl..Pb..Bi..
0000160: 506f c58b 4174 c58c 526e c598 4672 c599  Po..At..Rn..Fr..
0000170: 5261 c59c 4163 c59d 5468 c5a2 5061 c5a1  Ra..Ac..Th..Pa..
0000180: 55c5 a84e 70c5 a750 75c5 ae41 6dc5 ad43  U..Np..Pu..Am..C
0000190: 6dc5 b142 6bc5 b143 66c5 b545 73c5 b646  m..Bk..Cf..Es..F
00001a0: 6dc5 bb22 295b 315d 2e63 6861 7243 6f64  m..")[1].charCod
00001b0: 6541 7428 292d 3132 3229 0a              eAt()-122).

```

Even more golfed: https://jsfiddle.net/stefnotch/gz3hu8bq/4/

Hexdump:

```
0000000: 733d 3e65 7661 6c28 602f 247b 737d 285c  s=>eval(`/${s}(\
0000010: 5c57 292f 2e65 7865 6328 2248 7b48 657e  \W)/.exec("H{He~
0000020: 4c69 c281 4265 c283 42c2 8543 c286 4ec2  Li..Be..B..C..N.
0000030: 884f c28a 46c2 8d4e 65c2 8e4e 61c2 914d  .O..F..Ne..Na..M
0000040: 67c2 9241 6cc2 9553 69c2 9650 c299 53c2  g..Al..Si..P..S.
0000050: 9a43 6cc2 9d41 72c2 a24b c2a1 4361 c2a2  .Cl..Ar..K..Ca..
0000060: 5363 c2a7 5469 c2aa 56c2 ad43 72c2 ae4d  Sc..Ti..V..Cr..M
0000070: 6ec2 b146 65c2 b243 6fc2 b54e 69c2 b543  n..Fe..Co..Ni..C
0000080: 75c2 ba5a 6ec2 bb47 61c3 8047 65c3 8341  u..Zn..Ga..Ge..A
0000090: 73c3 8553 65c3 8942 72c3 8a4b 72c3 8e52  s..Se..Br..Kr..R
00000a0: 62c3 8f53 72c3 9259 c393 5a72 c395 4e62  b..Sr..Y..Zr..Nb
00000b0: c397 4d6f c39a 5463 c39c 5275 c39f 5268  ..Mo..Tc..Ru..Rh
00000c0: c3a1 5064 c3a4 4167 c3a6 4364 c3aa 496e  ..Pd..Ag..Cd..In
00000d0: c3ad 536e c3b1 5362 c3b4 5465 c3ba 49c3  ..Sn..Sb..Te..I.
00000e0: b958 65c3 bd43 73c3 bf42 61c4 834c 61c4  .Xe..Cs..Ba..La.
00000f0: 8543 65c4 8650 72c4 874e 64c4 8a50 6dc4  .Ce..Pr..Nd..Pm.
0000100: 8b53 6dc4 9045 75c4 9247 64c4 9754 62c4  .Sm..Eu..Gd..Tb.
0000110: 9944 79c4 9d48 6fc4 9f45 72c4 a154 6dc4  .Dy..Ho..Er..Tm.
0000120: a359 62c4 a74c 75c4 a948 66c4 ac54 61c4  .Yb..Lu..Hf..Ta.
0000130: af57 c4b2 5265 c4b4 4f73 c4b8 4972 c4ba  .W..Re..Os..Ir..
0000140: 5074 c4bd 4175 c4bf 4867 c583 546c c586  Pt..Au..Hg..Tl..
0000150: 5062 c589 4269 c58b 506f c58b 4174 c58c  Pb..Bi..Po..At..
0000160: 526e c598 4672 c599 5261 c59c 4163 c59d  Rn..Fr..Ra..Ac..
0000170: 5468 c5a2 5061 c5a1 55c5 a84e 70c5 a750  Th..Pa..U..Np..P
0000180: 75c5 ae41 6dc5 ad43 6dc5 b142 6bc5 b143  u..Am..Cm..Bk..C
0000190: 66c5 b545 73c5 b646 6dc5 bb22 295b 315d  f..Es..Fm..")[1]
00001a0: 2e63 6861 7243 6f64 6541 7428 292d 3132  .charCodeAt()-12
00001b0: 3260 290a                                2`).

```

It uses RegExp to find the appropriate char from the string then it converts to it's Unicode value and subtracts something.
I wonder if this can be shortened even further.
Credits: manatwork, vihan1086 (Mass of elements)

# Answer
> 3 votes
# Rev 1, Ruby, 327

I'm fairly new to Ruby, so thanks to **ManAtWork** for refactoring and massively improving the code part of my program, for a saving of 42 bytes.

```
"1H3He3Li2Be2B1C2N2O3F1Ne3Na1Mg3Al1Si3P1S3Cl4K1Ar0Ca5Sc3Ti3V1Cr3Mn1Fe3Co0Ni5Cu1Zn5Ga3Ge2As4Se1Br4Kr1Rb3Sr1Y2Zr2Nb3Mo2Tc3Ru2Rh3Pd2Ag4Cd3In4Sn3Sb5I1Te3Xe2Cs4Ba2La1Ce1Pr3Nd1Pm5Sm2Eu5Gd2Tb4Dy2Ho2Er2Tm4Yb2Lu3Hf3Ta3W2Re4Os2Ir3Pt2Au4Hg3Tl3Pb2Bi0Po1At12Rn1Fr3Ra1Ac4Pa1Th5Np1U5Am1Pu3Cm0Bk4Cf1Es5Fm0"=~/#{gets}\d/
p eval($`.gsub /\D/,?+)

```

As far as I can tell, it works like this:

the `=~` ("match") operator searches the data for an incidence of a string of type `/#{gets}\d/` where `#{gets}` is an element input by the user and `\d` can be any number. This returns the position/index where the element occurs in the big string, but the return value is thrown away.

`$'` (not used here) is a special global variable which contains the part of the string to the right of the last match. `$``  is the complementary special global variable which contains the part of the string to the left of the last match. This is basically the truncated string from my rev 0.

The remaining code to subsitute all the letters for `+`symbols and evaluate the expression formed works in the same way as my original answer, except that ManAtWork has improved my original `.gsub(/[A-z]/,'+')` to `.gsub /\D/,?+`

# Rev 0, Ruby, 369

```
s="1H3He3Li2Be2B1C2N2O3F1Ne3Na1Mg3Al1Si3P1S3Cl4K1Ar0Ca5Sc3Ti3V1Cr3Mn1Fe3Co0Ni5Cu1Zn5Ga3Ge2As4Se1Br4Kr1Rb3Sr1Y2Zr2Nb3Mo2Tc3Ru2Rh3Pd2Ag4Cd3In4Sn3Sb5I1Te3Xe2Cs4Ba2La1Ce1Pr3Nd1Pm5Sm2Eu5Gd2Tb4Dy2Ho2Er2Tm4Yb2Lu3Hf3Ta3W2Re4Os2Ir3Pt2Au4Hg3Tl3Pb2Bi0Po1At12Rn1Fr3Ra1Ac4Pa1Th5Np1U5Am1Pu3Cm0Bk4Cf1Es5Fm0"
e=gets
puts eval(s[0..s.gsub(/[0-9]/,'*').index(e+'*')-1].gsub(/[A-z]/,'+'))

```

The numbers in the string correspond to the difference between an element and the previous one (elements are in atomic mass order, not atomic number order, so the difference is always positive.)

The first `gsub` replaces all numbers with `*`. Then we search for `<element name>+"*"` to find the index of the element (the appended `*` is necessary to avoid confusing for example, `Be` and `B` because one is a substring of the other.)

The second `gsub` replaces all letters with `+`. This produces an expression that gives the mass of the element: `1+3++3++2...` (Ruby does not care about the duplicate `+`, presumably it considers the second `+` to be a unary `+`.) All that remains is to truncate the expression at the index mentioned above, and evaluate it.

# Answer
> 3 votes
# Python 3, 560 bytes

```
import functools as f
n="HᱨἬᨊBCNOFỆᶎỻ᭬∋PS᱄ᳲKᥣ’≴Vᷖ№ᮞᴍ῾ẟ⚬᫧ᰃᴳ₿ᵤⅦὤ⓶Y⠔ᷜⅣ⁼╺⅐ὀᨧᨬ὞⎪ῆℤI⊸ḙᤂ᳌ᩯ⎠Ṹ∐⍗ᾉᮼ
․ἸẺ⏄−⊼Ჰ῔W⁚⍽₂⑀ᶵ᳸⍰Ạᬒ⊰ᵴ⌼Ἤἒᤣ∠ṐU∠⒐ᮭᲇᮖ᪲ỿ᷎"
m="!$')+,.03478;<?@CHGHMPSTWX[[`afikoptuxy{}€‚…‡ŠŒ“—š Ÿ£¥©«¬°±¶¸½¿ÃÅÇÉÍÏÒÕØÚÞàãåéìïññòþÿĂăĈćĎčĔēėėěĜġ"
i=lambda w:f.reduce(lambda l,c:l*ord(c),list(w),1)
print(str(ord(m[n.find(chr(i(input())))])-32))

```

Try it online!

Explanation: So 2 variables, `n` and `m` are used to hold the abbreviations (more described below) and the elements atomic mass as strings. The input is then encrypted into a character to match to some character in `n`, using the string.find, an index of where the character is can be used to find the respective atomic mass in `m`. Due to shifting the mass values by 32 previously, they must be unshifted, then this number is output.

Each character of `n` is the ascii of the product of the ascii values of the characters in the name Each character of `m` is the ascii of mass + 32

Below is the setup code for `m` and `n`

```
obfNames=""
for name in names:
    prod=1
    for c in name:
        prod*=ord(c)
    obfNames+=chr(prod)
obfMass=""
for m in mass:
    obfMass+=chr(int(m)+32)
for c in obfNames:
    print(c,end="")
print()
for c in obfMass:
    print(c,end="")

```

Try it online!

# Answer
> 3 votes
# , ~~301~~ ~~289~~ ~~287~~ 273 bytes

Explanation.

Look up the index of the mass sorted atomic number in a UTF8 encoded hash string `s`. The hash is taken by taking the element name, encoding it as ASCII, multiplying by a 2-vector encoded with UTF-8 and modding three times in succession. The operator `+` is used for the UTF8 to integer vector conversion.

Encode the masses of the elements as a UTF8 encoded string of differences, packing 2 differences into each byte as `9*d1 + d2` where `d1` is the difference of the nth element and `d2` is the successive mass difference of the n+50th element. Then to retrieve the mass, take the corresponding position at the cumulative sum of the differences. This is shorter than a quadratic formula.

```
function(x)sum(c(o%%9,o%/%9)[1:match(sum(+x*+"")%%740%%623%%278,+'{ćWZ}èċæŸ+Ó>qÿƒ!2Eo&ē_u½8`h›’Hb‰¹ĎŽ‹@-if9ÃÛ%ù]V‚:LkXlÙDRŠ•˜OU·m,Sµ5TËt6cC7#/ąz1A')])
"+"=utf8ToInt
o=+"0%..%$%%
p

&00(1"

```

Try it online!

A mention to this 275 byte solution which *nearly* beats the R record by encoding three mass differences in mixed base 6,5.

# Answer
> 2 votes
# Ruby: ~~474~~ 468 characters

```
f=->e{"H1He4Li7Be9B11C12N14O16F19Ne20Na23Mg24Al27Si28P31S32Cl35Ar40K39Ca40Sc45Ti48V51Cr52Mn55Fe56Co59Ni59Cu64Zn65Ga70Ge73As75Se79Br80Kr84Rb85Sr88Y89Zr91Nb93Mo96Tc98Ru101Rh103Pd106Ag108Cd112In115Sn119Sb122Te128I127Xe131Cs133Ba137La139Ce140Pr141Nd144Pm145Sm150Eu152Gd157Tb159Dy163Ho165Er167Tm169Yb173Lu175Hf178Ta181W184Re186Os190Ir192Pt195Au197Hg201Tl204Pb207Bi209Po209At210Rn222Fr223Ra226Ac227Th232Pa231U238Np237Pu244Am243Cm247Bk247Cf251Es252Fm257"=~/#{e}(\d+)/;$><<$1}

```

Sample run:

```
irb(main):001:0> f=->e{"H1He4Li7Be9B11C12N14O16F19Ne20Na23Mg24Al27Si28P31S32Cl35Ar40K39Ca40Sc45Ti48V51Cr52Mn55Fe56Co59Ni59Cu64Zn65Ga70Ge73As75Se79Br80Kr84Rb85Sr88Y89Zr91Nb93Mo96Tc98Ru101Rh103Pd106Ag108Cd112In115Sn119Sb122Te128I127Xe131Cs133Ba137La139Ce140Pr141Nd144Pm145Sm150Eu152Gd157Tb159Dy163Ho165Er167Tm169Yb173Lu175Hf178Ta181W184Re186Os190Ir192Pt195Au197Hg201Tl204Pb207Bi209Po209At210Rn222Fr223Ra226Ac227Th232Pa231U238Np237Pu244Am243Cm247Bk247Cf251Es252Fm257"=~/#{e}(\d+)/;$><<$1}
=> #<Proc:0x00000001e59d08@(irb):2 (lambda)>
irb(main):002:0> f["K"]
39=> #<IO:<STDOUT>>

```

Online run:

http://rextester.com/edit/UMNY88866

# Answer
> 2 votes
# PHP, 893 695 bytes

```
function a($e,$m=[H,1,He,4,Li,7,Be,9,B,11,C,12,N,14,O,16,F,19,Ne,20,Na,23,Mg,24,Al,27,Si,28,P,31,S,32,Cl,35,Ar,40,K,39,Ca,40,Sc,45,Ti,48,V,51,Cr,52,Mn,55,Fe,56,Co,59,Ni,59,Cu,64,Zn,65,Ga,70,Ge,73,'As',75,Se,79,Br,80,Kr,84,Rb,85,Sr,88,Y,89,Zr,91,Nb,93,Mo,96,Tc,98,Ru,101,Rh,103,Pd,106,Ag,108,Cd,112,In,115,Sn,119,Sb,122,Te,128,I,127,Xe,131,Cs,133,Ba,137,La,139,Ce,140,Pr,141,Nd,144,Pm,145,Sm,150,Eu,152,Gd,157,Tb,159,Dy,163,Ho,165,Er,167,Tm,169,Yb,173,Lu,175,Hf,178,Ta,181,W,184,Re,186,Os,190,Ir,192,Pt,195,Au,197,Hg,201,Tl,204,Pb,207,Bi,209,Po,209,At,210,Rn,222,Fr,223,Ra,226,Ac,227,Th,232,Pa,231,U,238,Np,237,Pu,244,Am,243,Cm,247,Bk,247,Cf,251,Es,252,Fm,257]){return $m[array_search($e,$m)+1];}

```

The usage of the function is to omit the 2nd argument, eg

```
echo a('Ra'); // returns 226

```

Requires PHP \>=5.4 to allow `[...]` for array declaration and suppressing errors (at least `error_reporting(~E_NOTICE)` is given or there is the same clause in the php.ini file).

The `As` needs to be in quotes because it is considered as a reserved keyword.

# Answer
> 2 votes
# PHP 4.1, ~~645~~ ~~643~~ 641 bytes

This is not the best idea, it may be possible to minimize a little more.

```
<?$V=split(A,A1A4A7A9AbAcAeAgAjAkAnAoArAsAvAwAzA14A13A14A19A1cA1fA1gA1jA1kA1nA1nA1sA1tA1yA21A23A27A28A2cA2dA2gA2hA2jA2lA2oA2qA2tA2vA2yA30A34A37A3bA3eA3kA3jA3nA3pA3tA3vA3wA3xA40A41A46A48A4dA4fA4jA4lA4nA4pA4tA4vA4yA51A54A56A5aA5cA5fA5hA5lA5oA5rA5tA5tA5uA66A67A6aA6bA6gA6fA6mA6lA6sA6rA6vA6vA6zA70A75);echo base_convert($V[array_search($E,array(H,He,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,'As',Se,Br,Kr,Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,I,Xe,Cs,Ba,La,Ce,Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,Ta,W,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At,Rn,Fr,Ra,Ac,Th,Pa,U,Np,Pu,Am,Cm,Bk,Cf,Es,Fm))+1],36,10);

```

Sadly, I've seen myself forced to use Base36 instead of Base96.

To use it, simply set the key `E` in POST/GET/SESSION with the name of the element.

To emulate it in newer versions, simply add `$E=<element name>;` before `$V`.

You can test it on http://rextester.com/OMAA85454

# Answer
> 2 votes
# Python 3, ~~500~~ 495

Pretty self-explanatory. Just use a regex built from the input to find the number after the element's abbreviation.

**Try it here**

```
import re
print(re.findall(r"(?<=%s)\d+"%input(),"H1He4Li7Be9B11C12N14O16F19Ne20Na23Mg24Al27Si28P31S32Cl35Ar40K39Ca40Sc45Ti48V51Cr52Mn55Fe56Co59Ni59Cu64Zn65Ga70Ge73As75Se79Br80Kr84Rb85Sr88Y89Zr91Nb93Mo96Tc98Ru101Rh103Pd106Ag108Cd112In115Sn119Sb122Te128I127Xe131Cs133Ba137La139Ce140Pr141Nd144Pm145Sm150Eu152Gd157Tb159Dy163Ho165Er167Tm169Yb173Lu175Hf178Ta181W184Re186Os190Ir192Pt195Au197Hg201Tl204Pb207Bi209Po209At210Rn222Fr223Ra226Ac227Th232Pa231U238Np237Pu244Am243Cm247Bk247Cf251Es252Fm257")[0])

```

# Answer
> 2 votes
# SAS, 351 <sup>(chars)</sup> 791 <sup>(bytes)</sup> 454 bytes

Borrowing from the concept posted by Sp3000, using a quadratic regression to model the positional offsets mass = floor(o\*0.715+0.00031\*o\*o+1.7)+e where o represents the offset and e is the known error from actual.

```
%macro m(e);data;s='H-1He0Li1Be1B0C0N1O1F3Ne2Na3Mg2Al3Si01P1S01Cl2Ar5K01Ca0Sc2Ti3V4Cr3Mn4Fe2Co3Ni01Cu3Zn01Ga3Ge4As4Se6Br4Kr6Rb5Sr5Y04Zr3Nb03Mo03Tc02Ru02Rh00Pd00Ag-1Cd00In0Sn1Sb2Te5I2Xe4Cs4Ba5La5Ce3Pr02Nd02Pm-1Sm1Eu00Gd2Tb2Dy3Ho3Er2Tm01Yb2Lu1Hf2Ta2W3Re3Os5Ir4Pt4Au4Hg5Tl6Pb6Bi5Po03At00Rn9Fr7Ra7Ac6Th8Pa04U8Np5Pu9Am6Cm7Bk04Cf05Es02Fm03';o=prxmatch("#(?<=&e)[-\d]#",s)-1;a=floor(o*.715+.00031*o*o+1.7)+prxchange("s#.*&e(-?\d+).*#\1#",1,s);put a;run;%mend;

```

# Answer
> 2 votes
# Javascript ES6 354 bytes

```
p=e=>+'1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm'.split(RegExp(e+'[^a-z].*'))[0].split('').reduce((o,n)=>+n?+n+o:n=='-'?--o:++o)

```

Ungolfed:

```
function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
  .split(RegExp(e+'[^a-z].*'))[0]
  .split('')
  .reduce(function(o,n) {
    return +n ? +n+o : n=='-' ? --o : ++o;
  })
}

```

The elements are stored in a string of numbers symbols and minus characters. The mass of any given element is the number of letter characters preceding it, plus the sum of digits subtracted by the occurrences of `-`.

## Tests:

```
var a = [
  'H', 1,
  'He', 4,
  'Li', 7,
  'Be', 9,
  'B', 11,
  'C', 12,
  'N', 14,
  'O', 16,
  'F', 19,
  'Ne', 20,
  'Na', 23,
  'Mg', 24,
  'Al', 27,
  'Si', 28,
  'P', 31,
  'S', 32,
  'Cl', 35,
  'Ar', 40,
  'K', 39,
  'Ca', 40,
  'Sc', 45,
  'Ti', 48,
  'V', 51,
  'Cr', 52,
  'Mn', 55,
  'Fe', 56,
  'Co', 59,
  'Ni', 59,
  'Cu', 64,
  'Zn', 65,
  'Ga', 70,
  'Ge', 73,
  'As', 75,
  'Se', 79,
  'Br', 80,
  'Kr', 84,
  'Rb', 85,
  'Sr', 88,
  'Y', 89,
  'Zr', 91,
  'Nb', 93,
  'Mo', 96,
  'Tc', 98,
  'Ru', 101,
  'Rh', 103,
  'Pd', 106,
  'Ag', 108,
  'Cd', 112,
  'In', 115,
  'Sn', 119,
  'Sb', 122,
  'I', 127,
  'Te', 128,
  'Xe', 131,
  'Cs', 133,
  'Ba', 137,
  'La', 139,
  'Ce', 140,
  'Pr', 141,
  'Nd', 144,
  'Pm', 145,
  'Sm', 150,
  'Eu', 152,
  'Gd', 157,
  'Tb', 159,
  'Dy', 163,
  'Ho', 165,
  'Er', 167,
  'Tm', 169,
  'Yb', 173,
  'Lu', 175,
  'Hf', 178,
  'Ta', 181,
  'W', 184,
  'Re', 186,
  'Os', 190,
  'Ir', 192,
  'Pt', 195,
  'Au', 197,
  'Hg', 201,
  'Tl', 204,
  'Pb', 207,
  'Bi', 209,
  'Po', 209,
  'At', 210,
  'Rn', 222,
  'Fr', 223,
  'Ra', 226,
  'Ac', 227,
  'Pa', 231,
  'Th', 232,
  'U', 238,
  'Np', 237,
  'Pu', 244,
  'Am', 243,
  'Cm', 247,
  'Bk', 247,
  'Cf', 251,
  'Es', 252,
  'Fm', 257
];

function p(e) {
  return "1H2He1LiBeBC1N1O2FNe1Na-Mg1Al-Si1PS2Cl2KAr--Ca3Sc1Ti1VCr1Mn-Fe1Co--Ni3Cu-Zn3Ga1GeAs2Se-Br2Kr-Rb1Sr-Y1ZrNb1MoTc1RuRh1PdAg2Cd1In2Sn1Sb3ITe1XeCs2BaLa-Ce-Pr1Nd-Pm3SmEu3GdTb2DyHoErTm2YbLu1Hf1Ta1W1Re2OsIr1PtAu2Hg1Tl1PbBi--Po-At19Rn-Fr1Ra-Ac2Pa-Th3Np-U4Am-Pu1Cm--Bk2Cf-Es1Fm"
    .split(RegExp(e + '[^a-z].*'))[0]
    .split('')
    .reduce(function(o, n) {
      return +n ? +n + o : n == '-' ? --o : ++o;
    })
}
var $elementList = $('#elements');
for (var i = 0; i < a.length; i++) {
  var symbol = a[i];
  var mass = a[++i];
  var result = p(symbol);
  var resultString = "<li><b>" + symbol + "</b>";
  if (symbol.length === 1) resultString += " ";
  resultString += " expected <b>" + mass + "</b>";
  if (mass < 10) resultString += " ";
  if (mass < 100) resultString += " ";
  resultString += " got <b>" + result + "</b></li>";
  $elementList.append(resultString);
}
```

```
#elements li {
  font-family: "Consolas", monospace;
}
```

```
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="elements">
</ul>
```

# Answer
> 2 votes
# C (gcc), ~~373~~ 371 bytes

-2 bytes thanks to ceilingcat

One string containing the elements is used for lookup. When an index is found, this is used to traverse the second string, each character of which holds two mass deltas, one per nibble. (Each character has also been increased by 32 to bring them into printable territory.) In other words, we do not save the masses of the elements, only how much they differ from the previous one, in a list sorted by mass.

Adding all these deltas together gets us the mass for half of the elements (the ones that are in even slots). If we are in an odd slot, we remove the last delta again.

```
x,w,c,i;f(char*e){char*E="H HeLiBeB C N O F NeNaMgAlSiP S ClK ArCaScTiV CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY ZrNbMoTcRuRhPdAgCdInSnSbI TeXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcPaThNpU AmPuCmBkCfEsFm",a[3]={*e++,*e?:32};for(x=strstr(E,a)-E,w=i=0;i<=x/4;w+=c%16+c/16-2)c="3RABQQQQT0sQQPqsD432CCCDTU3DA35EDBDCSDCDS@<34553$5"[i++];c=x&2?w:w-c%16;}

```

Try it online!

# Answer
> 1 votes
# Python 2, ~~860~~ ~~859~~ 858 bytes

Based on @Cool Guy's version -

```
def f(x):print {"H":1,"He":4,"Li":7,"Be":9,"B":11,"C":12,"N":14,"O":16,"F":19,"Ne":20,"Na":23,"Mg":24,"Al":27,"Si":28,"P":31,"S":32,"Cl":35,"Ar":40,"K":39,"Ca":40,"Sc":45,"Ti":48,"V":51,"Cr":52,"Mn":55,"Fe":56,"Co":59,"Ni":59,"Cu":64,"Zn":65,"Ga":70,"Ge":73,"As":75,"Se":79,"Br":80,"Kr":84,"Rb":85,"Sr":88,"Y":89,"Zr":91,"Nb":93,"Mo":96,"Tc":98,"Ru":101,"Rh":103,"Pd":106,"Ag":108,"Cd":112,"In":115,"Sn":119,"Sb":122,"Te":128,"I":127,"Xe":131,"Cs":133,"Ba":137,"La":139,"Ce":140,"Pr":141,"Nd":144,"Pm":145,"Sm":150,"Eu":152,"Gd":157,"Tb":159,"Dy":163,"Ho":165,"Er":167,"Tm":169,"Yb":173,"Lu":175,"Hf":178,"Ta":181,"W":184,"Re":186,"Os":190,"Ir":192,"Pt":195,"Au":197,"Hg":201,"Tl":204,"Pb":207,"Bi":209,"Po":209,"At":210,"Rn":222,"Fr":223,"Ra":226,"Ac":227,"Th":232,"Pa":231,"U":238,"Np":237,"Pu":244,"Am":243,"Cm":247,"Bk":247,"Cf":251,"Es":252,"Fm":257}[x]

```

Usage -

```
f("K")

```

Output -

```
39

```

Try it here

# Answer
> 1 votes
# Python 2, 630 bytes

Based on @Cool Guy's code, just packed all the strings into one and then used regexp to get them back. It saved me few bytes.

**edit:** rewrote mass list, just to be sure it's correct this time.

```
import re;print dict(zip(map(str.strip,re.findall("..","H HeLiBeB C N O F NeNaMgAlSiP S ClArK CaScTiV CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY ZrNbMoTcRuRhPdAgCdInSnSbTeI XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaU NpPuAmCmBkCfEsFm")),(1,4,7,9,11,12,14,16,19,20,23,24,27,28,31,32,35,40,39,40,45,48,51,52,55,56,59,59,64,65,70,73,75,79,80,84,85,88,89,91,93,96,98,101,103,106,108,112,115,119,122,128,127,131,133,137,139,140,141,144,145,150,152,157,159,163,165,167,169,173,175,178,181,184,186,190,192,195,197,201,204,207,209,209,210,222,223,226,227,232,231,238,237,244,243,247,247,251,252,257)))[raw_input()]

```

# Answer
> 1 votes
## Java, 414 407 Bytes

```
void m(String e){String s="5.=\\>i6K<35)<?<@CB/L<Y0 ?M0ZCL59?OOA&z.8KI>qCR2v@./D@>)JP22%Jh=[::Dr2uH@.aA=5?:95h@H6)B+7J=J6~DH@*G?<xS*&xDw:<CX5L/A3$=H1`N0:rKW5nJ198:$8oD*:y=v<_CS6[H^:(An:nE<?`<u7T+*3Icj2y<].jLm /_}$7^U#;Ft)rD|33N#",a=s;int i=0,n=0,c,b;while(!a.equals(e)){c=s.charAt(i++)*95+s.charAt(i++)-3072;a=""+(char)(65+c%26);b=97+c/26%26;if(b!=122)a+=(char)b;c/=676;n+=c==9?12:c-1;}System.out.print(n);}

```

Usage: `m("He")`

The tuple `(element code, delta from previous element)` is transformed into an integer varying from 0 to 6760 (=26x26x10), then encoded in base 95 into the string.

It can be tested here: http://rextester.com/VFXLG54655

# Answer
> 1 votes
# Javascript ES6, 620 bytes

This is a pretty basic one, like my PHP answer.

It uses base36 to encode the values.

```
s=>parseInt("1|4|7|9|b|c|e|g|j|k|n|o|r|s|v|w|z|14|13|14|19|1c|1f|1g|1j|1k|1n|1n|1s|1t|1y|21|23|27|28|2c|2d|2g|2h|2j|2l|2o|2q|2t|2v|2y|30|34|37|3b|3e|3k|3j|3n|3p|3t|3v|3w|3x|40|41|46|48|4d|4f|4j|4l|4n|4p|4t|4v|4y|51|54|56|5a|5c|5f|5h|5l|5o|5r|5t|5t|5u|66|67|6a|6b|6g|6f|6m|6l|6s|6r|6v|6v|6z|70|75".split('|')["H0He0Li0Be0B0C0N0O0F0Ne0Na0Mg0Al0Si0P0S0Cl0Ar0K0Ca0Sc0Ti0V0Cr0Mn0Fe0Co0Ni0Cu0Zn0Ga0Ge0As0Se0Br0Kr0Rb0Sr0Y0Zr0Nb0Mo0Tc0Ru0Rh0Pd0Ag0Cd0In0Sn0Sb0Te0I0Xe0Cs0Ba0La0Ce0Pr0Nd0Pm0Sm0Eu0Gd0Tb0Dy0Ho0Er0Tm0Yb0Lu0Hf0Ta0W0Re0Os0Ir0Pt0Au0Hg0Tl0Pb0Bi0Po0At0Rn0Fr0Ra0Ac0Th0Pa0U0Np0Pu0Am0Cm0Bk0Cf0Es0Fm".split(0).indexOf(s)],36)

```

It creates an anonymous function that returns an integer value. Invalid values return weird results.

Try it (ES5, requires support for `Array.prototype.indexOf()`):

```
f=function(s){return parseInt("1|4|7|9|b|c|e|g|j|k|n|o|r|s|v|w|z|14|13|14|19|1c|1f|1g|1j|1k|1n|1n|1s|1t|1y|21|23|27|28|2c|2d|2g|2h|2j|2l|2o|2q|2t|2v|2y|30|34|37|3b|3e|3k|3j|3n|3p|3t|3v|3w|3x|40|41|46|48|4d|4f|4j|4l|4n|4p|4t|4v|4y|51|54|56|5a|5c|5f|5h|5l|5o|5r|5t|5t|5u|66|67|6a|6b|6g|6f|6m|6l|6s|6r|6v|6v|6z|70|75".split('|')["H0He0Li0Be0B0C0N0O0F0Ne0Na0Mg0Al0Si0P0S0Cl0Ar0K0Ca0Sc0Ti0V0Cr0Mn0Fe0Co0Ni0Cu0Zn0Ga0Ge0As0Se0Br0Kr0Rb0Sr0Y0Zr0Nb0Mo0Tc0Ru0Rh0Pd0Ag0Cd0In0Sn0Sb0Te0I0Xe0Cs0Ba0La0Ce0Pr0Nd0Pm0Sm0Eu0Gd0Tb0Dy0Ho0Er0Tm0Yb0Lu0Hf0Ta0W0Re0Os0Ir0Pt0Au0Hg0Tl0Pb0Bi0Po0At0Rn0Fr0Ra0Ac0Th0Pa0U0Np0Pu0Am0Cm0Bk0Cf0Es0Fm".split(0).indexOf(s)],36)}

document.getElementById('e').onkeyup=function(){
  document.getElementById('o').innerHTML=this.value?f(this.value):'';
}
```

```
<input type="text" id="e" placeholder="Element"><br>
<span>Element mass: <b id="o"></b></span>
```

# Answer
> 1 votes
# PHP, ~~415~~ ~~414~~ 387 bytes

```
<?preg_match("/\d".$_GET[e]."\d/",$b="2H3He4Li4Be6B2C3N3O5F4Ne4Na3Mg3Al2Si2P6S3Cl6Ar2K1Ca4Sc4Ti5V4Cr5Mn3Fe4Co1Ni3Cu2Zn4Ga5Ge4As6Se4Br5Kr4Rb4Sr3Y3Zr2Nb3Mo2Tc2Ru2Rh2Pd1Ag3Cd3In4Sn5Sb8Te4I6Xe6Cs7Ba6La5Ce3Pr3Nd1Pm4Sm3Eu5Gd4Tb6Dy5Ho4Er3Tm5Yb4Lu4Hf4Ta4W5Re6Os5Ir5Pt4Au6Hg6Tl6Pb5Bi2Po0At9Rn8Fr8Ra6Ac8Th4Pa8U5Np9Pu5Am6Cm4Bk5Cf3Es5Fm0",$m,256);$s=$m[0][1];echo round(.0003*$s*$s+.81*$s-1)+$b{$s};

```

This answer works basically off the same principle as Sp3000's CJam answer; I did a quadratic regression with the element's position in the `$b` string as the independent variable and molar mass as the dependent, then found the difference between the actual value and regression value for each element, and put it all into the $b string. I actually originally did this in Java, but then discovered that it takes a lot of bytes to do regex in Java.

I've never done a code golf before so please let me know if I'm doing something wrong here.

~~Edit: try it out here~~ (Actually, that link probably doesn't work)

Edit 2: Ismael Miguel helped me knock a bunch of bytes off of this!

# Answer
> 1 votes
# Powershell, 324 bytes

```
('1H3He3Li2Be2B1C2N2O3F1Ne3Na1Mg3Al1Si3P1S3Cl4K1ArCa5Sc3Ti3V1Cr3Mn1Fe3CoNi5Cu1Zn5Ga3Ge2As4Se1Br4Kr1Rb3Sr1Y2Zr2Nb3Mo2Tc3Ru2Rh3Pd2Ag4Cd3In4Sn3Sb5I1Te3Xe2Cs4Ba2La1Ce1Pr3Nd1Pm5Sm2Eu5Gd2Tb4Dy2Ho2Er2Tm4Yb2Lu3Hf3Ta3W2Re4Os2Ir3Pt2Au4Hg3Tl3Pb2BiPo1At12Rn1Fr3Ra1Ac4Pa1Th5Np1U5Am1Pu3CmBk4Cf1Es5Fm'-split"$args")[0]-replace'\D+','+'|iex

```

Test script:

```
$f = {

('1H3He3Li2Be2B1C2N2O3F1Ne3Na1Mg3Al1Si3P1S3Cl4K1ArCa5Sc3Ti3V1Cr3Mn1Fe3CoNi5Cu1Zn5Ga3Ge2As4Se1Br4Kr1Rb3Sr1Y2Zr2Nb3Mo2Tc3Ru2Rh3Pd2Ag4Cd3In4Sn3Sb5I1Te3Xe2Cs4Ba2La1Ce1Pr3Nd1Pm5Sm2Eu5Gd2Tb4Dy2Ho2Er2Tm4Yb2Lu3Hf3Ta3W2Re4Os2Ir3Pt2Au4Hg3Tl3Pb2BiPo1At12Rn1Fr3Ra1Ac4Pa1Th5Np1U5Am1Pu3CmBk4Cf1Es5Fm'`
-split"$args")[0]-replace'\D+','+'|iex

}

@(
    ,(16,'O')
    ,(119,'Sn')
    ,(1,'H')
    ,(257,'Fm')
) | % {
    $e,$a = $_
    $r = &$f $a
    "$($e-eq$r): $r"
}

```

Output:

```
True: 16
True: 119
True: 1
True: 257

```

## Explanation

The datastring contains chemical element labels and differences of the masses of adjacent elements. Zero differences are removed.

The script:

1. splits the datastring by a target element label.
2. takes a first substring with elements and differences that precede the target
3. replaces nondigit symbols to `+` (result for Oxigen element is: `1+3+3+2+2+1+2+2`)
4. calculates the result string as powershell expression

# Answer
> 1 votes
# C (clang), ~~624~~ 623 bytes

```
*a=L"H效楌敂BCNOF敎慎杍汁楓PS汃牁K慃捓楔V牃湍敆潃楎畃湚慇敇獁敓牂牋扒牓Y牚扎潍捔畒桒摐杁摃湉湓打敔I敘獃慂慌敃牐摎浐浓畅摇扔祄潈牅浔扙界晈慔W敒獏牉瑐畁杈汔扐楂潐瑁湒牆慒捁桔慐U灎畐流浃歂晃獅浆";f(short*x){return L"敉敌敏救敓敔敖敘敛敜敟敠散敤敧敨敫数敯数敵數敻敼敿斀斃斃斈斉斎斑斓斗斘斜斝斠斡斣斥斨斪断斯斲斴斸斻斿旂旈旇旋旍旑旓旔旕旘旙旞无日旧旫旭旯旱旵旷旺旽昀昂昆昈昋昍昑昔昗昙昙昚昦昧昪昫昰是昶昵昼昻昿昿晃晄晉"[wcschr(a,*x)-a]-a[1];}

```

Try it online!

Just a pair of table lookups. Little endian only.

# Answer
> 0 votes
# PowerShell , 655

```
$S="Ac227Ag108Al027Am243Ar040As075At210Au197B 011Ba137Be009Bi209Bk247Br080C 012Ca040Cd112Ce140Cf251Cl035Cm247Co059Cr052Cs133Cu064Dy163Er167Es252Eu152F 019Fe056Fm257Fr223Ga070Gd157Ge073H 001He004Hf178Hg201Ho165I 127In115Ir192K 039Kr084La139Li007Lu175Mg024Mn055Mo096N 014Na023Nb093Nd144Ne020Ni059Np237O 016Os190P 031Pa231Pb207Pd106Pm145Po209Pr141Pt195Pu244Ra226Rb085Re186Rh103Rn222Ru101S 032Sb122Sc045Se079Si028Sm150Sn119Sr088Ta181Tb159Tc098Te128Th232Ti048Tl204Tm169U 238V 051W 184Xe131Y 089Yb173Zn065Zr091"

$a=$args[0][0];$b=$args[0][1];if(!$b){$b=" "};
for($i=0;$i-lt500;$i+=5){if(($a-match$s[$i])-and$b-match$s[$i+1]){$s[$i+2]+$s[$i+3]+$s[$i+4];break}}

```

the string s was constructed with

```
awk '{printf(\"%-2s %03d\n\",$3,$4)}' .\input.txt | sort | awk '{printf(\"%-2s%03d\",$1,$2)}' > .\output.txt

```

result as follows

# Answer
> 0 votes
# T-SQL, ~~627~~ 524 bytes

```
SELECT ISNULL((SELECT SUBSTRING(value,3,3)FROM
STRING_SPLIT('B 11-F 19-Na23-Al27-P 31-K 39-Ar40-V 51-Mn55-Co59-Cu64-Se79-Sr88-Nb93-I 127-La139-Pr141-Nd144-Bi209-Po209-Rn222-Pa231-Np237-Am243-Cm247-Cf251','-')m,t
WHERE s=LEFT(value,2)),
(SELECT CHARINDEX(s,'H .He.LiBe.C N O ..Ne..Mg..Si..S .Cl..KCa...Sc.Ti..Cr..Fe.Ni....Zn...Ga.GeAs...Br..KRb..Y Zr...MoTc.RuRh.PdAg..Cd.In..Sn.Sb....Te.XeCs..Ba.Ce...Pm...SmEu...GdTb..DyHoErTm..YbLu.Hf.Ta.W Re..OsIr.PtAu..Hg.Tl.Pb.At...........Fr.RAc...Th....U ....Pu.Bk...Es...Fm')
FROM t))

```

Significant redesign, here, saved over 100 bytes.

Here is how this one works, reading back to front:

* The main piece here is a string (inside the `CHARINDEX()`, the dots are just filler) that *returns the element mass based on the string position*, for example, the letters `Ga` start at string position 70, which is the mass of Gallium.
* That unfortunately doesn't work for elements that have the *same* mass (`Ar` and `Ca`, for example), or elements that differ with mass that differs only by one (`Al` and `Si`), so I have a more traditional lookup table (inside the `STRING_SPLIT()`) that covers the exceptions.
* The `ISNULL()` statement pieces it all together: first it looks in the exception table, if that returns `NULL`, it instead returns the position from `CHARINDEX`.

**Original version, 627 bytes**:

```
SELECT SUBSTRING(value,3,3)
FROM STRING_SPLIT('H 1-He4-Li7-Be9-B 11-C 12-N 14-O 16-F 19-Ne20-Na23-Mg24-Al27-Si28-P 31
    -S 32-Cl35-Ar40-K 39-Ca40-Sc45-Ti48-V 51-Cr52-Mn55-Fe56-Co59-Ni59-Cu64-Zn65-Ga70-Ge73
    -As75-Se79-Br80-Kr84-Rb85-Sr88-Y 89-Zr91-Nb93-Mo96-Tc98-Ru101-Rh103-Pd106-Ag108-Cd112
    -In115-Sn119-Sb122-Te128-I 127-Xe131-Cs133-Ba137-La139-Ce140-Pr141-Nd144-Pm145-Sm150
    -Eu152-Gd157-Tb159-Dy163-Ho165-Er167-Tm169-Yb173-Lu175-Hf178-Ta181-W 184-Re186-Os190
    -Ir192-Pt195-Au197-Hg201-Tl204-Pb207-Bi209-Po209-At210-Rn222-Fr223-Ra226-Ac227-Th232
    -Pa231-U238-Np237-Pu244-Am243-Cm247-Bk247-Cf251-Es252-Fm257','-')
,t WHERE LEFT(value,2)=s

```

Returns are for formatting only.

Per our IO standards, input is taken via pre-existing table **t** with chemical symbol **s**. Joins to in-memory table derived from a delimited string.

# Answer
> 0 votes
# JavaScript (Node.js), 313 bytes

*Look Ma, no unprintable characters!*

(It does however use `Buffer`, making it a Node solution.)

```
s=>"046c;,1`6/+i2~2t9u(U8u-E;6+,<.+k8KBU+x !Hy;]<3+o9 )5<b#+KE*(DL;(6]=L/-A3'K>D+T3/.C=S/_>u/>8s3S@=<kA46cL5(X:#6N<02},h/K6-.7H84wCX2LG6013_1_>.7y5p8g>F/SDv2S:Z2~=A;N744]&I,|xV,e5L,,@d-dJA,*Fw.*7_$(?g/@FX".replace(/../g,S=>(B=Buffer)([([a,b]=B(S),n+=a*95+b-3513)%26+65,n/26%26+97])==(s[1]?s:s+'q')?n/676|0:'',n=0)

```

Try it online!

### Encoding

We first make sure that the data list is sorted by ascending mass first, and lexicographical order of the chemical symbols second.

For each pair \\$(s,m)\\$ of chemical symbol \\$s\\$ with mass \\$m\\$, we compute:

$$v=26^2\times m+26\times c\_0+c\_1$$

where:

* \\$c\_0\\$ is the ASCII code of the first character of \\$s\\$, minus \\$65\\$ (\\$0\\$ for 'A' to \\$25\\$ for 'Z')
* \\$c\_1\\$ is the ASCII code of the second character of \\$s\\$, minus \\$97\\$ (\\$0\\$ for 'a' to \\$25\\$ for 'z'), or the default value \\$16\\$ (for 'q') if there's no second character

We then compute the delta value between \\$v\\$ and the previous value \\$p\\$, plus \\$1\\$:

$$d=v-p+1$$

Each value of \\$d\\$ satisfies:

$$29\le d\le 8549 \< 95^2$$

Finally, we encode \\$d\\$ in base 95 and turn this into a 2-character string, using the printable ASCII range \\$\[32\dots126\]\\$. Once all results are concatenated together, the final encoding string is exactly 200 characters long.

NB: The only reason why we use \\$v-p+1\\$ rather than just \\$v-p\\$ is that it avoids baskslashes that would need to be escaped.

The following link generates a summary table of the encoding process:

Try it online!

### Decoding

We start with \\$n=0\\$. For each pair of encoding ASCII codes \\$(a,b)\\$, we add to \\$n\\$:

$$(a-32)\times 95+(b-32)-1=a\times 95+b-3073$$

The ASCII code of the symbol are:

$$c\_0=(n\bmod 26)+65$$ $$c\_1=(\lfloor n/26\rfloor \bmod 26)+97$$

and the mass is:

$$m=\lfloor n/26^2\rfloor=\lfloor n/676\rfloor$$

---
Tags: code-golf, chemistry
---


title: Extract the contained powers of two!

text:
# Question
Title: Extract the contained powers of two!
Take as input an integer in any reasonable format.

Then, output all the proper sub-strings of the base-10 digits of that integer that are powers of two, in any reasonable format.

# Test cases

```
10230 -> [1, 2]
13248 -> [32, 1, 2, 4, 8]
333 -> []
331 -> [1]
32 -> [2] # As 32 is not a proper sub-string, since it is the whole string.
322 -> [32, 2, 2]
106 -> [1]
302 -> [2]
464 -> [4, 4, 64]
655365536 -> [65536, 65536]

```

# Answer
> 4 votes
# Vyxal, 57 bitsv2, 7.125 bytes

```
ǎ$o'E¨²c

```

Try it Online!

Can't believe I let 05AB1E win for so long smh. Takes input as a string.

## Explained

```
ǎ$o'E¨²c
ǎ$o'      # Keep substrings of the input (excluding the input) where:
     ¨²   #   All the powers of 2
    E  c  #   Contains the substring.

```

Created with the help of Luminespire.

# Answer
> 4 votes
# , 30 bytes

```
(]#~1#.E.~&":"#:)2^[:i.2>.@^.]

```

Try it online!

*-4 bytes thanks to Neil's approach of generating all the powers of 2 and finding matches.*

* `2^[:i.2>.@^.]` All the powers of 2 less than the input.
* `(1#.E.~&":"#:)` Gets a count of the matches for each.
* `]#~` Uses that as a mask to copy the powers of two. Makes copies of those that occur more than once, and removes those that don't occur at all.

# Answer
> 4 votes
# 05AB1E, ~~10~~ 9 bytes

```
ŒIKʒ¬Ā*bR

```

Try it online or verify all test cases.

**Explanation:**

```
Œ         # Get all substrings of the (implicit) input
 IK       # Remove the input itself
   ʒ      # Filter this list by:
    ¬     #  Push its leading digit (without popping the number)
     Ā    #  Check that it's NOT 0 (0 if 0; 1 otherwise)
      *   #  Multiply that to the number
       b  #  Convert it to binary
        R #  Reverse it
          #  (only 1 is truthy in 05AB1E, including "01","001","0001",etc.)
          # (after which the filtered result is output implicitly)

```

# Answer
> 4 votes
# Excel, 136 bytes

```
=LET(s,ROW(1:1024),r,s-1,t,TRANSPOSE(s),p,""&2^r,m,IF(t+r>LEN(A1),,MID(A1,s,t)),TEXTJOIN(",",,IFERROR(m*1^MATCH(IF(--m=A1,,m),p,0),"")))

```

Input is in cell A1 and is limited to 308 digits long since 2^1023 = 8.99e+307 and that's the highest value Excel can handle in that series.

---

It's a fairly long formula but `LET()` allows us to break it into chunks of `variable, value` except for the last term which is the output.

* `s,ROW(1:999)` creates a vertical array of values 1-1024.
* `r,s-1` offsets that array by 1. This is a wash on bytes vs. using `s-1` directly later but it does make the formula look nicer.
* `t,TRANSPOSE(s)` creates a horizontal array of values 1-1024.
* `p,""&2^r` creates an array of *string* values from 2^0 to 2^1023. It's important later on that these are strings and not numbers.
* `m,IF(t+r>LEN(A1),,MID(A1,s,t))` creates a triangular array of all the various substrings (and also the entire string but that'll get ignored later). For the input of `13248`, that looks like this:
(This is just the top left corner since most of the 1024x1024 table is `0`.)

---

That's all the variables we define. The last term is the output so we'll break that down bit by bit.

```
TEXTJOIN(",",,IFERROR(m*1^MATCH(IF(--m=A1,,m),p,0),""))

```
* `IF(--m=A1,,m)` let's us ignore the entire input even if it's a power of 2. For instance, `32`. I could have required the input be a string and then drop the `--` here to convert the string `m` to a number, but Excel defaults to things that look like numbers being numbers and it's worth 2 bytes (to me) to not require funky inputs.
* `MATCH(IF(--m=A1,,m),p,0)` tries to find an exact match for the `m` value in the powers of 2 array. If it doesn't find a match, this will equal `#N/A`. This is the part that makes it important for the powers to be strings. If we converted `m` to numbers and matched those, them string like `02` would resolve to be just `2` so they would match. Something line `102` would return `1,2,2` instead of just `1,2`.
* `m*1^MATCH(~)` multiplies `m` by either `1` or `#N/A`.
* `IFERROR(~,"")` converts all the errors to blanks. For the input of `13248`, that looks like this:
* `TEXTJOIN(",",,IFERROR(~))` concatenates all the values with a comma between each term.

---

The screenshot below shows the formula in `B1`, the results for all the test cases, and the snippets showing partial results for input `13248` that are also shown above.

# Answer
> 3 votes
# Charcoal, 20 bytes

```
≔¹ηＷ‹ηＩθ«Ｅ⌕ＡθＩηＩη≦⊗η

```

Try it online! Link is to verbose version of code. Explanation:

```
≔¹η

```

Start by checking for embedded `1`s.

```
Ｗ‹ηＩθ«

```

Repeat until we reach the input number.

```
Ｅ⌕ＡθＩηＩη

```

Output each copy of the current power of 2 found in the input. (`FindAll` finds overlapping matches, otherwise `65536` would only be found once in `655365536`.)

```
≦⊗η

```

Try the next power of 2.

# Answer
> 3 votes
# C (gcc), ~~99~~ ~~96~~ ~~83~~ 81 bytes

* -5 thanks to ceilingcat
* -13 thanks to Juan Ignacio Díaz

Takes each power of 2 less than the input number and compares it to slices of the number.

```
j,k,m;f(i){for(m=j=1;j<i;j*=2)for(m*=j<m?:10,k=i;k;k/=10)k%m-j||printf("%d ",j);}

```

Try it online!

# Answer
> 3 votes
# Jelly, 15 bytes

```
DẆṖ×\S$ƇḌḤ’BȦƲƇ

```

Try it online!

## How it works

We use that fact that, if \\$x = 2^n\\$ for some \\$n \in \mathbb N \cup \\{0\\}\\$, then the binary representation of \\$2x-1\\$ consists of all \\$1\\$s. Note that the \\$2x\\$ is to handle \\$x = 1\\$.

```
DẆṖ×\S$ƇḌḤ’BȦƲƇ - Main link. Takes n on the left
D               - Digits of n
 Ẇ              - All substrings
  Ṗ             - Remove n
      $Ƈ        - Keep those with no leading zeros:
   ×\           -   Scan by product
     S          -   Resultant sum is zero
        Ḍ       - Cast to digits
             ƲƇ - Keep those that are a power of 2:
         Ḥ      -   Unhalve; Double
          ’     -   Decrement
           B    -   Binary
            Ȧ   -   All are non-zero?

```

# Answer
> 3 votes
# Brachylog, 14 bytes

```
~s.¬?h¬0&cḃ+1∧

```

Generator solution; takes the full number through its output parameter and yields a power-of-2 substring through its input parameter. Input and output are both lists of digits. Try it online!

### Explanation

```
~s.¬?h¬0&cḃ+1∧
                The input parameter (implicit)
~s              is a consecutive sublist of
  .             the output parameter
   ¬            which is not the same as
    ?           the input parameter
     h          whose first element
      ¬0        is not zero
        &       And, the input parameter
         c      concatenated together
          ḃ     converted to binary
           +    summed
            1   is 1
             ∧  Break unification of 1 with the output parameter

```

In other words, find a consecutive sublist of digits that satisfies the following conditions:

* It is not equal to the full list
* It does not start with 0
* When treated as a number and converted to binary, it is a 1 followed by some number of 0s

# Answer
> 3 votes
# , ~~124~~ ~~57~~ ~~71~~ ~~58~~ ~~55~~ 50 bytes

With the input `n` an integer:

```
function(n,m=2^{0:log2(n-.1)})m[sapply(m,grepl,n)]

```

Try it online!

Note: Even though `0:n` should work on principle, and thus gain 7 bytes against `0:log(n,2)`, it produces `NA` for most values of `n`...

# Answer
> 3 votes
# Retina 0.8.2, 64 bytes

```
(?!^|0)
$'¶
%`\B
¶$`
O#^$`
$.&
1A`
.+
$*
G`^((.+)(?=\2$))*.$
%`.

```

Try it online! Explanation:

```
(?!^|0)
$'¶
%`\B
¶$`

```

Generate all of the nontrivial substrings of the input integer that do not begin with `0`. (Retina 1 can do this using `Lw`[1-9].*`.)

```
O#^$`
$.&
1A`

```

Delete the longest such substring i.e. the original input. (Retina 1 would use `N` instead of `O#`.)

```
.+
$*

```

Convert to unary. (`$*` would be just `*` in Retina 1.)

```
G`^((.+)(?=\2$))*.$

```

Keep only powers of two. This works by matching half of the unary number each time until you get to the last `1` at the end. (Retina 1 matches differently and requires you add the `m` flag.)

```
%`.

```

Convert to decimal.

# Answer
> 2 votes
# Python 3.10.4 - 119 bytes

```
a=input()
for b in range(len(a)):
 for c in range(len(a)-b):
  n=int(a[c:c+b+1])
  if(n&(n-1)==0)and n!=0:
   print(n)

```

You type in the number when the program starts, we then iterate over all the possible substrings of that using this code snippet. Then we finally use this method to check if the given substring is a power of two, and if so, print it.

Note: when entering the number, be careful to not have an extra space on the end, as that will cause an error. Here's an example of me running it (on Arch Linux):

```
[me@computer ~]$ python golf.py
13248  <-- We enter that number ourself with the keyboard, the rest is automatically outputted
1
2
4
8
32

```

---

Note to OP:

In some cases (for example 10230), it outputs a number more than once:

```
$ python golf.py
10230
1
2
2

```

I assume that is fine, as it is a power of two, even if listed twice. If it isn't, I can update my answer

# Answer
> 2 votes
# Factor, ~~79~~ 76 bytes

```
[ all-subseqs 1 head* [ "0"head? ] reject [ dec> 1 - >bin all-eq? ] filter ]

```

Attempt This Online!

Takes input as a string. -3 bytes from tip by caird coinheringaahing.

* `all-subseqs` Get every contiguous subsequence of the input.
* `1 head*` Remove the last one (which is the same as the input).
* `[ "0"head? ] reject` Remove the ones that start with zero.
* `[ ... ] filter` Select every subsequence whose...
* `dec> 1 - >bin all-eq?` ...predecessor's binary digits are all the same.

# Answer
> 2 votes
# JavaScript (ES6), 79 bytes

Returns a space-separated string.

```
f=(s,n=1)=>n<s?(g=s=>s&&(s.match('^'+n)?n+' ':'')+g(s.slice(1)))(s)+f(s,n*2):''

```

Try it online!

# Answer
> 2 votes
# Pyth, 14 bytes

```
f!-PsT2g#\1P.:

```

Try it online!

Accepts a string as input, and returns a list of strings.

```
.:      -> All positive-length substrings of the (implicit) input
P       -> Remove the last substring (which is the entire string)
g#\1    -> Filter for substrings which start with "1" or greater
f       -> Filter for elements where...
   PsT  ->  ... its list of prime factors
 !-   2 ->  ... is empty when all 2s are removed

```

# Answer
> 2 votes
# Python 3.10 - 72 bytes

```
lambda x:sum(([w:=2**n]*(w<x)*str(x).count(str(w))for n in range(x)),[])

```

Can probably be golfed further - I initially misunderstood the question as wanting no duplicates (i.e. `322 -> [32, 2]`), but I think the structure that lead me to is fine. Also, in principle this will work for any input, but it takes several seconds to run on `13248` and will probably take longer than I care to wait on `655365536`.

# Answer
> 2 votes
# JavaScript, 70 bytes

```
f=(n,i=1)=>n>i?[...[...n.matchAll(`(?=${i})`)].fill(i),...f(n,i+i)]:[]

```

```
f=(n,i=1)=>n>i?[...[...n.matchAll(`(?=${i})`)].fill(i),...f(n,i+i)]:[]

console.log(JSON.stringify(f("10230"    ))) // [1, 2]
console.log(JSON.stringify(f("13248"    ))) // [32, 1, 2, 4, 8]
console.log(JSON.stringify(f("333"      ))) // []
console.log(JSON.stringify(f("331"      ))) // [1]
console.log(JSON.stringify(f("32"       ))) // [2]
console.log(JSON.stringify(f("322"      ))) // [32, 2, 2]
console.log(JSON.stringify(f("106"      ))) // [1]
console.log(JSON.stringify(f("302"      ))) // [2]
console.log(JSON.stringify(f("464"      ))) // [4, 4, 64]
console.log(JSON.stringify(f("655365536"))) // [65536, 65536]
```

Input a string, output array of integers.

# Answer
> 2 votes
# BQN, 29 bytes

```
•Fmt{∊⟜(¨⊸>⊸/2⋆↕)⊸/∾↑¨↓}

```

Try it here!

## Explanation

* `∾↑¨↓` substrings of input
* `∊⟜(...)⊸/` keep substrings that are found in the constructed list...
    + `⊸>⊸/2⋆↕` powers of 2 less than input
    + `¨` convert each element to string

---
Tags: code-golf, string, number, arithmetic
---


title: Consecutive coin flips

text:
# Question
Title: Consecutive coin flips
*This is a cross-post of a problem I posted to anarchy golf: http://golf.shinh.org/p.rb?tails*

Given two integers \\$ n \\$ and \\$ k \\$ \\$ (0 \le k \le n) \\$, count the number of combinations of \\$ n \\$ coin tosses with at least \\$ k \\$ tails in a row.

For example, if \\$ n = 3 \\$ and \\$ k = 2 \\$, the answer is \\$ 3 \\$:

```
HHH ❌
HHT ❌
HTH ❌
HTT ✔
THH ❌
THT ❌
TTH ✔
TTT ✔

```

**For convenience sake, you do not need to handle the case where \\$ n = k = 0 \\$**.

## Rules

* The input must take in two integers \\$ n \\$ and \\$ k \\$, and output the answer as a single integer
* Inaccuracies due to integer overflow are acceptable, but floating point errors are prohibited
* As usual for , the shortest code in bytes wins

## Test Cases

```
 n,  k  ->  ans
---------------
 1,  0  ->  2
 1,  1  ->  1
 2,  0  ->  4
 2,  1  ->  3
 2,  2  ->  1
 3,  1  ->  7
 3,  2  ->  3
 3,  3  ->  1
 5,  2  ->  19
 6,  4  ->  8
 9,  2  ->  423
12,  0  ->  4096
13,  5  ->  1262
14,  8  ->  256
16,  7  ->  2811

```

More terms are listed under OEIS A109435.

# Answer
> 12 votes
# Python, 50 bytes

```
lambda n,k:sum(k*"1"in bin(i)for i in range(2**n))
```

Attempt This Online!

Brute-force solution seems to be the shortest.

---

## Python, 67 bytes

```
lambda n,k:2**n-(g:=lambda a:a*k>0and(a<3or g(a-1)*2-g(a+~k)))(n+2)
```

Attempt This Online!

-11 bytes thanks to Neil.

More mathematical answer.

$$ f(n, k) = 2^n - k \operatorname{-bonacci}(n+k) \\ k \operatorname{-bonacci}(n) = \begin{cases} 0 & n \< k-1 \\ 1 & n = k-1 \\ \sum^k\_{i=1} k \operatorname{-bonacci}(n-i) & n \> k-1 \end{cases} $$

# Answer
> 8 votes
# x86-64 machine code, 28 bytes

```
31 C0 99 52 89 F1 FF C9 78 07 D1 EA 72 F8 75 F4 41 FF C0 5A FF C2 0F A3 FA 73 E8 C3

```

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes n in EDI and k in ESI and returns the answer in EAX.

In assembly:

```
f:  xor eax, eax
    cdq
a:  push rdx
b:  mov ecx, esi
c:  dec ecx
    js d
    shr edx, 1
    jc c
    jnz b
    .byte 0x41
d:  inc eax
    pop rdx
    inc edx
    bt edx, edi
    jnc a
    ret

```

# Answer
> 7 votes
# Vyxal `s`, ~~13~~ ~~9~~ 8 bytes

```
EƛbĠṠ⁰≥a

```

Try it Online!

A very neat little answer I'm very happy with.

## Explained

```
EƛbĠṠ⁰≥a
Eƛ       # to each item in the range [1, 2 ** N)
  bĠṠ    #   get the sums of consecutive digits of the binary representation of that number
     ⁰≥a #   are any of those greater than K? This is essentially getting all consecutive runs of flips and getting there length. Works because there's only 1s and 0s
# the s flag sums that list.

```

# Answer
> 6 votes
# JavaScript (ES7), 59 bytes

Expects `(n)(k)`.

Fully bitwise because binary string manipulation in JS is too verbose.

```
(n,i=1<<n)=>g=k=>i--&&(h=m=>i&m^m?h(m*2):m<=i)(2**k-1)+g(k)

```

Try it online!

---

# JavaScript (ES7), 38 bytes

*Saved 2 bytes thanks to @Neil*

Expects `(k)(n)`. Returns *true* for *1*.

Uses loopy walt's recursion formula.

```
k=>g=n=>n>k?2*g(--n)-g(n-=k)+2**n:n==k

```

Try it online!

# Answer
> 6 votes
# Python, 55 bytes

```
f=lambda n,k:n>=k and 2*f(n-1,k)-(-1<<n>>-~k)-f(n+~k,k)
```

Attempt This Online!

Slightly longer than @pxeger's Python answer but more efficient.

### How?

Uses the recurrence

\\$f(n,k)=2f(n-1,k)+2^{n-k-1}-f(n-k-1,k)\qquad(n\>k)\\$

which is obtained by counting words that have k consecutive 1's somewhere in the first n-1 places, words that end in a 0 followed by k 1's and the overlap of these two groups.

# Answer
> 5 votes
# PARI/GP, 40 bytes

```
k->g(n,r=k)=if(!r,2^n,n,g(n--,r--)+g(n))
```

Attempt This Online!

A port of @tsh's JavaScript answer.

---

# PARI/GP, 42 bytes

```
k->n->(y=1-x)/(y-=x)/(x+y/x^k)%x^(n+1)\x^n
```

Attempt This Online!

According to OEIS, the generating function for column \\$k\\$ is \\$\frac{x^k}{(1-\sum\_{i=1}^k x^i)(1-2x)}\\$, which could be simplified to \\$\frac{(1-x)}{(1-2x)(x+(1-2x)/x^k)}\\$.

# Answer
> 5 votes
# K (ngn/k), ~~21~~ 19 bytes

-2 bytes saved thanks to a suggestion by coltim on another answer.

Enumerates all binary length-n sequences and counts the ones with runs of at least k 1s.

```
{+/y<|/1(1+*)\!x#2}

```

Try it online!

# Answer
> 4 votes
# Factor \+ `sequences.repeating`, 58 bytes

```
[ "1"swap cycle swap 2^ iota [ >bin subseq? ] with count ]

```

Try it online!

---

Longer but much cooler:

# Factor, 67 bytes

```
[| n k | n 2^ ""n [ dup k short tail* sum suffix ] times last - ]

```

Try it online!

\\$2^n\\$ minus the \\$(n+k-1)\\$th k-bonacci number.

Example:

\\$n=6, k=4\\$

The tetranacci sequence begins: \\$0, 1, 1, 2, 4, 8, 15, 29, \color{red}{56}\\$

\\$2^6-56=8\\$

# Answer
> 4 votes
# Jelly, 8 bytes

```
1xẇⱮSɓ2ṗ

```

Try it online!

\\$k\\$ on the left, \\$n\\$ on the right.

```
   Ɱ        For each
     ɓ2ṗ    sequence of n 1s and 2s,
  ẇ         does it contain as a sublist
1x          k 1s?
    S       Sum.

```

# Answer
> 4 votes
# C (gcc), ~~89~~ 88 bytes

```
m;r;a;b;c;f(n,k){for(c=b=1<<n;b--;c-=m<k)for(a=b,m=r=0;a;m=r>m?r:m)r+=a&1?:-r,a/=2;m=c;}

```

Try it online!

*Saved a byte thanks to ceilingcat!!!*

# Answer
> 4 votes
**C** (standard compliant), 78 bytes

`int f(int n,int k,int c){return n?n+c-k?k-c?f(n-1,k,0)+f(n-1,k,c+1):1<<n:1:0;}`

For the readable version:

```
int f(int n,int k,int c){
    if(n==0)return 0; // no more coin flip left => no possible path
    if(n==(k-c))return 1; // last coin flip and 1 tail is lacking
    if(k==c)return 1<<n; // we have done k tails in a row, any path is a good path, and there are 2^n
    return f(n-1,k,0) // possible paths if we get a head
    +f(n-1,k,c+1); // possible paths if we get a tail
}

```

# Answer
> 3 votes
# APL(Dyalog Unicode), 28 bytes SBCS

```
{1⊥⍵≤⌈/∘(≢¨⊆⍨)⍤1⍉(⍺⍴2)⊤⍳2*⍺}

```

Try it on APLgolf!

Alternatively (same length): `{1⊥(∨/(⍵⍴1)⍷⊢)⍤1⍉(⍺⍴2)⊤⍳2*⍺}`

# Answer
> 3 votes
# Retina, 39 bytes

```
.+,(.+)
<$1*1>
"$+"+%`$
0$"1
<(.*)>.*\1

```

Try it online! No test suite due to the way the program uses history. Explanation:

```
.+,(.+)
<$1*1>

```

Replace the input with a string of `k` `1`s wrapped in `<>`s.

```
"$+"+

```

Repeat `n` times.

```
%`$
0$"1

```

Duplicate each row, appending `0` and `1` to each copy respectively.

```
<(.*)>.*\1

```

Count the number of rows where the binary portion contains `k` `1`s.

# Answer
> 3 votes
# , ~~72~~ 68 bytes

*Edit: -4 bytes thanks to @Dominic van Essen.*

```
\(n,k)sum(sapply(1:2^n-1,\(x,r=rle(intToBits(x)>0))any(r$l*r$v>=k)))
```

Attempt This Online!

# Answer
> 3 votes
# Retina 0.8.2, 94 bytes

```
.+,(.+)
1$&$*0;$&$*11;$1$*,1,
+`1;1*,(((1)|,)*)
;$1$#3$*1,
+`^(1+)0
$1$1
r`\2;;(1*,)*(1*),$

1

```

Try it online! Link includes test cases. Explanation:

```
.+,(.+)
1$&$*0;$&$*11;$1$*,1,

```

Create `2ⁿ` in binary, `n+1` in unary, then a list of `k` `0`s and a `1`.

```
+`1;1*,(((1)|,)*)
;$1$#3$*1,

```

Repeat `n+1` times, drop the first element of the list, and append the sum of the remaining elements. This calculates the `n+1`th k-bonacci number.

```
+`^(1+)0
$1$1

```

Convert the `2ⁿ` from binary into unary.

```
   r`\2;;(1*,)*(1*),$

```

Subtract the `n+1`th k-bonacci number from `2ⁿ`.

```
1

```

Convert to decimal.

Rather than trying to count the number of `n` coin tosses with at least `k` tails in a row, it's easier to consider the number of `n` coin tosses that do not have `k` tails in a row. By considering them grouped by the number of tails that they end with, it's possible to determine a recurrence relation for the number of rows. For example, with `k=3`:

* For `0` coin tosses, `1` ends in `0` tails, `0` end in `1` tails, and `0` end in `2` tails. Total `1` matching row.
* For `1` coin toss, `1` ends in `0` tails, `1` ends in `1` tails, and `0` end in `2` tails. Total `2` matching rows.
* For `2` coin tosses, `2` end in `0` tails, `1` ends in `1` tails, and `1` ends in `2` tails. Total `4` matching rows.
* For `3` coin tosses, `4` end in `0` tails, `2` end in `1` tails, and `1` ends in `2` tails. Total `7` matching rows.
* For `4` coin tosses, `7` end in `0` tails, `4` end in `1` tails, and `2` end in `2` tails. Total `13` matching rows.
* For `n` coin tosses:
    + The number that end in `0` tails is the total for `n-1` coin tosses, since the value is simply that plus an extra toss of heads.
    + The number that end in `1` tails is the number that end in `0` tails for `n-1` coin tosses, with each row having an extra toss of tails. However, this is also the total for `n-2` coin tosses.
    + The number that end in `2` tails is the number that end in `1` tails for `n-1` coin tosses, with each row having an extra toss of tails. Recursively, we see that this is also the total for `n-3` coin tosses.
    + The total is the sum of these which is also the total of the previous three totals. Thus, this forms a `3`-binacci sequence.

In general, the number of `n` coin tosses that do not have `k` tails in a row is a `k`-binacci sequence.

The number of `n` coin tosses with at least `k` tails in a row plus the number of `n` coin tosses that do not have `k` tails in a row is of course the total possible number of rows, `2ⁿ`.

# Answer
> 3 votes
# 05AB1E, 8 bytes

```
TIã$×δåO

```

Try it online or verify all test cases or try it step-by-step.

**Explanation:**

```
T        # Push 10
 I       # Push the first input `n`
  ã      # Take the cartesian product of the two, to create all possible n-sized strings
         # containing 0s and 1s
   $     # Push 1 and the second input `k`
    ×    # Repeat the 1 `k` amount of times as string
     δ   # Map over the list we created earlier, using this "1"-string as argument:
      å  #  Check if the "1"-string is a substring of the string we're mapping over
       O # Sum to get the amount of truthy values
         # (which is output implicitly as result)

```

# Answer
> 3 votes
# Pip, 14 bytes

```
0<1XbNTB_MS,Ea
```

Attempt This Online!

Brute force, same concept as pxeger's Python answer.

-5 bytes thanks to DLosc

# Answer
> 3 votes
# JavaScript (Node.js), 39 bytes

```
k=>g=(n,r=k)=>r?n&&g(--n,--r)+g(n):2**n

```

Try it online!

```
k=>    // Number of tails required in a row
g=(
  n,   // Number of coins we will flip
  r=k  // Number of tails still required in a row
)=>
  r?   // Do we still require tails?
   n&& // And we have more coins can flip?
    g(--n,--r)+ // Luckily, tail this time
    g(n):       // Nope, head this time
  2**n // We don't care what we get from remaining coins

```

# Answer
> 3 votes
# BQN, 20 bytes

```
{+´⥊∨´¨∧´˘¨⊸↕¨↕⥊2}

```

-1 byte thanks to @Razetime!

Try it here!

## Explanation

* `↕⥊2` generate binary sequences of length *n*
* `⊸↕¨` convert each binary sequence to subsequences of length *k*
    + example: `1010` with *k*=3 becomes `101, 010`
* `∨´¨∧´˘¨` AND each subsequence, then OR each result
* `+´⥊` sum to get total number of 1s

# Answer
> 2 votes
# Burlesque, 36 bytes

```
s11rzjCBg1{{gw:pd{-]g1>=}fl}fl}qL[IE

```

Try it online!

```
s1     # Save k
1rz    # [0, 1]
jCB    # Combinations up to n
g1     # Get k
{      # Catch k == 0
 {gw   # Group with length ([1, 1, 0] -> [[2, 1], [1, 0]])
  :pd  # Filter for tails
  {    # Filter
   -]  # Head
   g1  # k
   >=  # Greater than or equal
  }
  fl   # Count matches
 }
 fl    # Count matches
}
qL[    # Length of combinations (when k == 0)
IE     # If else

```

# Answer
> 2 votes
# Charcoal, 15 bytes

```
ＮθＩＬΦＸ²Ｎ№⍘ι²×θ1

```

Try it online! Link is to verbose version of code. Takes `k` as the first input and `n` as the second input. Explanation: Uses brute force, so takes exponential time in `n`.

```
Ｎθ              Input `k` as an integer
      ²         Literal integer `2`
     Ｘ          Raised to power
       Ｎ        Input `n` as an integer
    Φ           Filtered where
          ι     Current value
         ⍘      Converted to base
           ²    Literal integer `2`
        №       Count i.e. contains
              1 Literal string `1`
            ×   Repeated by
             θ  Input `k`
   Ｌ            Take the length
  Ｉ             Cast to string
                Implicitly print

```

27 bytes for an efficient version (approximately linear time):

```
ＮθＮη⊞υ¹Ｆ⊕θ⊞υ↨¹✂⮌υ⁰η¹Ｉ⁻Ｘ²θ⊟υ

```

Try it online! Link is to verbose version of code. Takes `n` as the first input and `k` as the second input. Explanation:

```
ＮθＮη

```

Input `n` and `k`.

```
⊞υ¹

```

Start with the zeroth term of the k-bonacci sequence.

```
Ｆ⊕θ⊞υ↨¹✂⮌υ⁰η¹

```

Calculate `n+1` additional terms.

```
Ｉ⁻Ｘ²θ⊟υ

```

Subtract the `n+1`th term from `2ⁿ`.

# Answer
> 2 votes
# Perl 5 `-pl`, 35 bytes

```
$b=<>;$_=grep/T{$b}/,glob"{H,T}"x$_

```

Try it online!

# Answer
> 2 votes
# MATLAB, 71 bytes

```
function f(n,k),sum(contains(string(dec2bin(0:2^n-1)),repmat('1',1,k)))

```

The binary representation of all numbers from 0-2^n-1 will give each combination of coin flips. From that, I can search the resulting string vector for repeated instances of 1 ( my choice for tails) that are at least k long and sum the logical array for the result.

Try it online!

# Answer
> 2 votes
# Excel, 49 bytes

```
=COUNT(FIND(REPT(1,B2),BASE(SEQUENCE(2^A2)-1,2)))

```

Input for *n* is in `A2` and *k* is in `B2`.

* `SEQUENCE(2^A2)-1` creates an array from 0 to (2^n)-1.
* `BASE(~,2))` converts that array to binary.
* `REPT(1,B2)` creates a string of *k* 1s.
* `FIND(REPT(~),BASE(~))` tries to find that repeated string in each binary element in the array. This returns an array of integers (if it's found in that element) and errors (if it's not).
* `COUNT(FIND(~))` counts the numbers in that array.

---
Tags: code-golf, number-theory, combinatorics
---


# Question
Title: Position from atomic number in the periodic table
Let us consider the following representation of the periodic table.

```
     __________________________________________________________________________
    | | 1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18 |
    |--------------------------------------------------------------------------|
    |1|  1                                                                   2 |
    | |                                                                        |
    |2|  3   4                                           5   6   7   8   9  10 |
    | |                                                                        |
    |3| 11  12                                          13  14  15  16  17  18 |
    | |                                                                        |
    |4| 19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36 |
    | |                                                                        |
    |5| 37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54 |
    | |                                                                        |
    |6| 55  56      72  73  74  75  76  77  78  79  80  81  82  83  84  85  86 |
    | |                                                                        |
    |7| 87  88     104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
    | |                                                                        |
    |8|             57  58  59  60  61  62  63  64  65  66  67  68  69  70  71 |
    | |                                                                        |
    |9|             89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 |
    |__________________________________________________________________________|

```

# Task

Produce a program that takes an atomic number as input, and outputs the row and column of the element of given atomic number.

For instance, giving the atomic number `1` should produce `1 1` (or `1, 1`, or anything that looks like a vector of two 1s).

# Details

The representation of the periodic table may be represented in various way. The one presented in this challenge does have the following property : Lantanides and Aktinoides are *all* in a dedicated row, hence there is no element that is placed at `6, 3` nor `7, 3`.

You may take a look at the atomic number repartitions here.

The atomic number is at least 1, at most 118.

# test cases

* `1` -\> `1, 1`
* `2` -\> `1, 18`
* `29` -\> `4, 11`
* `42` -\> `5, 6`
* `58` -\> `8, 5`
* `59` -\> `8, 6`
* `57` -\> `8, 4`
* `71` -\> `8, 18`
* `72` -\> `6, 4`
* `89` -\> `9, 4`
* `90` -\> `9, 5`
* `103` -\> `9, 18`

# Scoring

This is code golf, standard rules applies, hence fewer bytes win.

# Answer
> 5 votes
# JavaScript (ES6), 79 bytes

```
n=>[(n+=--n?n<4?33:n<12?43:n<56?53:n<71?90:n<88?39:n<103?76:25:17)/18|0,n%18+1]

```

Try it online! (raw output)

Try it online! (builds the periodic table)

Or **76 bytes** if 0-indexing is acceptable:

```
n=>[(n+=--n?n<4?15:n<12?25:n<56?35:n<71?72:n<88?21:n<103?58:7:-1)/18|0,n%18]

```

Try it online!

# Answer
> 5 votes
# JavaScript, 79 bytes

Inspired by Arnauld's answer. I tried to golf it more, and ended up with this.

```
n=>[(n+=[113,417,1195,5429,6906,8487,9964].find(x=>n<x/96)%96||25)/18|0,1+n%18]

```

Try it online!

The outside is the same as Arnauld's answer, but the lookup for what to offset `n` by is different.

If we create a table the same size as the periodic table, where each item is equal to `column + 18 * row - 1 - Z` (where `Z` is the atomic number at that position), we can see some groups emerge:

```
  ||   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
==||=========================================================================
1 ||  17 |                                                                33
  ||-----+                                       +---------------------------
2 ||  33  33                                     |    43  43  43  43  43  43
  ||---------------------------------------------+  +------------------------
3 ||  43  43                                        | 53  53  53  53  53  53
  ||------------------------------------------------+
4 ||  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53
  ||
5 ||  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53  53
  ||         +---------------------------------------------------------------
6 ||  53  53 |    39  39  39  39  39  39  39  39  39  39  39  39  39  39  39
  ||---------+  +------------------------------------------------------------
7 ||  39  39    | 25  25  25  25  25  25  25  25  25  25  25  25  25  25  25
  ||------------+------------------------------------------------------------
8 ||              90  90  90  90  90  90  90  90  90  90  90  90  90  90  90
  ||-------------------------------------------------------------------------
9 ||              76  76  76  76  76  76  76  76  76  76  76  76  76  76  76
  ||

```

Once we have the right group, we can just add its value to `Z`, then the row will be `(new) Z / 18`, the column `Z % 18 + 1`.

The lookup table `[113,417,1195,5429,6906,8487,9964]` will be more obvious in this solution with a factor of `100`, instead of `96`:

```
n=>[(n+=[117,433,1243,5653,7190,8839,10376].find(x=>n<x/100)%100||25)/18|0,1+n%18]

```

Each item `i` in the list `[117,433,1243,5653,7190,8839,10376]` corresponds to one of the groups such that the group value is `i % 100` and all atomic numbers in the group are less than `i / 100`. I reduced the factor to `96` to save bytes.

---

## Old invalid answer (0-indexed), 76 bytes

```
n=>[(n+=[82,341,999,4573,5825,7151,8403].find(x=>n<x/81)%81-2||7)/18|0,n%18]

```

Try it online!

# Answer
> 4 votes
# 05AB1E, 32 bytes

```
T•3¾§Ue•44вÝ•¨²>Ùö€•Ƶlв+ª˜18‰>Iè

```

Try it online or verify all test cases. (You can add `--debug-stack` as argument to the singular TIO to see all individual steps.)

**Explanation:**

```
T          # Push 10
 •3¾§Ue•   # Push compressed integer 15829433590
  44в      # Convert it to base-44 as list: [2,7,43,14,16,14,14]
     Ý     # Convert each inner value to a list in the range [0,value]
 •¨²>Ùö€•  # Push compressed integer 180810357611003
  Ƶl       # Push compressed integer 148
    в      # Convert to base-148 as list: [17,30,48,129,93,147,111]
     +     # Add these to each value of the inner lists at the same positions
 ª         # Append this list of lists, which will first implicitly converts
           # the initial 10 to list [1,0]
  ˜        # Flatten it
   18‰     # Divmod-18 each inner integer
      >    # Increase each integer in each pair by 1
       Iè  # 0-based index the input into this list of pairs
           # (after which the result is output implicitly)

```

See this 05AB1E tip of mine (sections *How to compress large integers?* and *How to compress integer lists?*) to understand why `•3¾§Ue•` is `15829433590`; `•3¾§Ue•44в` is `[2,7,43,14,16,14,14]`; `•¨²>Ùö€•` is `180810357611003`; `Ƶl` is `148`; and `•¨²>Ùö€•Ƶlв` is `[17,30,48,129,93,147,111]`.

# Answer
> 4 votes
# x86 32-bit machine code, ~~39~~ 36 bytes

```
99 42 A9 FE 42 3C 02 76 1A 8D 4A 02 D1 E9 0F AF C9 D1 E1 28 C8 77 ED 4A 42 2C F1 7F 04 42 7B F8 4A 04 03 C3

```

Try it online!

This takes a 32-bit integer in EAX, following the `regparm(1)` convention. It returns two 32-bit integers in EAX and EDX; this document seems to say that this would be the standard way of returning a structure of two 32-bit integers, but it apparently doesn't actually work that way (or am I misreading that document?), which is why the linked TIO program is coded differently.

-3 by calculating line lengths instead of hardcoding them.

In assembly:

```
f:  cdq
    inc edx
    .byte 0xA9, 0xFE
a:  inc edx
    cmp al, 2
    jbe e
    lea ecx, [edx + 2]
    shr ecx, 1
    imul ecx, ecx
    shl ecx, 1
s:  sub al, cl
    ja a
    dec edx
b:  inc edx
    sub al, -15
    jg t
    inc edx
    jpo b
    dec edx
t:  add al, 3
e:  ret

```

The main part of the code works as follows:

* If AL ≤ 2 (in the first two columns), jump straight to the end.
* Calculate the length of the current line as \\$ \lfloor\frac{\textrm{line number} + 2}{2}\rfloor^2\cdot 2\\$ and subtract it from AL.
* If the result is positive, jump back to repeat, moving to the next line.
* Otherwise, add a net +18 to AL and return.

Special cases are handled as follows:

* `sub al, -15` is used to check for being in the extracted block and reused to adjust the horizontal position for elements in that block.
* That check gives a false positive for elements 21 and 39 (the only ones in column 3). That is corrected for by looking at the parity flag after `inc edx`; the incremented value is 5 (1012) or 6 (1102), with even parity, for elements 21 and 39, whereas it is 7 (1112) or 8 (10002), with odd parity, for elements that do belong in the extracted block.
* Helium (2) is dealt with by `.byte 0xA9, 0xFE`, which subsumes the following two instructions into a `test eax, 0x023C42FE`. The following `be` condition is (CF or ZF); CF is always 0 after a `test`, and in this case, ZF is 1 only for element 1.

# Answer
> 3 votes
# Python, 124 bytes

```
def f(n):
    for m,z in zip(b'hYWH97%\x13\r\x0b\5\3\2\1',b'*,\x0f)+\x0e\r\x0cw\x0bv\n\xa2\t'):
        if m<=n:return z%9+1,z//9+n-m
```

Attempt This Online!

# Answer
> 2 votes
# Charcoal, 50 bytes

```
ＮθＩＥ↨⁺θ⊟⊟ΦＩ⪪⪪”←＆“▷/»h⦄¤Ａh)｜J\-w~~∕↔AＸ”¶¦ ‹⊟ιθ¹⁸⁺ικ

```

Try it online! Link is to verbose version of code. Explanation: Port of @Arnauld's JavaScript answer.

```
Ｎθ                              Input as a number
             ”...”              Compressed lookup table
            ⪪     ¶             Split on newlines
           ⪪                    Split each element on space
          Ｉ                     Cast to integer
         Φ                      Filtered where
                       ι        Current pair
                      ⊟         Remove last element
                     ‹          Is less than
                        θ       Input number
        ⊟                       Get last matching element
       ⊟                        Get first element
     ⁺                          Plus
      θ                         Input number
    ↨                           Base conversion (i.e. divmod)
                         ¹⁸     Literal integer `18`
   Ｅ                       ⁺ικ  Increment the second element
  Ｉ                             Cast to string
                                Implicitly print

```

# Answer
> 1 votes
# Rust Nightly, 239 bytes + `#![feature(exclusive_range_pattern)]`

```
|p:u32|match p{1=>(1,1),2=>(18,1),3|4|11|12=>(p%8-2,p/8+2),5..11|13..19=>((p-3)%8+11,(p-3)/8+2),19..57=>((p-1)%18+1,(p-1)/18+3),72..87|104..119=>((p-25)%32-11,(p-25)/32+5),87|88=>(p-86,7),57..72|89..104=>((p-25)%32+4,(p-25)/32+7),_=>(2,1)}

```

Basically just matches in some squares then uses some division and mod to format the remaining numbers.

Playground

The actual original data is 236 bytes (118 * 2) so this code is actually barely longer than the data it's trying to encode. I tried I guess :/

# Answer
> 1 votes
# Vyxal, 32 bytes

```
₀f»#tṪİ□»₆τʀ»₃¡s↳‡∴»⁺/τ+Jf18vḋ›i

```

Try it Online!

Port of 05AB1E.

## How?

```
₀f»#tṪİ□»₆τʀ»₃¡s↳‡∴»⁺/τ+Jf18vḋ›i
₀f                               # Push ten converted to a list of digits: [1, 0]
  »#tṪİ□»                        # Push compressed integer 145680302990
         ₆τ                      # Convert to base-64 as list: [2, 7, 43, 14, 16, 14, 14]
           ʀ                     # Convert each to a range [0, item]
            »₃¡s↳‡∴»             # Push compressed integer 180810357611003
                    ⁺/τ          # Convert to base-148 as list: [17, 30, 48, 129, 93, 147, 111]
                       +         # Add the values in these two lists at the same positions
                        J        # Join the [1, 0] and this list together
                         f       # Flatten this list
                          18vḋ   # Divmod each by 18
                              ›  # Increment each
                               i # 0-based index the input into this list

```

# Answer
> 1 votes
# , 113 bytes

```
\(x,p=matrix(c(1,0:-15,2:4,0:-9,5:12,0:-9,13:56,0,72:88,0,104:118,0:-2,57:71,0:-2,89:103),9,,T))which(p==x,T)[1,]
```

Attempt This Online!

Creates the matrix, then looks up values. Trying to redefine `+` so I can write

```
\(x,`+`=\(x)0:-x,p=matrix(c(1,+15,2:4,+9,5:12,+9,13:56,0,72:88,0,104:118,+2,57:71,+2,89:103),9,,T))which(p==x,T)[1,]

```

Is 116 characters.

# Answer
> 1 votes
# Ruby, 79 bytes

```
->n{j=0
"hilt8GXg".bytes{|i|n>i%104&&M=n+"+^9lG=3#"[j-=1].ord}
[M/18-1,M%18+1]}

```

Try it online!

Uses 1 based indexing per the question, and comes out the same length as a couple of other answers.

The approach is similar to Arnauld's but I use a magic string and loop to iterate through all the discontinuities. The character is taken `mod 104` to be able to cover all the values from 0 to 103 without the need to use nonprintable characters.

The raw output is stored in `M` (use a capital letter for a consonant, to avoid Ruby saying it can't find a variable that was defined inside a loop.) A second magic string is used to look up the correct offset.

The output is given as row `M/18-1` (the `-1` is used to avoid offsets smaller than ASCII 32 in the magic string) and column `M%18+1` (`+1` is used because the columns are 1-indexed.)

---
Tags: code-golf, kolmogorov-complexity, chemistry, physics
---