# “Atomic” code golf - determine the number of neutrons in an isotope

Your task is to take an string containing an isotope of an element as input, encoded like the following example with the atomic number followed by a space and the IUPAC chemical symbol for the element:

162 Dy


and return the number of neutrons in an atom of that isotope.

In the above example, dysprosium-162 has 96 neutrons (162 total nucleons, minus 66 protons because it's dysprosium), so the output should be 96.

You may assume that the element given will be one of the 114 elements currently given permanent names by the IUPAC (including flerovium and livermorium) and not a generic name such as Uus for "ununseptium". You may also assume that the atomic number of the isotope will not exceed 1000, or be less than the number of protons in the element.

You may not use built-ins to retrieve data about the proton or neutron number of elements, or use any function within your code that evaluates a string or number token as code on its own.

The program to use the fewest tokens to do this in any language wins. However, for the purpose of this challenge, every character in a string, or a variable name converted into a string, counts as a token.

List of elements and their atomic number for reference:

{
"H": 1,
"He": 2,
"Li": 3,
"Be": 4,
"B": 5,
"C": 6,
"N": 7,
"O": 8,
"F": 9,
"Ne": 10,
"Na": 11,
"Mg": 12,
"Al": 13,
"Si": 14,
"P": 15,
"S": 16,
"Cl": 17,
"Ar": 18,
"K": 19,
"Ca": 20,
"Sc": 21,
"Ti": 22,
"V": 23,
"Cr": 24,
"Mn": 25,
"Fe": 26,
"Co": 27,
"Ni": 28,
"Cu": 29,
"Zn": 30,
"Ga": 31,
"Ge": 32,
"As": 33,
"Se": 34,
"Br": 35,
"Kr": 36,
"Rb": 37,
"Sr": 38,
"Y": 39,
"Zr": 40,
"Nb": 41,
"Mo": 42,
"Tc": 43,
"Ru": 44,
"Rh": 45,
"Pd": 46,
"Ag": 47,
"Cd": 48,
"In": 49,
"Sn": 50,
"Sb": 51,
"Te": 52,
"I": 53,
"Xe": 54,
"Cs": 55,
"Ba": 56,
"La": 57,
"Ce": 58,
"Pr": 59,
"Nd": 60,
"Pm": 61,
"Sm": 62,
"Eu": 63,
"Gd": 64,
"Tb": 65,
"Dy": 66,
"Ho": 67,
"Er": 68,
"Tm": 69,
"Yb": 70,
"Lu": 71,
"Hf": 72,
"Ta": 73,
"W": 74,
"Re": 75,
"Os": 76,
"Ir": 77,
"Pt": 78,
"Au": 79,
"Hg": 80,
"Tl": 81,
"Pb": 82,
"Bi": 83,
"Po": 84,
"At": 85,
"Rn": 86,
"Fr": 87,
"Ra": 88,
"Ac": 89,
"Th": 90,
"Pa": 91,
"U": 92,
"Np": 93,
"Pu": 94,
"Am": 95,
"Cm": 96,
"Bk": 97,
"Cf": 98,
"Es": 99,
"Fm": 100,
"Md": 101,
"No": 102,
"Lr": 103,
"Rf": 104,
"Db": 105,
"Sg": 106,
"Bh": 107,
"Hs": 108,
"Mt": 109,
"Ds": 110,
"Rg": 111,
"Cn": 112,
"Fl": 114,
"Lv": 116
}

• Ahaha right, just read the tag wiki. Seems like I misunderstood what the tag encompassed. – Sp3000 Dec 10 '14 at 4:53
• In general, atomic code golf is better than code golf in terms of fairness between languages because it doesn't favour golfing-specific languages like CJam or Golfscript. – Joe Z. Dec 10 '14 at 5:56
• Are numbers considered as single token ? Like in my answer, is 65 a single token or 2 tokens ? – Optimizer Dec 10 '14 at 8:09
• "In general, atomic code golf is better than code golf in terms of fairness between languages because it doesn't favour golfing-specific languages like CJam or Golfscript." Or does it? ;) – Martin Ender Dec 10 '14 at 9:25
• If built-ins were allowed, the following function, written in Mathematica, might be used: f[i_] := {n = ElementData[#[[2]], ToString@"StandardName"] <> ToString[#[[1]]], IsotopeData[n, "NeutronNumber"]} &[i] – DavidC Dec 10 '14 at 15:29

# CJam, 120 6 or 116 tokens

If numbers are single token, then we have the whole code as 6 tokens:

212065132249371573644124577121541653292994215804249878509033804566332949723850494006885998432589817663973272353884582668784347806472090795216938149345570424681103578762377771127645936569136562173556524971260383556793671988140498193154122873972644190003488894117491373991987567199279175132071186860926217964652052981868515510731685503564515183920386542420190587555479397630718313762477968862282786518176572529906744217608955037610524188966159703528812103601771438848515748286560373093942224945233978303729665751831532717 128b:c~


where the tokens are

212065132249371573644124577121541653292994215804249878509033804566332949723850494006885998432589817663973272353884582668784347806472090795216938149345570424681103578762377771127645936569136562173556524971260383556793671988140498193154122873972644190003488894117491373991987567199279175132071186860926217964652052981868515510731685503564515183920386542420190587555479397630718313762477968862282786518176572529906744217608955037610524188966159703528812103601771438848515748286560373093942224945233978303729665751831532717
128
b
:
c
~


This code is equivalent to the below code containing the string version of the big number in the above code. But due to a string, the below code has 116 tokens:

ri"ᘭ᛭绊ڏ晍嬨塐弶⛡ᠸ庐ᖩે槑湘ࡊ㚋䊌栕ᄂỗ∘抁埵ໂČ槩唹ᘇ穗≧ṷ㴛勤烓≿Ⲳ㇭Ȋ嬅͙獚簜䱡数㍉㉦䩛爈拴矍㚴燌㾄䱮⃜⢴ⶏ㯗႒ݘ੡䅄瞟⮘㢧⳻⮵∼䚽珯ほֹ㳰櫣ݰ牜᫦殙ᆌ穟䖻ᄭⓚ獙஧༧撒咛啺"2F#b57b65f+:cr2*2<#2/)-


Tokens (with explanation) are

r                           "Read the first input";
i                           "and convert it to integer";
"
91 character string         "Then this base converted string";
"
2
F
#                           "'s ASCII representation of each character gets converted";
b                           "to base 2**15";
57                          "which gets converted to";
b                           "base 57";
65                          "and we add 65 to each element in the base 57 array";
f
+
:
c                           "and convert each array element to character.";
r                           "Then read the next input string, which is atom's IUPAC";
2                           "double it";
*
2                           "and take only first 2 characters";
<
#                           "Find the occurrence of these 2 characters in the big string";
2                           "and divide the index by 2";
/
)                           "increment the index to counter 0 offset";
-                           "and subtract this number, which is the atomic number from"
"the input number of nucleons";


This can be reduced to 109 tokens too, but then SE won't let me upload my answer, throwing malformed URI exception.

Input goes like

162 Dy


output is like

96


The weird string is just a base encoded string containing all IUPAC names at their Atomic number * 2 - 1 index.

Try it online here

• Why would numbers not be a single token? – Martin Ender Dec 10 '14 at 9:23
• @MartinBüttner Well, if for the purpose of this question, strings are not single token, then numbers might not be too :) – Optimizer Dec 10 '14 at 9:24
• This gives the wrong the results for Boron and Indium, because two-element letters starting with B and I appear before them. – Martin Ender Dec 10 '14 at 10:30
• ^ You mean boron and iodine. Indium is In. – Joe Z. Dec 10 '14 at 19:06
• Also, guys. That bug for B and I is fixed. – Optimizer Dec 10 '14 at 19:08

# Python 3 with exec, 17 tokens

exec(int.to_bytes(42580960806925240587487231677747050990110980939298529158008049507419456038066480774222358994792932281429500848123044123619998194774734911333011516763318834841258668032468977581617546825403043048781904307873076644287421190283925612029151422009703963147720234582458918676020358978146687598642493196719470433413287097024943497230356536978257362073205770196031226838532057690859535911353521203287284228407660035870497366713816359382867026152168356178620422021081074864815228071041303891869741111572003521808946355179139580269537828514345177247630946236685801543450404664783011350766913659964138280312012942354586269107632396118108534925651704031851802293836135007879834261627022944650861299698061444211422731907625,295,'big'))


# Python 3 without exec, 35 tokens

m,n=input().split()


# Ruby with eval, 17 tokens

eval [22146635005300445083784033446026580324048447941091204274213253110537099437818224958820496527991920943430421799402248351995854377736142191462062582991150146209770141259010870255095388946677505144127700666745571877848513432112199556183753919673308110291261587736766001750331575554182707626697924148465335917814388410868486650419909619279250674754531982074694183257024218097391705830277480110741636037821082572926228904583257826932150641336017429157246896262085081972165351023141358378905645164343005169041637147077645200273099823888392038731180787177889720531999846231330677441270444855911286469030157575699579420898896870179419644019549285098577609138033580761786048462145007410.to_s(16)].pack('H*')


# Javascript, 49 tokens

m=prompt().split(/ /);
_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_Md_No_Lr_Rf_Db_Sg_Bh_Hs_Mt_Ds_Rg_Cn_Uut_Fl_Uup_Lv_
}).toString().split(/_/).indexOf(m[1]))


Just to illustrate Count big-ints/symbols/variables as N tokens in atomic-code-golf :)

• Does disallowing eval and exec work against that? – Joe Z. Dec 10 '14 at 16:48
• @JoeZ. Still vulnerable to "big integer lookup table" attack, see update. – kennytm Dec 10 '14 at 18:57
• It seems like this problem was ill-formed from the start, then. – Joe Z. Dec 10 '14 at 19:05

## Javascript, 42 tokens (?)

alert(parseInt(x=prompt().split(" "))-Object.getOwnPropertyNames({__H_HeLiBeB_C_N_O_F_NeNaMgAlSiP_S_ClArK_CaScTiV_CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY_ZrNbMoTcRuRhPdAgCdInSnSbTeI_XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW_ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaU_NpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCn__Fl__Lv:0})[0].indexOf(x[1])/2)


PS: Where I can find some script to count tokens?