Periodic table
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Display the row and column in the periodic table of the given atomic number.
- The periodic table
Let us consider the following periodic table representation.
__________________________________________________________________________ | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | | | |1 H He | | | |2 Li Be B C N O F Ne | | | |3 Na Mg Al Si P S Cl Ar | | | |4 K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr | | | |5 Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe | | | |6 Cs Ba * Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn | | | |7 Fr Ra ° Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og | |__________________________________________________________________________| | | | | |8 Lantanoidi* La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu | | | |9 Aktinoidi° Ak Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr | |__________________________________________________________________________|
- Example test cases;
-
1
->1 1
-
2
->1 18
-
29
->4 11
-
42
->5 6
-
57
->8 4
-
58
->8 5
-
72
->6 4
-
89
->9 4
- 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.
- See also
- the periodic table
- This task was an idea from CompSciFact
- The periodic table in ascii that was used as template
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 ‘’))
- Output:
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)
6502 Assembly
A lookup table is the simplest solution, for the following reasons:
- The input value is guaranteed to be between 0 and 255
- The data doesn't fit a pattern that the CPU can easily take advantage of.
Since the 6502 can't index an array larger than 256 bytes, we'll store all the "low bytes" in one table and all the "high bytes" in another. Both tables share the same index, so this lets us store up to 255 possible elements while taking the same amount of memory as a single table of 16-bit values. Right now, we can do this either way, but since we're close to 128 elements, may as well future-proof the code, right?
Lookup: ;INPUT: X = atomic number of the element of interest.
LDA PeriodicTable_Column,x
STA $20 ;store column number in memory (I chose $20 arbitrarily, you can store it anywhere)
LDA PeriodicTable_Row,x
STA $21 ;store row number in memory
RTS
PeriodicTable_Column:
db $ff,$01,$18,$01,$02,$13,$14,$15,$16,$17,$18,... ;I don't need to write them all out, the concept is self-explanatory enough.
PeriodicTable_Row:
db $ff,$01,$01,$02,$02,$02,$02,$02,$02,$02,$02,...
68000 Assembly
A lookup table is the simplest solution, as the data of interest doesn't have a pattern that a computer can take advantage of easily. It's quicker than using a formula, but takes up more memory as a result.
The table consists of 118 16-bit values. The high byte is the row number, the low byte is the column number. Both are stored as binary-coded decimal (i.e. hex values that look like base 10 numbers.)
Lookup:
;input: D0.W = the atomic number of interest.
LEA PeriodicTable,A0
ADD.W D0,D0 ;we're indexing a table of words, so double the index.
MOVE.W (A0,D0),D0 ;D0.W contains row number in the high byte and column number in the low byte.
RTS
PeriodicTable:
DC.W $FFFF ;padding since arrays start at zero in assembly.
DC.W $0101 ;HYDROGEN
DC.W $0118 ;HELIUM
DC.W $0201 ;LITHIUM
DC.W $0202 ;BERYLLIUM
DC.W $0213 ;BORON
DC.W $0214 ;CARBON
DC.W $0215 ;NITROGEN
DC.W $0216 ;OXYGEN
DC.W $0217 ;FLUORINE
DC.W $0218 ;NEON
;etc.
ALGOL 68
BEGIN # display the period and group number of an element, #
# given its atomic number #
INT max atomic number = 118; # highest known element #
# the positions are stored as: #
# ( group number * group multiplier ) + period #
INT group multiplier = 100;
[ 1 : max atomic number ]INT position;
# construct the positions of the elements in the table #
BEGIN
STRING periodic table = "- ="
+ "-- -----="
+ "-- -----="
+ "-----------------="
+ "-----------------="
+ "--8--------------="
+ "--9--------------="
;
INT period := 1;
INT group := 1;
INT element := 1;
FOR t FROM LWB periodic table TO UPB periodic table DO
CHAR p = periodic table[ t ];
IF p = "8" OR p = "9" THEN
# lantanoids or actinoids #
INT series period = IF p = "8" THEN 8 ELSE 9 FI;
INT series group := 4;
FOR e TO 15 DO
position[ element ] := ( group multiplier * series group ) + series period;
element +:= 1;
series group +:= 1
OD
ELIF p /= " " THEN
# there is a single element here #
position[ element ] := ( group multiplier * group ) + period;
element +:= 1;
IF p = "=" THEN
# final element of the period #
period +:= 1;
group := 0
FI
FI;
group +:= 1
OD
END;
# display the period and group numbers of test elements #
[]INT test = ( 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113 );
FOR t FROM LWB test TO UPB test DO
INT e = test[ t ];
IF e < LWB position OR e > UPB position THEN
print( ( "Invalid element: ", whole( e, 0 ), newline ) )
ELSE
INT period = position[ e ] MOD group multiplier;
INT group = position[ e ] OVER group multiplier;
print( ( "Element ", whole( e, -3 )
, " -> ", whole( period, 0 ), ", ", whole( group, -2 )
, newline
)
)
FI
OD
END
- Output:
Element 1 -> 1, 1 Element 2 -> 1, 18 Element 29 -> 4, 11 Element 42 -> 5, 6 Element 57 -> 8, 4 Element 58 -> 8, 5 Element 59 -> 8, 6 Element 71 -> 8, 18 Element 72 -> 6, 4 Element 89 -> 9, 4 Element 90 -> 9, 5 Element 103 -> 9, 18 Element 113 -> 7, 13
BASIC
Applesoft BASIC
This program borrows from the Python solution but only PRINTs the results of the tests shown in the task. Each row and column from the tests are PLOTted in a COLORful table.
0 GR:HOME:COLOR=11:FORR=1TO7:FORC=1TO2:GOSUB7:NEXTC,R:COLOR=7:FORR=4TO7:FORC=3+(R>5)TO12:GOSUB7:NEXTC,R:COLOR=13:FORR=2TO7:FORC=13TO18:GOSUB7:NEXTC,R
1 forr=2to7:forc=13to18:GOSUB7:NEXTC,R:COLOR=14:R=8:FORC=4TO18:GOSUB7:NEXTC:COLOR=12:R=9:FORC=4TO18:GOSUB7:NEXTC:R=9:FORC=4TO18:GOSUB7:NEXTC:Z=2:R=7:C=3:GOSUB7:COLOR=14:R=6:C=3:GOSUB7:COLOR=15
2 S=14:W=18:FORI=1TO7:READN(I),I(I):NEXT:DATA2,0,10,0,18,0,36,0,54,0,86,57,118,89,1,1,1,2,1,18,29,4,11,42,5,6,57,8,4,58,8,5,72,6,4,89,9,4,59,8,6,71,8,18,90,9,5,103,9,18
3 FORT=1TO8:READA,Y,X:GOSUB4:PRINTRIGHT$(" "+STR$(A),3)"->"R" "LEFT$(STR$(C)+" ",3);:GOSUB7:NEXTT:VTAB23:END
4 N=0:FORR=1TO7:P=N:N=N(R):IFA>NTHEN:NEXTR
5 E=N-P:K=A-P:IFI(R)AND(I(R)<=AANDA<=I(R)+S)THENR=R+2:C=K+1:RETURN
6 E=W-E:L=1+(N>2):C=K+E*(K>L):RETURN
7 K=C+(R=1ANDC=2)*16:VLINR*4+Z,R*4+2ATK*2+1:RETURN
ASIC
REM Periodic table
DIM A(7)
DIM B(7)
REM Arrays A, B.
DATA 1, 2, 5, 13, 57, 72, 89, 104
DATA -1, 15, 25, 35, 72, 21, 58, 7
REM Example elements (atomic numbers).
DATA 1, 2, 29, 42, 57, 58, 72, 89, 90, 103
GOSUB SetAB:
FOR J = 0 TO 9
READ AtomicNum
GOSUB ShowRowAndColumn:
NEXT J
END
SetAB:
FOR I = 0 TO 7
READ A(I)
NEXT I
FOR I = 0 TO 7
READ B(I)
NEXT I
RETURN
ShowRowAndColumn:
I = 7
WHILE A(I) > AtomicNum
I = I - 1
WEND
M = AtomicNum + B(I)
R = M / 18
R = R + 1
C = M MOD 18
C = C + 1
PRINT AtomicNum;
PRINT " ->";
PRINT R;
PRINT C
RETURN
- Output:
1 -> 1 1 2 -> 1 18 29 -> 4 11 42 -> 5 6 57 -> 8 4 58 -> 8 5 72 -> 6 4 89 -> 9 4 90 -> 9 5 103 -> 9 18
BASIC256
subroutine MostarPos(N)
dim A = { 1, 2, 5, 13, 57, 72, 89, 104}
dim B = {-1, 15, 25, 35, 72, 21, 58, 7}
I = 7
while A[I] > N
I -= 1
end while
M = N + B[I]
R = (M \ 18) +1
C = (M % 18) +1
print "Atomic number "; rjust(N,3); "-> "; R ; ", "; C
end subroutine
dim Element = {1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113}
for I = 0 to Element[?]-1
call MostarPos(Element[I])
next I
- Output:
Same as FreeBASIC entry.
Chipmunk Basic
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
- Output:
Same as GW-BASIC entry.
Craft 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
FreeBASIC
Sub MostarPos(N As Integer)
Dim As Integer M, I, R, C
Dim As Integer A(0 To 7) = { 1, 2, 5, 13, 57, 72, 89, 104} 'magic numbers
Dim As Integer B(0 To 7) = {-1, 15, 25, 35, 72, 21, 58, 7}
I = 7
While A(I) > N
I -= 1
Wend
M = N + B(I)
R = (M \ 18) +1
C = (M Mod 18) +1
Print Using "Atomic number ### -> #_, ##"; N; R; C
End Sub
Dim As Integer Element(0 To 12) = {1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113}
For I As Integer = 0 To Ubound(Element)
MostarPos(Element(I))
Next I
- Output:
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
FTCBASIC
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
Gambas
Sub MostarPos(N As Integer) 'Mostrar fila y columna para el elemento
Dim M, I, R, C As Integer
Dim A As Integer[] = [1, 2, 5, 13, 57, 72, 89, 104] 'magic numbers
Dim B As Integer[] = [-1, 15, 25, 35, 72, 21, 58, 7]
I = 7
While A[I] > N
Dec I
Wend
M = N + B[I]
R = (M \ 18) + 1
C = (M Mod 18) + 1
Print "Atomic number "; Format(N, "###"); " -> "; R; ", "; C
End
Public Sub Main()
Dim Element As Integer[] = [1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113]
For e As Integer = 0 To 12
MostarPos(Element[e])
Next
End
- Output:
Same as FreeBASIC entry.
GW-BASIC
10 REM Periodic table
20 DIM A(7), B(7)
30 GOSUB 200
40 FOR J% = 0 TO 9
50 READ ANUM%: GOSUB 400
60 NEXT J%
70 END
190 REM Set arrays A, B.
200 FOR I% = 0 TO 7: READ A(I%): NEXT I%
210 FOR I% = 0 TO 7: READ B(I%): NEXT I%
220 RETURN
390 REM Show row and column for element
400 I% = 7
410 WHILE A(I%) > ANUM%
420 I% = I% - 1
430 WEND
440 M% = ANUM% + B(I%)
450 R% = M% \ 18 + 1
460 C% = M% MOD 18 + 1
470 PRINT ANUM%;"->";R%;C%
480 RETURN
990 REM Data
1000 REM Arrays A, B.
1010 DATA 1, 2, 5, 13, 57, 72, 89, 104
1020 DATA -1, 15, 25, 35, 72, 21, 58, 7
1030 REM Example elements (atomic numbers).
1040 DATA 1, 2, 29, 42, 57, 58, 72, 89, 90, 103
- Output:
1 -> 1 1 2 -> 1 18 29 -> 4 11 42 -> 5 6 57 -> 8 4 58 -> 8 5 72 -> 6 4 89 -> 9 4 90 -> 9 5 103 -> 9 18
Minimal BASIC
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 DIM A(7), B(7)
210 FOR I = 0 TO 7
220 READ A(I)
230 NEXT I
240 FOR I = 0 TO 7
250 READ B(I)
260 NEXT I
270 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
990 REM Data.
1000 REM Arrays A, B.
1010 DATA 1, 2, 5, 13, 57, 72, 89, 104
1020 DATA -1, 15, 25, 35, 72, 21, 58, 7
1030 REM Example elements (atomic numbers).
1040 DATA 1, 2, 29, 42, 57, 58, 72, 89, 90, 103
Nascom BASIC
10 REM Periodic table
20 GOSUB 200
30 FOR J=0 TO 9:READ ANUM:GOSUB 400:NEXT J
40 END
190 REM ** Set arrays A, B.
200 DIM A(7),B(7)
210 FOR I=0 TO 7:READ A(I):NEXT I
220 FOR I=0 TO 7:READ B(I):NEXT I
230 RETURN
390 REM ** Show row and column for element
400 I=7
410 IF A(I)>ANUM THEN I=I-1:GOTO 410
420 M=ANUM+B(I)
430 R=INT(M/18)+1
440 C=M-INT(M/18)*18+1
450 PRINT ANUM;"->";R;C
460 RETURN
990 REM ** Data.
1000 REM ** Arrays A, B.
1010 DATA 1,2,5,13,57,72,89,104
1020 DATA -1,15,25,35,72,21,58,7
1030 REM ** Example elements (atomic numbers).
1040 DATA 1,2,29,42,57,58,72,89,90,103
- Output:
1 -> 1 1 2 -> 1 18 29 -> 4 11 42 -> 5 6 57 -> 8 4 58 -> 8 5 72 -> 6 4 89 -> 9 4 90 -> 9 5 103 -> 9 18
QBasic
SUB MostarPos (N)
DIM a(7)
RESTORE a:
FOR x = 0 TO 7: READ a(x): NEXT x
DIM b(7)
RESTORE b:
FOR x = 0 TO 7: READ b(x): NEXT x
I = 7
WHILE a(I) > N
I = I - 1
WEND
M = N + b(I)
R = (M \ 18) + 1
C = (M MOD 18) + 1
PRINT USING "Atomic number ### -> #_, ##"; N; R; C
END SUB
DIM Element(0 TO 12)
RESTORE elements
elements:
DATA 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113
FOR x = 0 TO 12: READ Element(x): NEXT x
FOR I = 0 TO UBOUND(Element)
MostarPos (Element(I))
NEXT I
a:
DATA 1, 2, 5, 13, 57, 72, 89, 104
b:
DATA -1, 15, 25, 35, 72, 21, 58, 7
- Output:
Same as FreeBASIC entry.
Run BASIC
dim Element(12)
Element(0) = 1
Element(1) = 2
Element(2) = 29
Element(3) = 42
Element(4) = 57
Element(5) = 58
Element(6) = 59
Element(7) = 71
Element(8) = 72
Element(9) = 89
Element(10) = 90
Element(11) = 103
Element(12) = 113
for e = 0 to 12
call MostarPos Element(e)
next e
sub MostarPos N
dim A(7)
A(0) = 1
A(1) = 2
A(2) = 5
A(3) = 13
A(4) = 57
A(5) = 72
A(6) = 89
A(7) = 104
dim B(7)
B(0) = -1
B(1) = 15
B(2) = 25
B(3) = 35
B(4) = 72
B(5) = 21
B(6) = 58
B(7) = 7
I = 7
while A(I) > N
I = I - 1
wend
M = N + B(I)
R = int(M / 18) +1
C = (M mod 18) +1
print "Atomic number "; using("###", N); " -> "; R; ", "; C
end sub
- Output:
Same as FreeBASIC entry.
True BASIC
SUB MostarPos (n)
DIM a(0 TO 7)
LET a(0) = 1
LET a(1) = 2
LET a(2) = 5
LET a(3) = 13
LET a(4) = 57
LET a(5) = 72
LET a(6) = 89
LET a(7) = 104
DIM b(0 TO 7)
LET b(0) = -1
LET b(1) = 15
LET b(2) = 25
LET b(3) = 35
LET b(4) = 72
LET b(5) = 21
LET b(6) = 58
LET b(7) = 7
LET i = 7
DO WHILE a(i) > n
LET i = i - 1
LOOP
LET m = n + b(i)
LET r = IP(m / 18) + 1
LET c = REMAINDER(m, 18) + 1
PRINT USING "Atomic number ###": n;
PRINT " ->"; r; c
END SUB
DIM element(0 TO 12)
LET element(0) = 1
LET element(1) = 2
LET element(2) = 29
LET element(3) = 42
LET element(4) = 57
LET element(5) = 58
LET element(6) = 59
LET element(7) = 71
LET element(8) = 72
LET element(9) = 89
LET element(10) = 90
LET element(11) = 103
LET element(12) = 113
FOR e = 0 TO UBOUND(element)
CALL MostarPos (element(e))
NEXT e
END
- Output:
Similar to FreeBASIC entry.
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
- Output:
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
XBasic
PROGRAM "Periodic table"
DECLARE FUNCTION Entry ()
DECLARE FUNCTION MostarPos (N)
FUNCTION Entry ()
DIM Element[12]
Element[0] = 1
Element[1] = 2
Element[2] = 29
Element[3] = 42
Element[4] = 57
Element[5] = 58
Element[6] = 59
Element[7] = 71
Element[8] = 72
Element[9] = 89
Element[10] = 90
Element[11] = 103
Element[12] = 113
FOR e = 0 TO 12 'UBOUND (Element())
MostarPos (Element[e])
NEXT
END FUNCTION
FUNCTION MostarPos (N)
DIM A[7]
A[0] = 1
A[1] = 2
A[2] = 5
A[3] = 13
A[4] = 57
A[5] = 72
A[6] = 89
A[7] = 104
DIM B[7]
B[0] = -1
B[1] = 15
B[2] = 25
B[3] = 35
B[4] = 72
B[5] = 21
B[6] = 58
B[7] = 7
I = 7
DO WHILE A[I] > N
DEC I
LOOP
M = N + B[I]
R = (M \ 18) + 1
C = (M MOD 18) + 1
PRINT "Atomic number "; FORMAT$ ("###", N); " ->"; R; ","; C
END FUNCTION
END PROGRAM
- Output:
Similar to FreeBASIC entry.
Yabasic
// Rosetta Code problem: http://rosettacode.org/wiki/Periodic_table
// by Jjuanhdez, 06/2022
dim Element(12)
Element(0) = 1 : Element(1) = 2
Element(2) = 29 : Element(3) = 42
Element(4) = 57 : Element(5) = 58
Element(6) = 59 : Element(7) = 71
Element(8) = 72 : Element(9) = 89
Element(10) = 90
Element(11) = 103 : Element(12) = 113
for e = 0 to arraysize(Element(),1)
MostarPos (Element(e))
next e
end
sub MostarPos (N)
dim A(7)
A(0) = 1 : A(1) = 2
A(2) = 5 : A(3) = 13
A(4) = 57 : A(5) = 72
A(6) = 89 : A(7) = 104
dim B(7)
B(0) = -1 : B(1) = 15
B(2) = 25 : B(3) = 35
B(4) = 72 : B(5) = 21
B(6) = 58 : B(7) = 7
I = 7
while A(I) > N
I = I - 1
wend
M = N + B(I)
R = int(M / 18) +1
C = mod(M, 18) +1
print "Atomic number ", N using("###"), " -> ", R, ", ", C
end sub
- Output:
Same as FreeBASIC entry.
C
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
#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;
}
- Output:
Initial screen:
Element selected:
File: chem_table.txt
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
FutureBasic
_window = 1
void local fn BuildPeriodicTableArrays
CFArrayRef periodicArr = @[@"",¬
@"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", @"Lu", @"Hf", @"Ta", @"W", @"Re", @"Os", @"Ir", @"Pt", @"Au", @"Hg", @"Tl", @"Pb", @"Bi", @"Po", @"At", @"Rn",¬
@"Fr", @"Ra", @"Lr", @"Rf", @"Db", @"Sg", @"Bh", @"Hs", @"Mt", @"Ds", @"Rg", @"Cn", @"Nh", @"Fl", @"Mc", @"Lv", @"Ts", @"Og",¬
@"", @"", @"La", @"Ce", @"Pr", @"Nd", @"Pm", @"Sm", @"Eu", @"Gd", @"Tb", @"Dy", @"Ho", @"Er", @"Tm", @"Yb", @"", @"",¬
@"", @"", @"Ac", @"Th", @"Pa", @"U", @"NP", @"Pu", @"Am", @"Cm", @"Bk", @"Cf", @"Es", @"Fm", @"Md", @"No", @"", @""]
AppSetProperty( @"periodicTable", periodicArr )
CFArrayRef numbersArr = @[@"",¬
@"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", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86",¬
@"87", @"88", @"103", @"104", @"105", @"106", @"107", @"108", @"109", @"110", @"111", @"112", @"113", @"114", @"114", @"116", @"117", @"118",¬
@"", @"", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"59", @"70", @"", @"",¬
@"", @"", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100", @"101", @"102", @"", @""]
AppSetProperty( @"periodicNumbers", numbersArr )
end fn
void local fn BuildWindow
NSInteger i, j, row
CGRect r
CFArrayRef periodicArr, numbersArr
CFStringRef tempStr
periodicArr = fn AppProperty( @"periodicTable" )
numbersArr = fn AppProperty( @"periodicNumbers" )
window _window, @"Periodic Table", ( 0, 0, 700, 400 )
WindowSetBackgroundColor( _window, fn ColorWhite )
j = 0 : row = 350
r = fn CGRectMake( 10, row, 36, 40 )
for i = 1 to 162
if fn StringIsEqual( periodicArr[i], @"" ) then tempStr = @"" else tempStr = fn StringWithFormat( @"%@\n%@", numbersArr[i], periodicArr[i] )
textfield i,, tempStr, r, _window
TextFieldSetBackgroundColor( i, fn ColorBlue )
TextFieldSetTextColor( i, fn ColorWhite )
ControlSetFontWithName( i, @"Menlo", 12.0 )
ControlSetAlignment(i, NSTextAlignmentCenter )
r = fn CGRectOffset( r, 38, 0 )
j++
if ( j == 18 )
row = row - 42
r = fn CGRectMake( 10, row, 36, 40 )
j = 0
end if
next
for i = 1 to 162
if fn StringIsEqual( fn ControlStringValue( i ), @"" ) then ViewRemoveFromSuperview( i )
next
end fn
fn BuildPeriodicTableArrays
fn BuildWindow
HandleEvents
- Output:
Go
package main
import (
"fmt"
"log"
)
var limits = [][2]int{
{3, 10}, {11, 18}, {19, 36}, {37, 54}, {55, 86}, {87, 118},
}
func periodicTable(n int) (int, int) {
if n < 1 || n > 118 {
log.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
}
var row, start, end int
for i := 0; i < len(limits); 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
}
func main() {
for _, n := range []int{1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113} {
row, col := periodicTable(n)
fmt.Printf("Atomic number %3d -> %d, %-2d\n", n, row, col)
}
}
- Output:
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
J
Basically, here, we want a lookup table. For example:
PT=: (' ',.~[;._2) {{)n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 H He
2 Li Be B C N O F Ne
3 Na Mg Al Si P S Cl Ar
4 K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr
5 Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe
6 Cs Ba * Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn
7 Fr Ra - Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og
8 Lantanoidi* La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu
9 Aktinoidi- Ak Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr
}}
ptrc=: {{
tokens=. (#~ (3>#)@> * */@(tolower~:toupper)@>) ~.,;:,PT
ndx=. (>' ',L:0' ',L:0~tokens) {.@I.@E."1 ,PT
Lantanoidi=. ndx{+/\'*'=,PT
Aktinoidi=. ndx{+/\'-'=,PT
j=. 13|3*Lantanoidi+3*Aktinoidi
k=. {:$PT
0,}."1/:~j,.(<.ndx%k),.1+(/:~@~. i. ])k|ndx
}}
rowcol=: ptrc''
In other words, start with a hand crafted representation of the periodic table. Elements here are tokens with 1 or 2 letters. Locate the position of each token in the table. Get an initial row and column number from the character positions in the table. Translate character column to periodic table column by enumerating the unique (sorted) list of column numbers and using the index in that list. Character row was already periodic table row. Most elements here were already in atomic number order, and we can fix the exceptions by temporarily prefixing each row,col value and sorting. (Here, we use 0 for the first 56 elements, 3 for the next 17 elements (after Lantanoidi, before Aktinoidi), 12 for the next 15 (after Aktinoidi), 2 for the next 15 (the Lantanoidi) and 11 for the final 15 elements (the Aktinoidi).)
Thus:
1 2 29 42 57 58 72 89 { rowcol
1 1
1 18
4 11
5 6
8 4
8 5
6 4
9 4
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.
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)"
Invocation: jq -nr -f periodic-table.jq
- Output:
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
Julia
const limits = [3:10, 11:18, 19:36, 37:54, 55:86, 87:118]
function periodic_table(n)
(n < 1 || n > 118) && error("Atomic number is out of range.")
n == 1 && return [1, 1]
n == 2 && return [1, 18]
57 <= n <= 71 && return [8, n - 53]
89 <= n <= 103 && return [9, n - 85]
row, limitstart, limitstop = 0, 0, 0
for i in eachindex(limits)
if limits[i].start <= n <= limits[i].stop
row, limitstart, limitstop = i + 1, limits[i].start, limits[i].stop
break
end
end
return (n < limitstart + 2 || row == 4 || row == 5) ?
[row, n - limitstart + 1] : [row, n - limitstop + 18]
end
for n in [1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113]
rc = periodic_table(n)
println("Atomic number ", lpad(n, 3), " -> ($(rc[1]), $(rc[2]))")
end
- Output:
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)
Mathematica /Wolfram Language
Mathematica and the Wolfram language include the period and group in the function ElementData but has slightly different definitions for the lantanides and aktinoides.
ClearAll[FindPeriodGroup]
FindPeriodGroup[n_Integer] := Which[57 <= n <= 70,
{8, n - 53}
,
89 <= n <= 102,
{9, n - 85}
,
1 <= n <= 118,
{ElementData[n, "Period"], ElementData[n, "Group"]}
,
True,
Missing["Element does not exist"]
]
Row[{"Element ", #, " -> ", FindPeriodGroup[#]}] & /@ {1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113} // Column
Graphics[Text[#, {1, -1} Reverse@FindPeriodGroup[#]] & /@ Range[118]]
- Output:
Element 1 -> {1,1} Element 2 -> {1,18} Element 29 -> {4,11} Element 42 -> {5,6} Element 57 -> {8,4} Element 58 -> {8,5} Element 59 -> {8,6} Element 71 -> {6,3} Element 72 -> {6,4} Element 89 -> {9,4} Element 90 -> {9,5} Element 103 -> {7,3} Element 113 -> {7,13} [graphical representation of the periodic table positions]
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}"
- Output:
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
Perl
use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
use List::Util <sum head>;
sub divmod { int $_[0]/$_[1], $_[0]%$_[1] }
my $b = 18;
my(@offset,@span,$cnt);
push @span, ($cnt++) x $_ for <1 3 8 44 15 17 15 15>;
@offset = (16, 10, 10, (2*$b)+1, (-2*$b)-15, (2*$b)+1, (-2*$b)-15);
for my $n (<1 2 29 42 57 58 72 89 90 103 118>) {
printf "%3d: %2d, %2d\n", $n, map { $_+1 } divmod $n-1 + sum(head $span[$n-1], @offset), $b;
}
- Output:
1: 1, 1 2: 1, 18 29: 4, 11 42: 5, 6 57: 8, 4 58: 8, 5 72: 6, 4 89: 9, 4 90: 9, 5 103: 9, 18
Phix
with javascript_semantics constant match_wp = false function prc(integer n) constant t = {0,2,10,18,36,54,86,118,119} integer row = abs(binary_search(n,t,true))-1, col = n-t[row] if col>1+(row>1) then col = 18-(t[row+1]-n) if match_wp then if col<=2 then return {row+2,col+14} end if else -- matches above ascii: if col<=2+(row>5) then return {row+2,col+15} end if end if end if return {row,col} end function sequence pt = repeat(repeat(" ",19),10) pt[1][2..$] = apply(true,sprintf,{{"%3d"},tagset(18)}) -- column headings for i=1 to 9 do pt[i+1][1] = sprintf("%3d",i) end for -- row numbers for i=1 to 118 do integer {r,c} = prc(i) pt[r+1][c+1] = sprintf("%3d",i) end for if not match_wp then -- (ascii only:) pt[7][4] = " L*" pt[8][4] = " A*" pt[9][2..4] = {"Lanthanide:"} pt[10][2..4] = {" Actinide:"} end if printf(1,"%s\n",{join(apply(true,join,{pt,{"|"}}),"\n")})
- Output:
With match_wp set to true:
| 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| 71| 72| 73| 74| 75| 76| 77| 78| 79| 80| 81| 82| 83| 84| 85| 86 7| 87| 88|103|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| | 9| | | 89| 90| 91| 92| 93| 94| 95| 96| 97| 98| 99|100|101|102| |
Or with match_wp false:
| 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| L*| 72| 73| 74| 75| 76| 77| 78| 79| 80| 81| 82| 83| 84| 85| 86 7| 87| 88| A*|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118 8|Lanthanide:| 57| 58| 59| 60| 61| 62| 63| 64| 65| 66| 67| 68| 69| 70| 71 9| Actinide:| 89| 90| 91| 92| 93| 94| 95| 96| 97| 98| 99|100|101|102|103
alternate
constant ptxt = """ __________________________________________________________________________ | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | | | |1 H He | | | |2 Li Be B C N O F Ne | | | |3 Na Mg Al Si P S Cl Ar | | | |4 K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr | | | |5 Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe | | | |6 Cs Ba * Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn | | | |7 Fr Ra + Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og | |__________________________________________________________________________| | | | | |8 Lantanoidi* La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu | | | |9 Aktinoidi+ Ak Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr | |__________________________________________________________________________| """ function tablify(string ptxt) sequence lines = split(ptxt,"\n"), res = {} for l in lines do integer ln = l[2]-'0', c = 0 if ln>=1 and ln<=9 then res = append(res,{}) for j=5 to length(l) by 4 do c += 1 if l[j]>='A' and l[j+2]<=' ' then res[$] = append(res[$],{trim(l[j..j+1]),ln,c}) end if end for end if end for res[7][3..2] = res[9] res[6][3..2] = res[8] res = join(res[1..7],{},{}) return res end function constant pt = apply(true,sprintf,{{"(%s) is at %d, %d"},tablify(ptxt)}) for e in {1,2,29,42,57,58,59,71,72,89,90,103,113} do printf(1,"Element %d %s\n",{e,pt[e]}) end for
- Output:
Element 1 (H) is at 1, 1 Element 2 (He) is at 1, 18 Element 29 (Cu) is at 4, 11 Element 42 (Mo) is at 5, 6 Element 57 (La) is at 8, 4 Element 58 (Ce) is at 8, 5 Element 59 (Pr) is at 8, 6 Element 71 (Lu) is at 8, 18 Element 72 (Hf) is at 6, 4 Element 89 (Ak) is at 9, 4 Element 90 (Th) is at 9, 5 Element 103 (Lr) is at 9, 18 Element 113 (Nh) is at 7, 13
PHP
<?php
// Periodic table
class PeriodicTable
{
private $aArray = array(1, 2, 5, 13, 57, 72, 89, 104);
private $bArray = array(-1, 15, 25, 35, 72, 21, 58, 7);
public function rowAndColumn($n)
{
$i = 7;
while ($this->aArray[$i] > $n)
$i--;
$m = $n + $this->bArray[$i];
return array(floor($m / 18) + 1, $m % 18 + 1);
}
}
$pt = new PeriodicTable();
// Example elements (atomic numbers).
foreach ([1, 2, 29, 42, 57, 58, 72, 89, 90, 103] as $n) {
list($r, $c) = $pt->rowAndColumn($n);
echo $n, " -> ", $r, " ", $c, PHP_EOL;
}
?>
- Output:
1 -> 1 1 2 -> 1 18 29 -> 4 11 42 -> 5 6 57 -> 8 4 58 -> 8 5 72 -> 6 4 89 -> 9 4 90 -> 9 5 103 -> 9 18
Python
A solution trying hard not to encode too much data about the table.
def perta(atomic) -> (int, int):
NOBLES = 2, 10, 18, 36, 54, 86, 118
INTERTWINED = 0, 0, 0, 0, 0, 57, 89
INTERTWINING_SIZE = 14
LINE_WIDTH = 18
prev_noble = 0
for row, noble in enumerate(NOBLES):
if atomic <= noble: # we are at the good row. We now need to determine the column
nb_elem = noble - prev_noble # number of elements on that row
rank = atomic - prev_noble # rank of the input element among elements
if INTERTWINED[row] and INTERTWINED[row] <= atomic <= INTERTWINED[row] + INTERTWINING_SIZE: # lantanides or actinides
row += 2
col = rank + 1
else: # not a lantanide nor actinide
# handle empty spaces between 1-2, 4-5 and 12-13.
nb_empty = LINE_WIDTH - nb_elem # spaces count as columns
inside_left_element_rank = 2 if noble > 2 else 1
col = rank + (nb_empty if rank > inside_left_element_rank else 0)
break
prev_noble = noble
return row+1, col
# small test suite
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),
}
for input, out in TESTS.items():
found = perta(input)
print('TEST:{:3d} -> '.format(input) + str(found) + (f' ; ERROR: expected {out}' if found != out else ''))
Raku
my $b = 18;
my @offset = 16, 10, 10, (2×$b)+1, (-2×$b)-15, (2×$b)+1, (-2×$b)-15;
my @span = flat ^8 Zxx <1 3 8 44 15 17 15 15>;
for <1 2 29 42 57 58 72 89 90 103> -> $n {
printf "%3d: %2d, %2d\n", $n, map {$_+1}, ($n-1 + [+] @offset.head(@span[$n-1])).polymod($b).reverse;
}
- Output:
1: 1, 1 2: 1, 18 29: 4, 11 42: 5, 6 57: 8, 4 58: 8, 5 72: 6, 4 89: 9, 4 90: 9, 5 103: 9, 18
Scheme
The following is a minimal recursive implementation. It calculates the position of the requested element by the position of the previous element.
(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))
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.
(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))))))
- Output:
1 -> 1 1 2 -> 1 18 29 -> 4 11 42 -> 5 6 57 -> 8 4 58 -> 8 5 72 -> 6 4 89 -> 9 4
V (Vlang)
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
}
- Output:
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
Wren
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.
import "./fmt" for Fmt
var limits = [3..10, 11..18, 19..36, 37..54, 55..86, 87..118]
var periodicTable = Fn.new { |n|
if (n < 1 || n > 118) Fiber.abort("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]
var row
var start
var end
for (i in 0...limits.count) {
var limit = limits[i]
if (n >= limit.from && n <= limit.to) {
row = i + 2
start = limit.from
end = limit.to
break
}
}
if (n < start + 2 || row == 4 || row == 5) return [row, n - start + 1]
return [row, n - end + 18]
}
for (n in [1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113]) {
var rc = periodicTable.call(n)
Fmt.print("Atomic number $3d -> $d, $-2d", n, rc[0], rc[1])
}
- Output:
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
XPL0
proc ShowPosn(N); \Show row and column for element
int N, M, A, B, I, R, C;
[A:= [ 1, 2, 5, 13, 57, 72, 89, 104]; \magic numbers
B:= [-1, 15, 25, 35, 72, 21, 58, 7];
I:= 7;
while A(I) > N do I:= I-1;
M:= N + B(I);
R:= M/18 +1;
C:= rem(0) +1;
IntOut(0, N); Text(0, " -> ");
IntOut(0, R); Text(0, ", ");
IntOut(0, C); CrLf(0);
];
int Element, I;
[Element:= [1, 2, 29, 42, 57, 58, 72, 89, 90, 103];
for I:= 0 to 10-1 do ShowPosn(Element(I));
]
- Output:
1 -> 1, 1 2 -> 1, 18 29 -> 4, 11 42 -> 5, 6 57 -> 8, 4 58 -> 8, 5 72 -> 6, 4 89 -> 9, 4 90 -> 9, 5 103 -> 9, 18
- Programming Tasks
- Solutions by Programming Task
- 11l
- 6502 Assembly
- 68000 Assembly
- ALGOL 68
- BASIC
- Applesoft BASIC
- ASIC
- BASIC256
- Chipmunk Basic
- Craft Basic
- FreeBASIC
- FTCBASIC
- Gambas
- GW-BASIC
- Minimal BASIC
- Nascom BASIC
- QBasic
- Run BASIC
- True BASIC
- UBasic/4tH
- XBasic
- Yabasic
- C
- Gadget
- FutureBasic
- Go
- J
- Jq
- Julia
- Mathematica
- Wolfram Language
- Nim
- Perl
- Phix
- PHP
- Python
- Raku
- Scheme
- V (Vlang)
- Wren
- Wren-fmt
- XPL0