Skip to content

Instantly share code, notes, and snippets.

@eltabo
Created January 14, 2016 07:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eltabo/e9fa5fc1e5dd8c2140b4 to your computer and use it in GitHub Desktop.
Save eltabo/e9fa5fc1e5dd8c2140b4 to your computer and use it in GitHub Desktop.
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