Intro
I have a simple methods that converts the input instances of java.math.BigInteger to checksum. The checksum algorithm sums up all the digits in the input BigInteger. Then, if the checksum \$c\$ consists of only one digit, we are done and return that sole digit as our checksum. Otherwise, we call the checksum algorithm with \$c\$ as its parameter.
Code
io.github.coderodde.util.hash.SimpleBigIntegerChecksum.java:
package io.github.coderodde.util.hash;
import java.math.BigInteger;
/**
* This class provides the facilities for computing simple
* {@link java.math.BigInteger} hash that is a value between 0 and 9, both
* inclusively. The idea is to sum all the digits of an input integer. If it's
* withing range 0 and 9, we are done. Otherwise, we compute the hash
* recursively.
*
* @version 1.0.0 (Jul 1, 2025)
* @since 1.0.0 (Jul 1, 2025)
*/
public final class SimpleBigIntegerChecksum {
private SimpleBigIntegerChecksum() {}
/**
* Computes the checksum for the input {@link java.math.BigInteger}.
*
* @param bi the target big integer.
*
* @return the checksum of {@code bi}.
*/
public static int hash(final BigInteger bi) {
if (bi.compareTo(BigInteger.ZERO) < 0) {
// Omit the leading minus sign:
return hash(bi.negate());
}
final String integerText = bi.toString();
if (integerText.length() == 1) {
// Once the hash is only one character long, we are done:
return Integer.parseInt(integerText);
}
BigInteger nextInteger = BigInteger.ZERO;
// Sum up all the digits in the current big integer:
for (final char ch : integerText.toCharArray()) {
nextInteger = nextInteger.add(BigInteger.valueOf((long)(ch - '0')));
}
// Recur further:
return hash(nextInteger);
}
public static void main(String[] args) {
// Eighteen nines = 90 + 8 * 9 = 90 + 72 = 162, checksum must be 9:
System.out.println(hash(BigInteger.valueOf(999999999999999999L)));
}
}
Critique request
So what do you like? Please, tell me anything that comes to mind.