19
\$\begingroup\$

Each element on the periodic table of the elements has an atomic weight. For example, boron (element 5) has an atomic weight of 10.81. Your challenge is to write a program which takes as input the atomic number of an element and outputs its atomic weight.

Your program only needs to support elements up to and including element 118 (oganesson), the current (as of December 2022) highest known element.

Your score is calculated using the formula:

\$(1+b)\times(1+m)\$

where \$b\$ is your program's length in bytes and \$m\$ is the mean-squared error of your estimates.

List of weights

These are the weights your program must output for a given atomic number. If I input 1 I should get 1.0079, 2 should give me 4.0026, etc.

1.0079
4.0026
6.941
9.0122
10.811
12.0107
14.0067
15.9994
18.9984
20.1797
22.9897
24.305
26.9815
28.0855
30.9738
32.065
35.453
39.948
39.0983
40.078
44.9559
47.867
50.9415
51.9961
54.938
55.845
58.9332
58.6934
63.546
65.39
69.723
72.64
74.9216
78.96
79.904
83.8
85.4678
87.62
88.9059
91.224
92.9064
95.94
98
101.07
102.9055
106.42
107.8682
112.411
114.818
118.71
121.76
127.6
126.9045
131.293
132.9055
137.327
138.9055
140.116
140.9077
144.24
145
150.36
151.964
157.25
158.9253
162.5
164.9303
167.259
168.9342
173.04
174.967
178.49
180.9479
183.84
186.207
190.23
192.217
195.078
196.9665
200.59
204.3833
207.2
208.9804
209
210
222
223
226
227
232.0381
231.0359
238.0289
237
244
243
247
247
251
252
257
258
259
262
261
262
266
264
277
268
281
282
285
286
289
290
293
294
294

The scoring calculation was last updated December 15th 2022 at 5:50 pm UTC. Any answers posted before then use an old version of the scoring calculation, and, while not required, are encouraged to update their scores.

\$\endgroup\$
5
  • 6
    \$\begingroup\$ No scoring script? \$\endgroup\$
    – Neil
    Commented Dec 15, 2022 at 20:30
  • 1
    \$\begingroup\$ The atomic weight depends on the isotope distribution. Where does the list of values come from? E.g., for carbon, it is different in the upper atmosphere and 10 km below ground. For oxygen water/snow, it depends on the temperature. \$\endgroup\$ Commented Dec 16, 2022 at 21:36
  • 1
    \$\begingroup\$ @PeterMortensen The atomic weights used for this challenge most be the ones included in the post. \$\endgroup\$
    – Ginger
    Commented Dec 17, 2022 at 13:34
  • 2
    \$\begingroup\$ @Ginger can you provide a source for your weights? It seems to disagree with the mathematica built-in, which is interesting \$\endgroup\$ Commented Dec 18, 2022 at 9:44
  • 3
    \$\begingroup\$ @infinitezero I got it from RadvylfPrograms, so you'll have to ask him about it. Personally, I think the built-in not working is a feature, not a bug :p \$\endgroup\$
    – Ginger
    Commented Dec 18, 2022 at 22:39

17 Answers 17

12
\$\begingroup\$

Wolfram Language (Mathematica), score \$(1+29)(1+2.52543) = 105.763\$

#~ElementData~"AtomicWeight"&

If the builtin fits…! Doesn't work on tio.run because it requires access to Wolfram's servers or something like that. Gives each output as a quantity-with-units rather than a raw number.

For whatever reason, Mathematica has different ideas about the atomic weights of elements #104–109 than the OP, differing by 6, 8, 3, 6, 7, and 10, respectively. Without those errors, this function's score would be just over 31.

I love the fact that CursorCoercer's answer beats this builtin!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ The international union of pure & applied chemistry appears to agree with Mathematica here... link \$\endgroup\$ Commented Dec 16, 2022 at 9:03
11
\$\begingroup\$

Nibbles, 3 6 bytes (12 nibbles), score 118.37 85.62

*^^$41-39~
  ^$41          # input to 41st power
 ^    -39       # get integer value of 39th root
*        ~      # and multiply by 2

Mean-square-error = 11.23

enter image description here


Nibbles, 67 bytes (134 nibbles), score 70.26

Uninventive port of MarcMush's 119-byte answer (now improved; see edits): upvote that instead of this. See screenshot here.

-/*13$5=$`D16 110123444657688996ac998aabbdbdaaa9b9bacdddddedeccba7a9a89accdbb9a999a999889787876669b1324251404253534659b9e3f5667798ac
\$\endgroup\$
10
\$\begingroup\$

Julia, score 139 129 115.83

\$ score = (1+97) \cdot (1+0.1820) = 115.8387 \$

x->2.6x-b"$/;GG=_HS_k`TITTklllb`>IIS`VJIHIHH=<=0G$/FI*+0<Hj"[-~x>>1]÷11^(-~x%2)%11*1.5

Attempt This Online!

the error of x->2.6x, devided by 1.5 and rounded to the nearest integer is comprised between 0 and 10. This allows to store 2 errors in a ASCII character (0-127) like a base-11 number. This way, all errors are less than 0.75, giving a quite good mean squared error (0.18)

previous answers:

\$\endgroup\$
9
\$\begingroup\$

Jelly, 3 bytes, score 338.70775620762714

5H×

Try it online!

Multiplies the input by \$\frac{5}{2}\$

\$\endgroup\$
9
\$\begingroup\$

Pyth, score = \$(1+6) \cdot (1+13.1372) \approx 98.9605\$

y*@Q20

Try it online!

Uses the formula \$ 2\cdot n^{1.05} \$. Verify the score and compare to the integer divide by two, multiply by 5 (would be 4 bytes in pyth) method on Desmos

Explanation

y*@Q20Q    # Add implicit Q
           # Q = eval(input()) implicitly
y          # 2 times
 *    Q    # Q times
  @Q20     # the 20th root of Q
\$\endgroup\$
7
\$\begingroup\$

Python 3, score = \$(1 + 789) * (1 + 0) = 790\$

lambda n:[1.0079,4.0026,6.941,9.0122,10.811,12.0107,14.0067,15.9994,18.9984,20.1797,22.9897,24.305,26.9815,28.0855,30.9738,32.065,35.453,39.948,39.0983,40.078,44.9559,47.867,50.9415,51.9961,54.938,55.845,58.9332,58.6934,63.546,65.39,69.723,72.64,74.9216,78.96,79.904,83.8,85.4678,87.62,88.9059,91.224,92.9064,95.94,98,101.07,102.9055,106.42,107.8682,112.411,114.818,118.71,121.76,127.6,126.9045,131.293,132.9055,137.327,138.9055,140.116,140.9077,144.24,145,150.36,151.964,157.25,158.9253,162.5,164.9303,167.259,168.9342,173.04,174.967,178.49,180.9479,183.84,186.207,190.23,192.217,195.078,196.9665,200.59,204.3833,207.2,208.9804,209,210,222,223,226,227,232.0381,231.0359,238.0289,237,244,243,247,247,251,252,257,258,259,262,261,262,266,264,277,268,281,282,285,286,289,290,293,294,294][n-1]

Try it online!

Prints the exact values.

Python 3, score = \$(1 + 16) * (1 + 12.83) \approx 235.18\$

lambda n:2.6*n-8

Try it online!

Verify the score here

  • Thanks to MarcMush for cutting down the score from 473 to 237.
  • Thanks to Neil for cutting down the score from 237 to 235.
\$\endgroup\$
5
  • 1
    \$\begingroup\$ score of 237 with lambda n:2.6*n-9 \$\endgroup\$
    – MarcMush
    Commented Dec 16, 2022 at 13:00
  • \$\begingroup\$ @MarcMush thanks! \$\endgroup\$
    – The Thonnu
    Commented Dec 16, 2022 at 13:06
  • 1
    \$\begingroup\$ @MarcMush lambda n:2.6*n-8 is only 235, I think? \$\endgroup\$
    – Neil
    Commented Dec 17, 2022 at 13:23
  • \$\begingroup\$ @Neil thanks, updated. \$\endgroup\$
    – The Thonnu
    Commented Dec 17, 2022 at 14:34
  • \$\begingroup\$ @Neil thanks, I don't know how I didn't try that. I updated my answer too \$\endgroup\$
    – MarcMush
    Commented Dec 17, 2022 at 15:48
7
\$\begingroup\$

MATLAB, score 144.247 (from @Neil)

@(n)1.6*n^1.1

MATLAB, score 161.092

@(n)1.7*n^1.087

Using the best fit for \$a*n^b\$.


MATLAB, score 206.683

@(n)n.^(0:3)*[5/4;1.77;.0154;-7.77e-5]

Using the best fit for \$a+bn+cn^2+dn^3\$.

\$\endgroup\$
2
  • \$\begingroup\$ What does @(n)1.6*n^1.1 score? (I think it scores 144 but then again I think @(n)1.7*n^1.0876 scores 182, so...) \$\endgroup\$
    – Neil
    Commented Dec 17, 2022 at 13:26
  • \$\begingroup\$ @Neil You're right on both fronts; I somehow miscounted the number of characters in the answer. \$\endgroup\$ Commented Dec 17, 2022 at 20:45
6
\$\begingroup\$

Vyxal, 776 bytes, score = 0

⟨1.0079|4.0026|6.941|9.0122|10.811|12.0107|14.0067|15.9994|18.9984|20.1797|22.9897|24.305|26.9815|28.0855|30.9738|32.065|35.453|39.948|39.0983|40.078|44.9559|47.867|50.9415|51.9961|54.938|55.845|58.9332|58.6934|63.546|65.39|69.723|72.64|74.9216|78.96|79.904|83.8|85.4678|87.62|88.9059|91.224|92.9064|95.94|98|101.07|102.9055|106.42|107.8682|112.411|114.818|118.71|121.76|127.6|126.9045|131.293|132.9055|137.327|138.9055|140.116|140.9077|144.24|145|150.36|151.964|157.25|158.9253|162.5|164.9303|167.259|168.9342|173.04|174.967|178.49|180.9479|183.84|186.207|190.23|192.217|195.078|196.9665|200.59|204.3833|207.2|208.9804|209|210|222|223|226|227|232.0381|231.0359|238.0289|237|244|243|247|247|251|252|257|258|259|262|261|262|266|264|277|268|281|282|285|286|289|290|293|294|294⟩i

Try it Online!

Prints the exact average for each element, so each \$(p_{el} - w_{el})\$ = 0, meaning the fraction sums to 0/118, and multiplication gives score = 0

The scoring of this answer was correct at 2:27pm 15 Dec 2022 UTC. If by some chance the question has been edited to make the score invalid, I won't be available to change it for another 8 or so hours. Therefore, if the score is invalidated, I'll update this when I can.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ I hope this gets fixed, otherwise all the answers will get very boring \$\endgroup\$
    – MarcMush
    Commented Dec 15, 2022 at 15:41
  • \$\begingroup\$ @MarcMush I believe its new score is 1000602.176. \$\endgroup\$
    – Ginger
    Commented Dec 15, 2022 at 16:19
  • 1
    \$\begingroup\$ @Ginger - new, new score is now simply 777. \$\endgroup\$ Commented Dec 15, 2022 at 17:50
  • \$\begingroup\$ waiting for a -1 byter now \$\endgroup\$
    – math scat
    Commented Dec 17, 2022 at 18:56
6
\$\begingroup\$

Charcoal, 10 bytes, score 113.33712413095091

I×¹·⁶XN¹·¹

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

      N     Input integer
     X      Raised to power
       ¹·¹  Literal number `1.1`
 ×          Multiplied by
  ¹·⁶       Literal number `1.6`
I           Cast to string
            Implicitly print
\$\endgroup\$
5
\$\begingroup\$

Perl 5, 179 bytes, score 186

sub f{my$s;map$s+=/C|D/?hex:$_,("0x3221223xxxx5-11533xx051532414x12232323243436-142421x1525242224233324232433201Cx15-17-17-1404151x-114-2D-9Dxxx10"=~s/x/13/gr=~/-?./g)[1..pop];$s}

Try it online!

...to compute score.

sub f {
  my $s;                   #sum
  map $s += /C|D/?hex:$_,  #convert C and D to 12 and 13, one char per int
    (                      #string of closest integer diff between each weight:
      ("0x3221223xxxx5-11533xx051532414x12232323243436-142421x152".
       "5242224233324232433201Cx15-17-17-1404151x-114-2D-9Dxxx10")
      =~ s/x/13/gr         #"13" is frequent, decompress all "x" into "13"
      =~ /-?./g            #split into 118 negative and positive ints,
                           #...one char each with a - in front for negative ints
    )[1..pop];             #sum diffs only up to given input N popped from @_
   $s                      #return sum
}
\$\endgroup\$
5
\$\begingroup\$

GolfScript, score = (0 + 1) * (10550.87 + 1) = 10551.87, 0 bytes

Not sure what's wrong

\$\endgroup\$
9
  • \$\begingroup\$ The problem is that more error more better, so a language whose empty program output 0 is better than cat \$\endgroup\$
    – l4m2
    Commented Dec 15, 2022 at 16:45
  • \$\begingroup\$ I agree that there is a (big) problem with the new scoring scheme, but until that's fixed, it's worth noting that this is a polyglot, too... \$\endgroup\$ Commented Dec 15, 2022 at 16:48
  • \$\begingroup\$ @DominicvanEssen I'm not really sure what to do with the scoring system. People keep finding clever loopholes. \$\endgroup\$
    – Ginger
    Commented Dec 15, 2022 at 16:49
  • \$\begingroup\$ @DominicvanEssen Any language do output 0? \$\endgroup\$
    – l4m2
    Commented Dec 15, 2022 at 16:52
  • 1
    \$\begingroup\$ @Ginger - I'm reluctant to pretend to know the right answer here, but this challenge had a pretty simple (bytes+1)*(RMS error +1) scoring scheme and it seemed to work quite Ok... \$\endgroup\$ Commented Dec 15, 2022 at 16:54
5
\$\begingroup\$

Pyt, 9 bytes, score ~141.372

37*ᵮ₄₅^2*

Port of CursorCoercer's Pyth answer

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$ Commented Dec 17, 2022 at 1:48
  • \$\begingroup\$ I've been here before. I forgot my old password. My old username was mudkip201. But thanks~ \$\endgroup\$ Commented Dec 17, 2022 at 2:02
5
\$\begingroup\$

Pip, 11 bytes, score ≈ 103.034

1.6*aE1.1

Try It Online!

Score calculation

Better score thanks to @Neil

Blatantly stole the best fit for \$a∗n^b\$ from @97.100.97.109's answer, but adapted for a better score. The full list can be calculated here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 1.6*aE1.1 seems to score only 103. \$\endgroup\$
    – Neil
    Commented Dec 17, 2022 at 13:27
4
\$\begingroup\$

Python 3: Length = 16, Error ≈ 12.97, Score ≈ 237.48

A simple linear approximation.

lambda x:2.6*x-9
\$\endgroup\$
3
  • 1
    \$\begingroup\$ This is copied from my answer! \$\endgroup\$
    – The Thonnu
    Commented Dec 17, 2022 at 9:53
  • 1
    \$\begingroup\$ I've now got my one down to 235. \$\endgroup\$
    – The Thonnu
    Commented Dec 17, 2022 at 14:34
  • 1
    \$\begingroup\$ @TheThonnu - re: "This is copied from my answer" might be more-fairly stated as "This is the same approach as Marc Mush's answer, and his suggestion in the comments to my answer"... \$\endgroup\$ Commented Dec 21, 2022 at 21:14
4
\$\begingroup\$

[Bash + qalc], 47 bytes, Score 169.036

LANG=en qalc -t "atom($1;mass)/u"|sed s/±.*//

qalc is a CLI based interface of qalculate!, which allows us to extract certain properties of atoms atom(...;mass) will provide us the atomic mass of the n-th element. We divide the result by u to get a numeric value without unit (u). The result may have an uncertainty, notated by ±..., so we use sed to remove such parts. The decimal separator will depend on your locale. While comma could be disabled by setting an option it is shorter to change the locale in case. -t or --terse is required to prevent printing of the input query, thus the use of LANG=en. If we do not care for portability and assume a decimal point locale we can omit the first 8 bytes. If we do not care about uncertainties printed in the output we can remove the last 13 bytes.

Further if we assume the input has been injected before by variable q ... we can directly use LANG=en qalc -t -f file to achieve:

[qalc], 14 bytes, 52.824

atom(q;mass)/u

Note that this method differs somewhat from the given list for some period 7 block d elements, namely Lawrencium-Meitnerium or 103-109. This is because qalculate gives us the usual standard value, which is the mass of most stable known isotope. These elements are incredibly unstable and will decay in at most a few seconds. Thus they do not have some sort of "natural average" atomic weight, as they do not exist in nature. The values given in this list belong to less stable isotopes of these elements. If we adapt the list the score would of course improve drastically to 54.324 or 16.976.

\$\endgroup\$
2
  • \$\begingroup\$ What is your score though? This is not code golf, you are scored by the formula given, not length \$\endgroup\$
    – mousetail
    Commented Dec 17, 2022 at 12:53
  • \$\begingroup\$ @mousetail Added the score, thank you for pointing this out! \$\endgroup\$
    – Lazy
    Commented Dec 17, 2022 at 14:17
3
\$\begingroup\$

Japt, 2 bytes, score 101.61232686228814

ѽ

Try it

Score calulation

Logically identical to this Jelly answer (and some others), but shockingly 1 byte shorter for a decent score.

Explanation:

Ñ and ½ are "shortcuts" in Japt, meaning they get expanded before transpiling. Ñ becomes *2, while ½ becomes .5. As a result, this effectively becomes the program *2.5, which mulitplies the input by 2.5.

\$\endgroup\$
3
\$\begingroup\$

Desmos, 27 bytes, score ≈ 149.312

f(x)=211sin(.0132x-.87)+162

Try it on Desmos! A sine wave solution, created by optimizing the following regression:

asin([1...118]b+c)+d~[1.0079,4.0026,6.941,9.0122,10.811,12.0107,14.0067,15.9994,18.9984,20.1797,22.9897,24.305,26.9815,28.0855,30.9738,32.065,35.453,39.948,39.0983,40.078,44.9559,47.867,50.9415,51.9961,54.938,55.845,58.9332,58.6934,63.546,65.39,69.723,72.64,74.9216,78.96,79.904,83.8,85.4678,87.62,88.9059,91.224,92.9064,95.94,98,101.07,102.9055,106.42,107.8682,112.411,114.818,118.71,121.76,127.6,126.9045,131.293,132.9055,137.327,138.9055,140.116,140.9077,144.24,145,150.36,151.964,157.25,158.9253,162.5,164.9303,167.259,168.9342,173.04,174.967,178.49,180.9479,183.84,186.207,190.23,192.217,195.078,196.9665,200.59,204.3833,207.2,208.9804,209,210,222,223,226,227,232.0381,231.0359,238.0289,237,244,243,247,247,251,252,257,258,259,262,261,262,266,264,277,268,281,282,285,286,289,290,293,294,294][1...118]

The regression yields values of 211.205, 0.0131883, -0.871366, and 162.758 for a, b, c, and d, respectively.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.