Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cálculo del dígito de control del código de municipio INE
public class DigitoControl {
private static final int[][] magic = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 3, 8, 2, 7, 4, 1, 5, 9, 6},
{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
};
public static int calc(int test) {
byte[] bytes = String.format("%05d", test).getBytes();
int sum = 0, i = 0;
for(Byte v : bytes){
sum+=magic[2 - i % 3][v - 48];
i++;
}
return sum == 0?0:10 - sum % 10;
}
public static void main(String[] args) {
System.out.println(calc(1001));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment