local fmt = require "fmt" local limits = { range(3, 10), range(11, 18), range(19, 36), range(37, 54), range(55, 86), range(87, 118) } local function periodic_table(n) if n < 1 or n > 118 then error("Atomic number is out of range.") end if n == 1 then return {1, 1} end if n == 2 then return {1, 18} end if 57 <= n <= 71 then return {8, n - 53} end if 89 <= n <= 103 then return {9, n - 85} end local row local start local finish for i = 1, #limits do local limit = limits[i] if limit[1] <= n <= limit:back() then row = i + 1 start = limit[1] finish = limit:back() break end end if n < start + 2 or row == 4 or row == 5 then return {row, n - start + 1} end return {row, n - finish + 18} end for {1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113} as n do local rc = periodic_table(n) fmt.print("Atomic number %3d -> %d, %-2d", n, rc[1], rc[2]) end