Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Determine a systematic element name
<?php
/**
* Represents a systematic element name
*/
class SystematicElementName {
/**
* Gets the root matching the digit
*
* @param int $digit The digit
* @param mixed $nextDigit To respect elision rules, the next digit (as int) or null if this is the last digit (facultative)
* @return string The root
*/
static public function getDigitRoot ($digit, $nextDigit = false) {
switch ((int)$digit) {
case 0: return "nil"; break;
case 1: return "un"; break;
case 2: return ($nextDigit === null) ? "b" : "bi"; break;
case 3: return ($nextDigit === null) ? "tr" : "tri"; break;
case 4: return "quad"; break;
case 5: return "pent"; break;
case 6: return "hex"; break;
case 7: return "sept"; break;
case 8: return "oct"; break;
case 9: return ($nextDigit === 0) ? "en" : "enn"; break;
default: throw new InvalidArgumentException("getDigitRoot: a digit were expected, got '$digit' instead.");
}
return $digit;
}
/**
* Gets the systematic name of the element having the specified number
*
* @param int $number the number of the element
* @return string the systematic name of the element
*/
static public function getName ($number) {
$name = [];
$digits = str_split($number);
$n = count($digits);
for ($i = 0 ; $i < $n ; $i++) {
$nextDigit = ($i < $n - 1) ? (int)$digits[$i + 1] : null;
$name[] = self::getDigitRoot($digits[$i], $nextDigit);
}
$name[] = 'ium';
return join($name);
}
}
@dereckson

This comment has been minimized.

Copy link
Owner Author

@dereckson dereckson commented Nov 13, 2014

L11 should be @param int $lastDigit mixed (int|null)

@dereckson

This comment has been minimized.

Copy link
Owner Author

@dereckson dereckson commented Nov 14, 2014

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment