How to resolve the algorithm Periodic table step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Periodic table step by step in the Wren programming language

Table of Contents

Problem Statement

Display the row and column in the periodic table of the given atomic number. Let us consider the following periodic table representation. 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Periodic table step by step in the Wren programming language

Source code in the wren programming language

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])
}

  

You may also check:How to resolve the algorithm Repeat a string step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Go programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Vim Script programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Batch File programming language
You may also check:How to resolve the algorithm Humble numbers step by step in the ALGOL 68 programming language