Damm Algorithm
In error detection, the Damm algorithm is a check digit algorithm that detects all single-digit errors and all adjacent transposition errors. It was presented by H. Michael Damm in 2004. Its essential part is a quasigroup of order 10 (i.e. having a 10×10 Latin square as operation table) with the special feature of being totally anti-symmetric. Damm revealed several methods to create such TA-quasigroups of order 10 and gave some examples in his doctoral dissertation. With this, Damm also disproved an old conjecture that TA-quasigroups of order 10 do not exist.
static char CheckSum(char* number)
{
const char table[] = "0317598642709215486342068713591750983426612304597836742095815869720134894536201794386172052581436790";
char interim = '0';
for (char* p = number; *p != '\0'; ++p)
{
if ((unsigned char)(*p - '0') > 9)
return '-';
interim = table[(*p - '0') + (interim - '0') * 10];
}
return interim;
}
static bool Validate(char* number)
{
return CheckSum(number) == '0';
}
Example
char c = CheckSum("572");
bool isValid = Validate("5724");
Output
c: 4
isValid: true