Periodic table: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Minor adjustment to code.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 4 users not shown)
Line 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 1,320 ⟶ 1,362:
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>
 
 
=={{header|FutureBasic}}==
Line 2,017 ⟶ 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 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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