Hash function in java [closed]

-1

Could anyone show me (and explain) a hash function (scatter function) other than the one of division? (key% size)? I wish it was a simple (and there is). In case I have a table of 11 positions that should calclar from the number of the cpf its index, but without using cpf% size_table.

    
asked by anonymous 12.06.2017 / 03:03

1 answer

0

You could interpret the 11 digits as a BigInteger , multiply by a very large prime number (ideally, greater than a CPF number, ie 12 digits), such as 112287187009. Thus:

private static final BigInteger PRIME = new BigInteger("112287187009");
private static final BigInteger ONZE = BigInteger.valueOf(11);

public int hashCpf(String cpf) {
    BigInteger v = new BigInteger(cpf.replace(".", "").replace("-", ""));
    return BigInteger.multiply(PRIME).remainder(11).intValue();
}
    
13.06.2017 / 18:06