Periodic table: Difference between revisions

New post.
(New post.)
Line 1,505:
9 4</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
 
public final class PeriodicTable {
 
public static void main(String[] aArgs) {
for ( int atomicNumber : List.of( 1, 2, 29, 42, 57, 58, 59, 71, 72, 89, 90, 103, 113 ) ) {
Position position = periodicTable(atomicNumber);
System.out.println(String.format("%s%-3d%s%d%s%d",
"Atomic number ", atomicNumber, " -> ", position.period, ", ", position.group));
}
}
private static Position periodicTable(int aAtomicNumber) {
if ( aAtomicNumber < 1 || aAtomicNumber > 118 ) {
throw new IllegalArgumentException("Atomic number is out of range:" + aAtomicNumber);
}
if ( aAtomicNumber == 1 ) { // Hydrogen
return new Position(1, 1);
}
if ( aAtomicNumber == 2 ) { // Helium
return new Position(1, 18);
}
if ( aAtomicNumber >= 57 && aAtomicNumber <= 71 ) { // Lanthanides
return new Position(8, aAtomicNumber - 53);
}
if ( aAtomicNumber >= 89 && aAtomicNumber <= 103 ) { // Actinides
return new Position(9, aAtomicNumber - 85);
}
int period = 0;
int periodFirst = 0;
int periodLast = 0;
for ( int i = 0; i < GROUPS.size(); i++ ) {
Group group = GROUPS.get(i);
if ( aAtomicNumber >= group.first && aAtomicNumber <= group.last ) {
period = i + 2;
periodFirst = group.first;
periodLast = group.last;
break;
}
}
if ( aAtomicNumber < periodFirst + 2 || period == 4 || period == 5 ) {
return new Position(period, aAtomicNumber - periodFirst + 1);
}
return new Position(period, aAtomicNumber - periodLast + 18);
}
private static record Group(int first, int last) {}
private static record Position(int period, int group) {}
private static final List<Group> GROUPS = List.of( new Group(3, 10), new Group(11, 18),
new Group(19, 36), new Group(37, 54), new Group(55, 86), new Group(87, 118) );
 
}
</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|jq}}==
908

edits