Periodic table: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Major edit)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(38 intermediate revisions by 12 users not shown)
Line 58:
*   [https://ascii.periodni.com/index.html The periodic table in ascii] that was used as template
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F perta(atomic)
-V
NOBLES = [2, 10, 18, 36, 54, 86, 118]
INTERTWINED = [0, 0, 0, 0, 0, 57, 89]
INTERTWINING_SIZE = 14
LINE_WIDTH = 18
 
V prev_noble = 0
V row = 0
Int col
L(noble) NOBLES
row = L.index
I atomic <= noble
V nb_elem = noble - prev_noble
V rank = atomic - prev_noble
I INTERTWINED[row] & atomic C INTERTWINED[row] .. INTERTWINED[row] + INTERTWINING_SIZE
row += 2
col = rank + 1
E
V nb_empty = LINE_WIDTH - nb_elem
V inside_left_element_rank = I noble > 2 {2} E 1
col = rank + (I rank > inside_left_element_rank {nb_empty} E 0)
L.break
prev_noble = noble
R (row + 1, col)
 
V TESTS = [
(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)),
]
 
L(input, out) TESTS
V found = perta(input)
print(‘TEST:#3 -> ’.format(input)‘’String(found)‘’(I found != out {‘ ; ERROR: expected ’out} E ‘’))
</syntaxhighlight>
 
{{out}}
<pre>
TEST: 1 -> (1, 1)
TEST: 2 -> (1, 18)
TEST: 29 -> (4, 11)
TEST: 42 -> (5, 6)
TEST: 58 -> (8, 5)
TEST: 59 -> (8, 6)
TEST: 57 -> (8, 4)
TEST: 71 -> (8, 18)
TEST: 72 -> (6, 4)
TEST: 89 -> (9, 4)
TEST: 90 -> (9, 5)
TEST:103 -> (9, 18)
</pre>
 
=={{header|6502 Assembly}}==
Line 276 ⟶ 342:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|BASICA}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 REM Periodic table
20 CLS
30 DIM a(7),b(7)
40 GOSUB 100
50 FOR j = 0 TO 9
60 READ anum : GOSUB 140
70 NEXT j
80 END
90 REM Set arrays A, B.
100 FOR i = 0 TO 7 : READ a(i) : NEXT i
110 FOR i = 0 TO 7 : READ b(i) : NEXT i
120 RETURN
130 REM Show row AND column FOR element
140 i = 7
150 WHILE a(i) > anum
160 i = i-1
170 WEND
180 m = anum+b(i)
190 r = INT(m/18)+1
200 c = m MOD 18+1
210 PRINT anum "-> " r c
220 RETURN
230 REM DATA
240 REM Arrays A, B.
250 DATA 1,2,5,13,57,72,89,104
260 DATA -1,15,25,35,72,21,58,7
270 REM Example elements (atomic numbers).
280 DATA 1,2,29,42,57,58,72,89,90,103</syntaxhighlight>
{{out}}
<pre>Same as GW-BASIC entry.</pre>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">dim a[1, 2, 5, 13, 57, 72, 89, 104]
dim b[-1, 15, 25, 35, 72, 21, 58, 7]
 
title "Periodic Table Search"
resize 0, 0, 220, 140
center
 
formid 1
formtext "Search"
buttonform 55, 40, 100, 20
 
formid 2
formtext ""
staticform 1, 1, 220, 20
 
do
 
if forms = 1 then
 
gosub searchtable
 
endif
 
button k, 27
 
wait
 
loop k <> 1
 
end
 
sub searchtable
 
input "Atomic number", e
 
let i = 8
 
do
 
let i = i - 1
 
loop a[i] > e
 
let m = e + b[i]
let r = int(m / 18) + 1
let c = int(m % 18) + 1
 
formid 2
formtext "Period: ", r, comma, " Group: ", c
updateform
 
return</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 312 ⟶ 468:
Atomic number 113 -> 7, 13</pre>
 
==={{header|FTCBASIC}}===
<syntaxhighlight lang="basic">rem uses carry command to perform modulus
rem compiles with FTCBASIC to 545 bytes com file
 
define e = 0, i = 0, m = 0, r = 0, c = 0, q = 0
 
dim a[1, 2, 5, 13, 57, 72, 89, 104]
dim b[-1, 15, 25, 35, 72, 21, 58, 7]
 
do
 
cls
 
print "Periodic Table Lookup"
print "0 to continue or 1 to quit: " \
 
input q
 
if q = 0 then
 
gosub searchtable
 
endif
 
pause
 
loop q <> 1
 
end
 
sub searchtable
 
print "Atomic number: " \
input e
 
let i = 8
 
do
 
let i = i - 1
 
loop @a[i] > e
 
let m = e + @b[i]
 
let r = m / 18
carry c
 
let r = r + 1
let c = c + 1
 
print "Period: " \
print r \
print " Group: " \
print c \
 
return</syntaxhighlight>
 
==={{header|Gambas}}===
Line 502 ⟶ 715:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Quite BASIC}}===
{{trans|Minimal BASIC}}
<syntaxhighlight lang="qbasic">10 REM Periodic table
20 GOSUB 200
30 FOR J = 0 TO 9
40 READ N
50 GOSUB 400
60 NEXT J
70 END
190 REM Set arrays A, B.
200 ARRAY A
210 LET A[0] = 1
215 LET A[1] = 2
220 LET A[2] = 5
225 LET A[3] = 13
230 LET A[4] = 57
235 LET A[5] = 72
240 LET A[6] = 89
245 LET A[7] = 104
246 ARRAY B
250 LET B[0] = -1
255 LET B[1] = 15
260 LET B[2] = 25
265 LET B[3] = 35
270 LET B[4] = 72
275 LET B[5] = 21
280 LET B[6] = 58
285 LET B[7] = 7
290 RETURN
390 REM Show row and column for element
400 LET I = 7
410 IF A(I) <= N THEN 440
420 LET I = I-1
430 GOTO 410
440 LET M = N+B(I)
450 LET R = INT(M/18)+1
460 LET C = M-INT(M/18)*18+1
470 PRINT N; " -> "; R; " "; C
480 RETURN
1030 REM Example elements (atomic numbers).
1040 DATA 1, 2, 29, 42, 57, 58, 72, 89, 90, 103</syntaxhighlight>
 
==={{header|Run BASIC}}===
Line 609 ⟶ 864:
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|uBasic/4tH}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="uBasic/4tH">Dim @a(8)
Dim @b(8)
Dim @e(13)
 
Push 1, 2, 5, 13, 57, 72, 89, 104 ' load array A
For x = Used()-1 to 0 Step -1 : @a(x) = Pop() : Next
 
Push -1, 15, 25, 35, 72, 21, 58, 7 ' load array B
For x = Used()-1 to 0 Step -1 : @b(x) = Pop() : Next
' load array E
Push 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113 : s = Used()-1
For x = s to 0 Step -1 : @e(x) = Pop() : Next
 
For x = 0 To s
Proc _MostarPos(@e(x))
Next
 
End
 
_MostarPos
Param (1)
Local (4)
 
c@ = 7
Do While @a(c@) > a@
c@ = c@ - 1
Loop
 
b@ = a@ + @b(c@)
d@ = (b@ / 18) + 1
e@ = (b@ % 18) + 1
Print Using "Atomic number __#"; a@; Using " -> _#"; d@; Using ", _#"; e@
Return</syntaxhighlight>
{{Out}}
<pre>Atomic number 1 -> 1, 1
Atomic number 2 -> 1, 18
Atomic number 29 -> 4, 11
Atomic number 42 -> 5, 6
Atomic number 57 -> 8, 4
Atomic number 58 -> 8, 5
Atomic number 59 -> 8, 6
Atomic number 71 -> 8, 18
Atomic number 72 -> 6, 4
Atomic number 89 -> 9, 4
Atomic number 90 -> 9, 5
Atomic number 103 -> 9, 18
Atomic number 113 -> 7, 13
 
0 OK, 0:468</pre>
 
==={{header|XBasic}}===
Line 714 ⟶ 1,021:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
 
==={{libheader|Gadget}}===
The solution is written in ANSI C, but with the "Gadget" library, designed to work on Linux Debian 11 OS and its derivatives.
 
More information about this bookstore, please visit the following link:
 
https://github.com/DanielStuardo/Gadget
 
<syntaxhighlight lang="C">
 
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
/* prototypes */
GD_VIDEO put_chemical_cell( GD_VIDEO table, MT_CELL * E, DS_ARRAY E_data );
MT_CELL* load_chem_elements( MT_CELL * E, DS_ARRAY * E_data );
int select_box_chemical_elem( RDS(MT_CELL, Elements) );
void put_information(RDS( MT_CELL, elem), int i);
 
Main
GD_VIDEO table;
/* las dimensiones del terminal son muy importantes
para desplegar la tabla */
Resize_terminal(42,135);
Init_video( &table );
Gpm_Connect conn;
 
if ( ! Init_mouse(&conn)){
Msg_red("No se puede conectar al servidor del ratón\n");
Stop(1);
}
Enable_raw_mode();
Hide_cursor;
 
/* I declare a multitype array "Elements", and load the file that will populate this array. */
New multitype Elements;
Elements = load_chem_elements( pSDS(Elements) );
Throw( load_fail );
/* I create a "Button" object with which the execution will end. */
New objects Btn_exit;
Btn_exit = New_object_mouse( SMD(&Btn_exit), BUTTOM, " Terminar ", 6,44, 15, 0);
 
/* Fill the space on the screen with the table of chemical elements. */
table = put_chemical_cell( table, SDS(Elements) );
 
/* I print the screen and place the mouse object. */
Refresh(table);
Put object Btn_exit;
/* I wait for a mouse event. */
int c;
Waiting_some_clic(c)
{
if( select_box_chemical_elem(SDS(Elements)) ){
Waiting_some_clic(c) break;
}
if (Object_mouse( Btn_exit)) break;
Refresh(table);
Put object Btn_exit;
}
 
Free object Btn_exit;
Free multitype Elements;
Exception( load_fail ){
Msg_red("No es un archivo matriciable");
}
 
Free video table;
Disable_raw_mode();
Close_mouse();
Show_cursor;
At SIZE_TERM_ROWS,0;
Prnl;
End
 
void put_information(RDS(MT_CELL, elem), int i)
{
At 2,19;
Box_solid(11,64,67,17);
Color(15,17);
At 4,22; Print "Elemento (%s) = %s", (char*)$s-elem[i,5],(char*)$s-elem[i,6];
if (Cell_type(elem,i,7) == double_TYPE ){
At 5,22; Print "Peso atómico = %f", $d-elem[i,7];
}else{
At 5,22; Print "Peso atómico = (%ld)", $l-elem[i,7];
}
At 6,22; Print "Posición = (%ld, %ld)",$l-elem[i,0]+ ($l-elem[i,0]>=8 ? 0:1),$l-elem[i,1]+1;
At 8,22; Print "1ª energía de";
if (Cell_type(elem,i,12) == double_TYPE ){
At 9,22; Print "ionización (kJ/mol) = %.*f",2,$d-elem[i,12];
}else{
At 9,22; Print "ionización (kJ/mol) = ---";
}
if (Cell_type(elem,i,13) == double_TYPE ){
At 10,22; Print "Electronegatividad = %.*f",2,$d-elem[i,13];
}else{
At 10,22; Print "Electronegatividad = ---";
}
At 4,56; Print "Conf. electrónica:";
At 5,56; Print " %s", (char*)$s-elem[i,14];
At 7,56; Print "Estados de oxidación:";
if ( Cell_type(elem,i,15) == string_TYPE ){
At 8,56; Print " %s", (char*)$s-elem[i,15];
}else{
/* Strangely, when the file loader detects a "+n", it treats it as a string,
but when it detects a "-n", it treats it as a "long".
I must review that. */
At 8,56; Print " %ld", $l-elem[i,15];
}
At 10,56; Print "Número Atómico: %ld",$l-elem[i,4];
Reset_color;
}
 
int select_box_chemical_elem( RDS(MT_CELL, elem) )
{
int i;
Iterator up i [0:1:Rows(elem)]{
if ( Is_range_box( $l-elem[i,8], $l-elem[i,9], $l-elem[i,10], $l-elem[i,11]) ){
Gotoxy( $l-elem[i,8], $l-elem[i,9] );
Color_fore( 15 ); Color_back( 0 );
Box( 4,5, DOUB_ALL );
 
Gotoxy( $l-elem[i,8]+1, $l-elem[i,9]+2); Print "%ld",$l-elem[i,4];
Gotoxy( $l-elem[i,8]+2, $l-elem[i,9]+2); Print "%s",(char*)$s-elem[i,5];
Flush_out;
Reset_color;
put_information(SDS(elem),i);
return 1;
}
}
return 0;
}
 
GD_VIDEO put_chemical_cell(GD_VIDEO table, MT_CELL * elem, DS_ARRAY elem_data)
{
int i;
/* put each cell */
Iterator up i [0:1:Rows(elem)]{
long rx = 2+($l-elem[i,0]*4);
long cx = 3+($l-elem[i,1]*7);
long offr = rx+3;
long offc = cx+6;
 
Gotoxy(table, rx, cx);
 
Color_fore(table, $l-elem[i,2]);
Color_back(table,$l-elem[i,3]);
 
Box(table, 4,5, SING_ALL );
 
char Atnum[50], Elem[50];
sprintf(Atnum,"\x1b[3m%ld\x1b[23m",$l-elem[i,4]);
sprintf(Elem, "\x1b[1m%s\x1b[22m",(char*)$s-elem[i,5]);
 
Outvid(table,rx+1, cx+2, Atnum);
Outvid(table,rx+2, cx+2, Elem);
 
Reset_text(table);
/* Update positions of each cell to be detected by the mouse. */
$l-elem[i,8] = rx;
$l-elem[i,9] = cx;
$l-elem[i,10] = offr;
$l-elem[i,11] = offc;
}
/* put rows and cols */
Iterator up i [ 1: 1: 19 ]{
Gotoxy(table, 31, 5+(i-1)*7);
char num[5]; sprintf( num, "%d",i );
Outvid(table, num );
}
Iterator up i [ 1: 1: 8 ]{
Gotoxy(table, 3+(i-1)*4, 130);
char num[5]; sprintf( num, "%d",i );
Outvid(table, num );
}
Outvid( table, 35,116, "8");
Outvid( table, 39,116, "9");
/* others */
Color_fore(table, 15);
Color_back(table, 0);
Outvid(table,35,2,"Lantánidos ->");
Outvid(table,39,2,"Actínidos ->");
Reset_text(table);
return table;
}
 
MT_CELL* load_chem_elements( MT_CELL * E, DS_ARRAY * E_data )
{
F_STAT dataFile = Stat_file("chem_table.txt");
if( dataFile.is_matrix ){
/*
Set the ranges to read from the file.
You can choose the portion of the file that you want to upload.
*/
Range ptr E [0:1:dataFile.total_lines-1, 0:1:dataFile.max_tokens_per_line-1];
E = Load_matrix_mt( SDS(E), "chem_table.txt", dataFile, DET_LONG);
}else{
Is_ok=0;
}
return E;
}
 
</syntaxhighlight>
{{out}}
<p>Initial screen:</p>
[[File:Tabla_periodica_rosetta_code_01.png]]
<p>Element selected:</p>
[[File:Tabla_periodica_rosetta_code_02.png]]
<p>File: chem_table.txt</p>
<pre>
0,0,232,105,1,H,Hidrógeno,1.00794,0,0,0,0,1312.0,2.20,1s¹,+1-1
0,17,232,4,2,He,Helio,4.002602,0,0,0,0,2372.3,---,1s²,---
1,0,232,166,3,Li,Litio,6.941,0,0,0,0,520.2,0.98,1s²2s¹,+1-1
1,1,232,172,4,Be,Berilio,9.012182,0,0,0,0,899.5,1.57,1s²2s²,+2
1,12,232,70,5,B,Boro,10.811,0,0,0,0,800.6,2.04,1s²2s²2p¹,+3+2+1
1,13,232,105,6,C,Carbono,12.0107,0,0,0,0,1086.5,2.55,1s²2s²2p²,+4+3+2+1-1-2-3-4
1,14,232,105,7,N,Nitrógeno,14.0067,0,0,0,0,1402.3,3.04,1s²2s²2p³,+5+4+3+2+1-1-2-3
1,15,232,105,8,O,Oxígeno,15.9994,0,0,0,0,1313.9,3.44,1s²2s²2p⁴,+2+1-1-2
1,16,232,5,9,F,Flúor,18.998403,0,0,0,0,1681.0,3.98,1s²2s²2p⁵,-1
1,17,232,4,10,Ne,Neón,20.1797,0,0,0,0,2080.7,---,1s²2s²2p⁶,---
2,0,232,166,11,Na,Sodio,22.98976,0,0,0,0,495.8,0.93,[Ne]3s¹,+1-1
2,1,232,172,12,Mg,Magnesio,24.3050,0,0,0,0,737.7,1.31,[Ne]3s²,+2+1
2,12,232,178,13,Al,Aluminio,26.98153,0,0,0,0,577.5,1.61,[Ne]3s²3p¹,+3+1
2,13,232,70,14,Si,Silicio,28.0855,0,0,0,0,786.5,1.90,[Ne]3s²3p²,+4+3+2+1-1-2-3-4
2,14,232,105,15,P,Fósforo,30.97696,0,0,0,0,1011.8,2.19,[Ne]3s²3p³,+5+4+3+2+1-1-2
2,15,232,105,16,S,Azufre,32.065,0,0,0,0,999.6,2.58,[Ne]3s²3p⁴,+6+5+4+3+2+1-1-2
2,16,232,5,17,Cl,Cloro,35.453,0,0,0,0,1251.2,3.16,[Ne]3s²3p⁵,+7+6+5+4+3+2+1-1
2,17,232,4,18,Ar,Argón,39.948,0,0,0,0,1520.6,---,[Ne]3s²3p⁶,---
3,0,232,166,19,K,Potasio,39.0983,0,0,0,0,418.8,0.82,[Ar]4s¹,+1
3,1,232,172,20,Ca,Calcio,40.078,0,0,0,0,589.8,1.00,[Ar]4s²,+2
3,2,232,112,21,Sc,Escandio,44.95591,0,0,0,0,633.1,1.36,[Ar]3d¹4s²,+3+2+1
3,3,232,112,22,Ti,Titanio,47.867,0,0,0,0,658.8,1.54,[Ar]3d²4s²,+4+3+2+1-1
3,4,232,112,23,V,Vanadio,50.9415,0,0,0,0,650.9,1.63,[Ar]3d³4s²,+5+4+3+2+1-1
3,5,232,112,24,Cr,Cromo,51.9962,0,0,0,0,652.9,1.66,[Ar]3d⁵4s¹,+6+5+4+3+2+1-1-2
3,6,232,112,25,Mn,Manganeso,54.93804,0,0,0,0,717.3,1.55,[Ar]3d⁵4s²,+7+6+5+4+3+2+1...-3
3,7,232,112,26,Fe,Hierro,55.845,0,0,0,0,762.5,1.83,[Ar]3d⁶4s²,+6+5+4+3+2+1-1-2
3,8,232,112,27,Co,Cobalto,58.93319,0,0,0,0,760.4,1.91,[Ar]3d⁷4s²,+5+4+3+2+1-1-2
3,9,232,112,28,Ni,Niquel,58.6934,0,0,0,0,737.1,1.88,[Ar]3d⁸4s²,+4+3+2+1-1
3,10,232,112,29,Cu,Cobre,63.546,0,0,0,0,745.5,1.90,[Ar]3d¹⁰4s¹,+4+3+2+1
3,11,232,112,30,Zn,Zinc,65.38,0,0,0,0,906.4,1.65,[Ar]3d¹⁰4s²,+2
3,12,232,178,31,Ga,Galio,69.723,0,0,0,0,578.8,1.81,[Ar]3d¹⁰4s²4p¹,+3+2+1
3,13,232,70,32,Ge,Germanio,72.64,0,0,0,0,762.0,2.01,[Ar]3d¹⁰4s²4p²,+4+3+2+1-4
3,14,232,70,33,As,Arsénico,74.92160,0,0,0,0,947.0,2.18,[Ar]3d¹⁰4s²4p³,+5+3+2-3
3,15,232,105,34,Se,Selenio,78.96,0,0,0,0,941.0,2.55,[Ar]3d¹⁰4s²4p⁴,+6+4+2-2
3,16,232,5,35,Br,Bromo,79.904,0,0,0,0,1139.9,2.96,[Ar]3d¹⁰4s²4p⁵,+7+5+4+3+1-1
3,17,232,4,36,Kr,Kriptón,83.798,0,0,0,0,1350.8,3.00,[Ar]3d¹⁰4s²4p⁶,+2
4,0,232,166,37,Rb,Rubidio,85.4678,0,0,0,0,403.0,0.82,[Kr]5s¹,+1
4,1,232,172,38,Sr,Estroncio,87.62,0,0,0,0,549.5,0.95,[Kr]5s²,+2
4,2,232,112,39,Y,Itrio,88.90585,0,0,0,0,600.0,1.22,[Kr]4d¹5s²,+3+2+1
4,3,232,112,40,Zr,Circonio,91.224,0,0,0,0,640.1,1.33,[Kr]4d²5s²,+4+3+2+1
4,4,232,112,41,Nb,Niobio,92.90638,0,0,0,0,652.1,1.60,[Kr]4d⁴5s¹,+5+4+3+2-1
4,5,232,112,42,Mo,Molibdeno,95.96,0,0,0,0,684.3,2.16,[Kr]4d⁵5s¹,+6+5+4+3+2+1-1-2
4,6,232,112,43,Tc,Tecnecio,98,0,0,0,0,702.0,1.90,[Kr]4d⁵5s²,+7+6+5+4+3+2+1-1-3
4,7,232,112,44,Ru,Rutenio,101.07,0,0,0,0,710.2,2.20,[Kr]4d⁷5s¹,+8+7+6+5+4+3+2+1-2
4,8,232,112,45,Rh,Rodio,102.9055,0,0,0,0,719.7,2.28,[Kr]4d⁸5s¹,+6+5+4+3+2+1-1
4,9,232,112,46,Pd,Paladio,106.42,0,0,0,0,804.4,2.20,[Kr]4d¹⁰,+4+2
4,10,232,112,47,Ag,Plata,107.8682,0,0,0,0,731.0,1.93,[Kr]4d¹⁰5s¹,+3+2+1
4,11,232,112,48,Cd,Cadmio,112.441,0,0,0,0,867.8,1.69,[Kr]4d¹⁰5s²,+2
4,12,232,178,49,In,Indio,114.818,0,0,0,0,558.3,1.78,[Kr]4d¹⁰5s²5p¹,+3+2+1
4,13,232,178,50,Sn,Estaño,118.710,0,0,0,0,708.6,1.96,[Kr]4d¹⁰5s²5p²,+4+2-4
4,14,232,70,51,Sb,Antimonio,121.760,0,0,0,0,834.0,2.05,[Kr]4d¹⁰5s²5p³,+5+3-3
4,15,232,70,52,Te,Telurio,127.60,0,0,0,0,869.3,2.10,[Kr]4d¹⁰5s²5p⁴,+6+5+4+2-2
4,16,232,5,53,I,Yodo,126.9044,0,0,0,0,1008.4,2.66,[Kr]4d¹⁰5s²5p⁵,+7+5+3+1-1
4,17,232,4,54,Xe,Xenón,131.293,0,0,0,0,1170.4,2.60,[Kr]4d¹⁰5s²5p⁶,+8+6+4+2
5,0,232,166,55,Cs,Cesio,132.9054,0,0,0,0,375.7,0.79,[Xe]6s¹,+1
5,1,232,172,56,Ba,Bario,137.327,0,0,0,0,502.9,0.89,[Xe]6s²,+2
5,2,232,112,71,Lu,Lutecio,174.9668,0,0,0,0,523.5,1.27,[Xe]4f¹⁴5d¹6s²,+3
5,3,232,112,72,Hf,Hafnio,178.49,0,0,0,0,658.5,1.30,[Xe]4f¹⁴5d²6s²,+4+3+2
5,4,232,112,73,Ta,Tantalio,180.9478,0,0,0,0,761.0,1.50,[Xe]4f¹⁴5d³6s²,+5+4+3+2-1
5,5,232,112,74,W,Wolframio,183.84,0,0,0,0,770.0,2.36,[Xe]4f¹⁴5d⁴6s²,+6+5+4+3+2+1-1-2
5,6,232,112,75,Re,Renio,186.207,0,0,0,0,760.0,1.90,[Xe]4f¹⁴5d⁵6s²,+7+6+5+4+3+2+1-1-3
5,7,232,112,76,Os,Osmio,190.23,0,0,0,0,840.0,2.20,[Xe]4f¹⁴5d⁶6s²,+8+7+6+5+4+3+2+1-2
5,8,232,112,77,Ir,Iridio,192.217,0,0,0,0,880.0,2.20,[Xe]4f¹⁴5d⁷6s²,+6+5+4+3+2+1-1-3
5,9,232,112,78,Pt,Platino,195.084,0,0,0,0,870.0,2.28,[Xe]4f¹⁴5d⁹6s¹,+6+5+4+2
5,10,232,112,79,Au,Oro,196.9665,0,0,0,0,890.0,2.54,[Xe]4f¹⁴5d¹⁰6s¹,+5+3+2+1-1
5,11,232,112,80,Hg,Mercurio,200.59,0,0,0,0,1007.1,2.00,[Xe]4f¹⁴5d¹⁰6s²,+4+2+1
5,12,232,178,81,Tl,Talio,204.3833,0,0,0,0,589.4,1.62,[Xe]4f¹⁴5d¹⁰6s²6p¹,+3+1
5,13,232,178,82,Pb,Plomo,207.2,0,0,0,0,715.6,2.33,[Xe]4f¹⁴5d¹⁰6s²6p²,+4+2-4
5,14,232,178,83,Bi,Bismuto,208.9804,0,0,0,0,703.0,2.02,[Xe]4f¹⁴5d¹⁰6s²6p³,+5+3-3
5,15,232,70,84,Po,Polonio,210,0,0,0,0,812.1,2.00,[Xe]4f¹⁴5d¹⁰6s²6p⁴,+6+4+2-2
5,16,232,5,85,At,Astato,210,0,0,0,0,890.0,2.20,[Xe]4f¹⁴5d¹⁰6s²6p⁵,+1-1
5,17,232,4,86,Rn,Radón,220,0,0,0,0,1037.0,---,[Xe]4f¹⁴5d¹⁰6s²6p⁶,---
6,0,232,166,87,Fr,Francio,223,0,0,0,0,380.0,0.70,[Rn]7s¹,+1
6,1,232,172,88,Ra,Radio,226,0,0,0,0,509.3,0.90,[Rn]7s²,+2
6,2,232,112,103,Lr,Lawrencio,262,0,0,0,0,470,---,[Rn]5f¹⁴7s²7p¹,+3
6,3,232,112,104,Rf,Rutherfordio,261,0,0,0,0,580.0,---,[Rn]5f¹⁴7s²6d²,+4
6,4,232,112,105,Db,Dubnio,262,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d³,+5
6,5,232,112,106,Sg,Seaborgio,266,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d⁴,+6
6,6,232,112,107,Bh,Bohrio,264,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d⁵,+7
6,7,232,112,108,Hs,Hasio,277,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰,+8
6,8,232,112,109,Mt,Meitnerio,268,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d⁷,+3+1+3+5
6,9,232,112,110,Ds,Darmstatio,271,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d⁸,+6
6,10,232,112,111,Rg,Roentgenio,272,0,0,0,0,---,---,[Rn]5f¹⁴7s¹6d¹⁰,-1+1+3+5
6,11,232,112,112,Cn,Copernicio,285,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰,+4+2
6,12,232,178,113,Nh,Nihonio,284,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p¹,+1+3+5
6,13,232,178,114,Fl,Flerovio,289,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p²,+2+4
6,14,232,178,115,Mc,Moscovio,288,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p³,+1+3
6,15,232,178,116,Lv,Livermorio,292,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p⁴,+2+4
6,16,232,5,117,Ts,Teneso,294,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p⁵,-1+1+3+5
6,17,232,4,118,Og,Oganesón,294,0,0,0,0,---,---,[Rn]5f¹⁴7s²6d¹⁰7p⁶,+2+4
8,2,232,46,57,La,Lantano,138.9054,0,0,0,0,538.1,1.10,[Xe]5d¹6s²,+3+2
8,3,232,46,58,Ce,Cerio,140.116,0,0,0,0,534.4,1.12,[Xe]4f¹5d¹6s²,+4+3+2
8,4,232,46,59,Pr,Praseodimio,140.9076,0,0,0,0,527.0,1.13,[Xe]4f³6s²,+4+3+2
8,5,232,46,60,Nd,Neodimio,144.242,0,0,0,0,533.1,1.14,[Xe]4f⁴6s²,+3+2
8,6,232,46,61,Pm,Prometio,145,0,0,0,0,540.0,---,[Xe]4f⁵6s²,+3
8,7,232,46,62,Sm,Samario,150.36,0,0,0,0,544.5,1.17,[Xe]4f⁶6s²,+3+2
8,8,232,46,63,Eu,Europio,151.964,0,0,0,0,547.1,---,[Xe]4f⁷6s²,+3+2
8,9,232,46,64,Gd,Gadolinio,157.25,0,0,0,0,593.4,1.20,[Xe]4f⁷5d¹6s²,+3+2+1
8,10,232,46,65,Tb,Terbio,158.9253,0,0,0,0,565.8,---,[Xe]4f⁹6s²,+4+3+1
8,11,232,46,66,Dy,Disprosio,162.500,0,0,0,0,573.0,1.22,[Xe]4f¹⁰6s²,+3+2
8,12,232,46,67,Ho,Holmio,164.9303,0,0,0,0,581.0,1.23,[Xe]4f¹¹6s²,+3
8,13,232,46,68,Er,Erbio,167.259,0,0,0,0,589.3,1.24,[Xe]4f¹²6s²,+3
8,14,232,46,69,Tm,Tulio,168.9342,0,0,0,0,596.7,1.25,[Xe]4f¹³6s²,+3+2
8,15,232,46,70,Yb,Iterbio,173.054,0,0,0,0,603.4,---,[Xe]4f¹⁴6s²,+3+2
9,2,232,40,89,Ac,Actinio,227,0,0,0,0,499.0,1.10,[Rn]6d¹7s²,+3
9,3,232,40,90,Th,Torio,232.0380,0,0,0,0,587.0,1.30,[Rn]6d²7s²,+4+3+2
9,4,232,40,91,Pa,Protactinio,231.0359,0,0,0,0,568.0,1.50,[Rn]5f²6d¹7s²,+5+4+3
9,5,232,40,92,U,Uranio,238.0285,0,0,0,0,597.6,1.38,[Rn]5f³6d¹7s²,+6+5+4+3
9,6,232,40,93,Np,Neptunio,237,0,0,0,0,604.5,1.36,[Rn]5f⁴6d¹7s²,+7+6+5+4+3
9,7,232,40,94,Pu,Plutonio,244,0,0,0,0,584.7,1.28,[Rn]5f⁶7s²,+7+6+5+4+3
9,8,232,40,95,Am,Americio,243,0,0,0,0,578.0,1.30,[Rn]5f⁷7s²,+6+5+4+3+2
9,9,232,40,96,Cm,Curio,247,0,0,0,0,581.0,1.30,[Rn]5f⁷6d¹7s²,+4+3
9,10,232,40,97,Bk,Berkelio,247,0,0,0,0,601.0,1.30,[Rn]5f⁹7s²,+4+3
9,11,232,40,98,Cf,Californio,251,0,0,0,0,608.0,1.30,[Rn]5f¹⁰7s²,+4+3+1
9,12,232,40,99,Es,Einstenio,252,0,0,0,0,619.0,1.30,[Rn]5f¹¹6s²,+3+2
9,13,232,40,100,Fm,Fermio,257,0,0,0,0,627.0,1.30,[Rn]5f¹²7s²,+3+2
9,14,232,40,101,Md,Mendelevio,258,0,0,0,0,635.0,1.30,[Rn]5f¹³7s²,+3+2
9,15,232,40,102,No,Nobelio,259,0,0,0,0,642.0,1.30,[Rn]5f¹⁴7s²,+3+2
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <vector>
 
struct Group {
int32_t first;
int32_t last;
};
 
struct Position{
int32_t period;
int32_t group;
};
 
const std::vector<Group> GROUPS = { Group(3, 10), Group(11, 18),
Group(19, 36), Group(37, 54), Group(55, 86), Group(87, 118) };
 
Position periodic_table(const int32_t& atomic_number) {
if ( atomic_number < 1 || atomic_number > 118 ) {
throw std::invalid_argument("Atomic number is out of range:" + atomic_number);
}
 
if ( atomic_number == 1 ) { // Hydrogen
return Position(1, 1);
}
if ( atomic_number == 2 ) { // Helium
return Position(1, 18);
}
if ( atomic_number >= 57 && atomic_number <= 71 ) { // Lanthanides
return Position(8, atomic_number - 53);
}
if ( atomic_number >= 89 && atomic_number <= 103 ) { // Actinides
return Position(9, atomic_number - 85);
}
 
int32_t period = 0;
int32_t periodFirst = 0;
int32_t periodLast = 0;
for ( uint64_t i = 0; i < GROUPS.size() && period == 0; ++i ) {
Group group = GROUPS[i];
if ( atomic_number >= group.first && atomic_number <= group.last ) {
period = i + 2;
periodFirst = group.first;
periodLast = group.last;
}
}
 
if ( atomic_number < periodFirst + 2 || period == 4 || period == 5 ) {
return Position(period, atomic_number - periodFirst + 1);
}
return Position(period, atomic_number - periodLast + 18);
}
 
int main() {
for ( int32_t atomic_number : { 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113 } ) {
Position position = periodic_table(atomic_number);
std::cout << "Atomic number " << std::left << std::setw(3) << atomic_number
<< " -> " << position.period << ", " << position.group << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Atomic number 1 -> 1, 1
Atomic number 2 -> 1, 18
Atomic number 29 -> 4, 11
Atomic number 42 -> 5, 6
Atomic number 57 -> 8, 4
Atomic number 58 -> 8, 5
Atomic number 59 -> 8, 6
Atomic number 71 -> 8, 18
Atomic number 72 -> 6, 4
Atomic number 89 -> 9, 4
Atomic number 90 -> 9, 5
Atomic number 103 -> 9, 18
Atomic number 113 -> 7, 13
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc mpos n . .
a[] = [ 1 2 5 13 57 72 89 104 ]
b[] = [ -1 15 25 35 72 21 58 7 ]
i = len a[]
while a[i] > n
i -= 1
.
m = n + b[i]
r = m div 18 + 1
c = m mod 18 + 1
print "Atomic number " & n & "-> " & r & ", " & c
.
elem[] = [ 1 2 29 42 57 58 59 71 72 89 90 103 113 ]
for e in elem[]
mpos e
.
</syntaxhighlight>
 
 
Line 789 ⟶ 1,539:
{{output}}
[[File:Periodic Table FutureBasic.png]]
 
 
=={{header|Go}}==
Line 901 ⟶ 1,650:
9 4</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
 
public final class PeriodicTable {
 
public static void main(String[] aArgs) {
for ( int atomicNumber : List.of( 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113 ) ) {
Position position = periodicTable(atomicNumber);
System.out.println(String.format("%s%-3d%s%d%s%d",
"Atomic number ", atomicNumber, " -> ", position.period, ", ", position.group));
}
}
private static Position periodicTable(int aAtomicNumber) {
if ( aAtomicNumber < 1 || aAtomicNumber > 118 ) {
throw new IllegalArgumentException("Atomic number is out of range:" + aAtomicNumber);
}
if ( aAtomicNumber == 1 ) { // Hydrogen
return new Position(1, 1);
}
if ( aAtomicNumber == 2 ) { // Helium
return new Position(1, 18);
}
if ( aAtomicNumber >= 57 && aAtomicNumber <= 71 ) { // Lanthanides
return new Position(8, aAtomicNumber - 53);
}
if ( aAtomicNumber >= 89 && aAtomicNumber <= 103 ) { // Actinides
return new Position(9, aAtomicNumber - 85);
}
int period = 0;
int periodFirst = 0;
int periodLast = 0;
for ( int i = 0; i < GROUPS.size() && period == 0; i++ ) {
Group group = GROUPS.get(i);
if ( aAtomicNumber >= group.first && aAtomicNumber <= group.last ) {
period = i + 2;
periodFirst = group.first;
periodLast = group.last;
}
}
if ( aAtomicNumber < periodFirst + 2 || period == 4 || period == 5 ) {
return new Position(period, aAtomicNumber - periodFirst + 1);
}
return new Position(period, aAtomicNumber - periodLast + 18);
}
private static record Group(int first, int last) {}
private static record Position(int period, int group) {}
private static final List<Group> GROUPS = List.of( new Group(3, 10), new Group(11, 18),
new Group(19, 36), new Group(37, 54), new Group(55, 86), new Group(87, 118) );
 
}
</syntaxhighlight>
{{ out }}
<pre>
Atomic number 1 -> 1, 1
Atomic number 2 -> 1, 18
Atomic number 29 -> 4, 11
Atomic number 42 -> 5, 6
Atomic number 57 -> 8, 4
Atomic number 58 -> 8, 5
Atomic number 59 -> 8, 6
Atomic number 71 -> 8, 18
Atomic number 72 -> 6, 4
Atomic number 89 -> 9, 4
Atomic number 90 -> 9, 5
Atomic number 103 -> 9, 18
Atomic number 113 -> 7, 13
</pre>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
After making minor tweaks to two lines, the following program also works with jaq, the Rust implementation of jq.
 
See the disclaimer at [[#Wren|Wren]].
<syntaxhighlight lang=jq>
def limits: [[3,10], [11,18], [19,36], [37,54], [55,86], [87,118]];
 
def periodicTable(n):
if (n < 1 or n > 118) then "Atomic number is out of range." | error
elif n == 1 then [1, 1]
elif n == 2 then [1, 18]
elif (n >= 57 and n <= 71) then [8, n - 53]
elif (n >= 89 and n <= 103) then [9, n - 85]
else
first( range( 0; limits|length) as $i
| limits[$i] as $limit
| if (n >= $limit[0] and n <= $limit[1])
then {row: ($i + 2),
start: $limit[0],
end: $limit[1] }
else empty
end)
| if (n < .start + 2 or .row == 4 or .row == 5)
then [.row, n - .start + 1]
else [.row, n - .end + 18]
end
end;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
(1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113) as $n
| periodicTable($n) as [$r, $c]
| "Atomic number \($n|lpad(3)) -> \($r) \($c)"
 
</syntaxhighlight>
'''Invocation''': jq -nr -f periodic-table.jq
{{output}}
<pre>
Atomic number 1 -> 1 1
Atomic number 2 -> 1 18
Atomic number 29 -> 4 11
Atomic number 42 -> 5 6
Atomic number 57 -> 8 4
Atomic number 58 -> 8 5
Atomic number 59 -> 8 6
Atomic number 71 -> 8 18
Atomic number 72 -> 6 4
Atomic number 89 -> 9 4
Atomic number 90 -> 9 5
Atomic number 103 -> 9 18
Atomic number 113 -> 7 13
</pre>
 
=={{header|Julia}}==
Line 977 ⟶ 1,857:
 
[graphical representation of the periodic table positions]</pre>
 
=={{header|Nim}}==
{{trans|Wren}}
<syntaxhighlight lang="Nim">import std/strformat
 
const Limits = [3..10, 11..18, 19..36, 37..54, 55..86, 87..118]
 
func periodicTable(n: Positive): (int, int) =
doAssert n in 1..118, "Atomic number is out of range."
if n == 1: return (1, 1)
if n == 2: return (1, 18)
if n in 57..71: return (8, n - 53)
if n in 89..103: return (9, n - 85)
var row, start, stop = 0
for i, limit in Limits:
if n in limit:
row = i + 2
start = limit.a
stop = limit.b
break
if n < start + 2 or row == 4 or row == 5:
return (row, n - start + 1)
result = (row, n - stop + 18)
 
for n in [1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113]:
let (row, col) = periodicTable(n)
echo &"Atomic number {n:3} → {row}, {col}"
</syntaxhighlight>
 
{{out}}
<pre>Atomic number 1 → 1, 1
Atomic number 2 → 1, 18
Atomic number 29 → 4, 11
Atomic number 42 → 5, 6
Atomic number 57 → 8, 4
Atomic number 58 → 8, 5
Atomic number 59 → 8, 6
Atomic number 71 → 8, 18
Atomic number 72 → 6, 4
Atomic number 89 → 9, 4
Atomic number 90 → 9, 5
Atomic number 103 → 9, 18
Atomic number 113 → 7, 13
</pre>
 
=={{header|Perl}}==
Line 1,238 ⟶ 2,162:
 
</syntaxhighlight>
 
=={{header|Quackery}}==
 
A lookup table is precomputed at compile time from a representation of the periodic table.
 
<syntaxhighlight lang="Quackery"> [ dup 1 119 within not if
[ $ "Unknown element." fail ]
[ table 0
[ 118 times
[ i^ 1+
' [ 1 - - - - - - - - - - - - - - - - 2
3 4 - - - - - - - - - - 5 6 7 8 9 10
11 12 - - - - - - - - - - 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 - 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
87 88 - 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
- - - 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
- - - 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 ]
find 18 /mod 1+ dip 1+
join nested swap dip join ] ] now! ] ] is task ( n --> [ )
 
' [ 1 2 29 42 57 58 59 71 72 89 90 103 113 ]
witheach [ dup echo say " -> " task echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>1 -> [ 1 1 ]
2 -> [ 1 18 ]
29 -> [ 4 11 ]
42 -> [ 5 6 ]
57 -> [ 8 4 ]
58 -> [ 8 5 ]
59 -> [ 8 6 ]
71 -> [ 8 18 ]
72 -> [ 6 4 ]
89 -> [ 9 4 ]
90 -> [ 9 5 ]
103 -> [ 9 18 ]
113 -> [ 7 13 ]
</pre>
 
We can confirm that the lookup table was created during compilation by decompiling the word <code>task</code> with <code>' task copy unbuild echo$</code>.
 
{{out}}
 
<pre>[ dup 1 119 within not if [ [ ' [ 85 110 107 110 111 119 110 32 101 108 101 109 101 110 116 ] ] fail ] [ table 0 [ 1 1 ] [ 1 18 ] [ 2 1 ] [ 2 2 ] [ 2 13 ] [ 2 14 ] [ 2 15 ] [ 2 16 ] [ 2 17 ] [ 2 18 ] [ 3 1 ] [ 3 2 ] [ 3 13 ] [ 3 14 ] [ 3 15 ] [ 3 16 ] [ 3 17 ] [ 3 18 ] [ 4 1 ] [ 4 2 ] [ 4 3 ] [ 4 4 ] [ 4 5 ] [ 4 6 ] [ 4 7 ] [ 4 8 ] [ 4 9 ] [ 4 10 ] [ 4 11 ] [ 4 12 ] [ 4 13 ] [ 4 14 ] [ 4 15 ] [ 4 16 ] [ 4 17 ] [ 4 18 ] [ 5 1 ] [ 5 2 ] [ 5 3 ] [ 5 4 ] [ 5 5 ] [ 5 6 ] [ 5 7 ] [ 5 8 ] [ 5 9 ] [ 5 10 ] [ 5 11 ] [ 5 12 ] [ 5 13 ] [ 5 14 ] [ 5 15 ] [ 5 16 ] [ 5 17 ] [ 5 18 ] [ 6 1 ] [ 6 2 ] [ 8 4 ] [ 8 5 ] [ 8 6 ] [ 8 7 ] [ 8 8 ] [ 8 9 ] [ 8 10 ] [ 8 11 ] [ 8 12 ] [ 8 13 ] [ 8 14 ] [ 8 15 ] [ 8 16 ] [ 8 17 ] [ 8 18 ] [ 6 4 ] [ 6 5 ] [ 6 6 ] [ 6 7 ] [ 6 8 ] [ 6 9 ] [ 6 10 ] [ 6 11 ] [ 6 12 ] [ 6 13 ] [ 6 14 ] [ 6 15 ] [ 6 16 ] [ 6 17 ] [ 6 18 ] [ 7 1 ] [ 7 2 ] [ 9 4 ] [ 9 5 ] [ 9 6 ] [ 9 7 ] [ 9 8 ] [ 9 9 ] [ 9 10 ] [ 9 11 ] [ 9 12 ] [ 9 13 ] [ 9 14 ] [ 9 15 ] [ 9 16 ] [ 9 17 ] [ 9 18 ] [ 7 4 ] [ 7 5 ] [ 7 6 ] [ 7 7 ] [ 7 8 ] [ 7 9 ] [ 7 10 ] [ 7 11 ] [ 7 12 ] [ 7 13 ] [ 7 14 ] [ 7 15 ] [ 7 16 ] [ 7 17 ] [ 7 18 ] ] ]
 
</pre>
 
=={{header|Raku}}==
Line 1,258 ⟶ 2,231:
90: 9, 5
103: 9, 18</pre>
 
=={{header|Scheme}}==
The following is a minimal recursive implementation. It calculates the position of the requested element by the position of the previous element.
<syntaxhighlight lang="scheme">(define (position-increment n)
(cond
((= n 1) '( 0 . 17))
((= n 2) '( 1 . -17))
((= n 4) '( 0 . 11))
((= n 10) '( 1 . -17))
((= n 12) '( 0 . 11))
((= n 18) '( 1 . -17))
((= n 36) '( 1 . -17))
((= n 54) '( 1 . -17))
((= n 56) '( 2 . 2))
((= n 71) '(-2 . -14))
((= n 86) '( 1 . -17))
((= n 88) '( 2 . 2))
((= n 103) '(-2 . -14))
(else '( 0 . 1))))
 
(define (move p i)
(cons (+ (car p) (car i))
(+ (cdr p) (cdr i))))
 
(define (position n)
(if (= n 1)
'(1 . 1)
(let ((m (- n 1)))
(move (position m)
(position-increment m)))))
 
(define (format-line n p)
(display n)
(display " -> ")
(display (car p))
(display " ")
(display (cdr p))
(newline))
 
(for-each (lambda (n)
(format-line n (position n)))
(list 1 2 29 42 57 58 72 89))</syntaxhighlight>
For successive calculations the above code is inefficient, because the position of 58 gets calculated by recalculating the position of 57, although is has already been calculated in the previous step. This can be enhanced by the use of memoization. The result of each calculation gets stored and whenever a position has to be calculated, the already calculated value gets used instead.
<syntaxhighlight lang="scheme">(define position*
(let ((memo (make-vector 118 #f)))
(lambda (n)
(let* ((mi (- n 1))
(mp (vector-ref memo mi)))
(or mp
(let ((p (position n)))
(vector-set! memo mi p)
p))))))</syntaxhighlight>
{{out}}
<pre>1 -> 1 1
2 -> 1 18
29 -> 4 11
42 -> 5 6
57 -> 8 4
58 -> 8 5
72 -> 6 4
89 -> 9 4</pre>
 
=={{header|V (Vlang)}}==
{{trans|Wren}}
<syntaxhighlight lang="Zig">
import log
 
const limits = [[3, 10], [11, 18], [19, 36], [37, 54], [55, 86], [87, 118]]
 
fn main() {
for n in [1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113] {
row, col := periodic_table(n)
println("Atomic number ${n} -> ${row}, ${col}")
}
}
 
fn periodic_table(n int) (int, int) {
mut logged := log.Log{}
mut limit := []int{}
mut row, mut start, mut end := 0, 0, 0
if n < 1 || n > 118 {logged.fatal("Atomic number is out of range.")}
if n == 1 {return 1, 1}
if n == 2 {return 1, 18}
if n >= 57 && n <= 71 {return 8, n - 53}
if n >= 89 && n <= 103 {return 9, n - 85}
for i := 0; i < limits.len; i++ {
limit = limits[i]
if n >= limit[0] && n <= limit[1] {
row, start, end = i + 2, limit[0], limit[1]
break
}
}
if n < start + 2 || row == 4 || row == 5 {return row, n - start + 1}
return row, n - end + 18
}
</syntaxhighlight>
 
{{out}}
<pre>
Atomic number 1 -> 1, 1
Atomic number 2 -> 1, 18
Atomic number 29 -> 4, 11
Atomic number 42 -> 5, 6
Atomic number 57 -> 8, 4
Atomic number 58 -> 8, 5
Atomic number 59 -> 8, 6
Atomic number 71 -> 8, 18
Atomic number 72 -> 6, 4
Atomic number 89 -> 9, 4
Atomic number 90 -> 9, 5
Atomic number 103 -> 9, 18
Atomic number 113 -> 7, 13
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
There is a discrepancy between how the periodic table is arranged in the Wikipedia article and how it is arranged in the task description. I've used the latter in the following script.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var limits = [3..10, 11..18, 19..36, 37..54, 55..86, 87..118]
9,476

edits