Jump to content

Periodic table: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 66:
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?
 
<langsyntaxhighlight lang="6502asm">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)
Line 76:
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,...</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
Line 84:
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.)
 
<langsyntaxhighlight lang="68000devpac">Lookup:
;input: D0.W = the atomic number of interest.
LEA PeriodicTable,A0
Line 103:
DC.W $0217 ;FLUORINE
DC.W $0218 ;NEON
;etc.</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # display the period and group number of an element, #
# given its atomic number #
INT max atomic number = 118; # highest known element #
Line 166:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 188:
==={{header|Applesoft BASIC}}===
This program borrows from the [[#Python|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.
<langsyntaxhighlight lang="gwbasic">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
Line 195:
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</langsyntaxhighlight>
 
=== {{header|ASIC}} ===
{{trans|Nascom BASIC}}
<langsyntaxhighlight lang="basic">
REM Periodic table
DIM A(7)
Line 240:
PRINT C
RETURN
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 257:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">subroutine MostarPos(N)
dim A = { 1, 2, 5, 13, 57, 72, 89, 104}
dim B = {-1, 15, 25, 35, 72, 21, 58, 7}
Line 273:
for I = 0 to Element[?]-1
call MostarPos(Element[I])
next I</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 279:
==={{header|FreeBASIC}}===
{{trans|XPL0}}
<langsyntaxhighlight lang="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
Line 296:
For I As Integer = 0 To Ubound(Element)
MostarPos(Element(I))
Next I</langsyntaxhighlight>
{{out}}
<pre>Atomic number 1 -> 1, 1
Line 314:
==={{header|FutureBasic}}===
Old fashioned way:
<langsyntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 365:
 
HandleEvents
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 385:
 
Modern way. (Too bad you can no longer upload images to Rosetta Code.)
<langsyntaxhighlight lang="futurebasic">
_window = 1
 
Line 455:
 
HandleEvents
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 462:
 
==={{header|Gambas}}===
<langsyntaxhighlight lang="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
Line 483:
Next
 
End</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 490:
{{trans|Nascom BASIC}}
{{works with|Commodore BASIC|3.5}}
<langsyntaxhighlight lang="gwbasic">
10 REM Periodic table
20 GOSUB 200
Line 523:
1030 REM Example elements (atomic numbers).
1040 DATA 1, 2, 29, 42, 57, 58, 72, 89, 90, 103
</syntaxhighlight>
</lang>
 
=== {{header|Nascom BASIC}} ===
{{works with|Nascom ROM BASIC|4.7}}
<langsyntaxhighlight lang="basic">
10 REM Periodic table
20 GOSUB 200
Line 551:
1030 REM ** Example elements (atomic numbers).
1040 DATA 1,2,29,42,57,58,72,89,90,103
</langsyntaxhighlight>
{{out}}
<pre>
Line 570:
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">SUB MostarPos (N)
DIM a(7)
RESTORE a:
Line 601:
DATA 1, 2, 5, 13, 57, 72, 89, 104
b:
DATA -1, 15, 25, 35, 72, 21, 58, 7</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 609:
{{works with|Liberty BASIC}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="lb">dim Element(12)
Element(0) = 1
Element(1) = 2
Line 655:
C = (M mod 18) +1
print "Atomic number "; using("###", N); " -> "; R; ", "; C
end sub</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 661:
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">SUB MostarPos (n)
DIM a(0 TO 7)
LET a(0) = 1
Line 708:
CALL MostarPos (element(e))
NEXT e
END</langsyntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
Line 715:
{{works with|Windows XBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="xbasic">PROGRAM "Periodic table"
 
DECLARE FUNCTION Entry ()
Line 771:
 
END FUNCTION
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
Line 777:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">// Rosetta Code problem: http://rosettacode.org/wiki/Periodic_table
// by Jjuanhdez, 06/2022
 
Line 813:
C = mod(M, 18) +1
print "Atomic number ", N using("###"), " -> ", R, ", ", C
end sub</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 819:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="ecmascript">package main
 
import (
Line 865:
fmt.Printf("Atomic number %3d -> %d, %-2d\n", n, row, col)
}
}</langsyntaxhighlight>
 
{{out}}
Line 888:
Basically, here, we want a lookup table. For example:
 
<langsyntaxhighlight Jlang="j">PT=: (' ',.~[;._2) {{)n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 H He
Line 911:
}}
 
rowcol=: ptrc''</langsyntaxhighlight>
 
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).)
Line 917:
Thus:
 
<langsyntaxhighlight Jlang="j"> 1 2 29 42 57 58 72 89 { rowcol
1 1
1 18
Line 925:
8 5
6 4
9 4</langsyntaxhighlight>
 
 
=={{header|Julia}}==
{{trans|Wren}}
<langsyntaxhighlight lang="ruby">const limits = [3:10, 11:18, 19:36, 37:54, 55:86, 87:118]
 
function periodic_table(n)
Line 953:
println("Atomic number ", lpad(n, 3), " -> ($(rc[1]), $(rc[2]))")
end
</langsyntaxhighlight>{{out}}
<pre>
Atomic number 1 -> (1, 1)
Line 972:
=={{header|Mathematica}}/{{header|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.
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FindPeriodGroup]
FindPeriodGroup[n_Integer] := Which[57 <= n <= 70,
{8, n - 53}
Line 986:
]
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]]</langsyntaxhighlight>
{{out}}
<pre>Element 1 -> {1,1}
Line 1,006:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings; no warnings 'uninitialized';
use feature 'say';
Line 1,020:
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;
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1, 1
Line 1,034:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">match_wp</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
Line 1,067:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"|"</span><span style="color: #0000FF;">}}),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
With match_wp set to true:
Line 1,096:
</pre>
===alternate===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">ptxt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
__________________________________________________________________________
Line 1,149:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Element %d %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">e</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,171:
A solution trying hard not to encode too much data about the table.
 
<syntaxhighlight lang="python">
<lang Python>
def perta(atomic) -> (int, int):
 
Line 1,219:
print('TEST:{:3d} -> '.format(input) + str(found) + (f' ; ERROR: expected {out}' if found != out else ''))
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>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>;
Line 1,228:
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;
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1, 1
Line 1,244:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var limits = [3..10, 11..18, 19..36, 37..54, 55..86, 87..118]
Line 1,273:
var rc = periodicTable.call(n)
Fmt.print("Atomic number $3d -> $d, $-2d", n, rc[0], rc[1])
}</langsyntaxhighlight>
 
{{out}}
Line 1,293:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="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
Line 1,310:
[Element:= [1, 2, 29, 42, 57, 58, 72, 89, 90, 103];
for I:= 0 to 10-1 do ShowPosn(Element(I));
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.