Can you help me identify the name of the algorithm described here that calculates the check digit of a $10-$digit number?
Given a $012\text-345678\text-n$ arrangement of separated digits, where n is the check digit, the following algorithm is used to calculate the check digit:
Ignoring the $n$ digit, for each next digit take the value of the digit and multiply it by the position of the digit from right-to-left, where $n$ is the $1^{st}$ digit in this example, and $0$ is the $10^{th}$ digit.
Calculate the sum of each multiplication from step $1$, and then modulo the result by $11$.
If the result is less than $2$, it should be equal to the check digit ($n$). Otherwise, the sum of $n$ and the calculated result should be equal to $11$.
As an example, the step $1$ results in: $$0\cdot10 + 1\cdot9 + 2\cdot8 + 3\cdot7 + 4\cdot6 + 5\cdot5 + 6\cdot4 + 7\cdot3 + 8\cdot2 = 156$$ (notice that the last digit ($8$) is in the $2^{nd}$ position from right to left)
In step $2$, the sum ($156$) modulo $11$ would be $2$.
In step $3$, the result $2$ is not less than $2$, so the check digit ($n$) plus $2$ should be equal to $11$, which means that $n = 11 - 2 = 9$.
Thus, $012\text-345678\text-9$ would be a valid series of digits with this algorithm.
This algorithm is used for national ID verification in Iran, and I have written an example implementation, but I don't know what the algorithm is called.
/**
* Example Javascript snippet to validate Iran's National ID (Code Melli)
* https://math.stackexchange.com/q/2740295/285467
* David Refoua <[email protected]>
*/
function validateNID( code )
{
var str = code.toString();
// validate the string length and value
var strLen = str.length, strVal = parseInt(str);
if ( strLen < 8 || strLen > 10 || isNaN(strVal) || strVal == 0 )
return false;
// make sure the string is padded with zeros
while ( str.length < 10 ) str = '0' + str;
// invalidate consecutive arrangement of the same digit
if ( str.match(/^(\d)\1+$/g) ) return false;
var checkDigit = parseInt(str.slice(-1)), // rightmost digit
str = str.slice(0, -1); // remove the check digit
for ( var sum = 0, i = str.length; i > 0; i-- )
sum += (i+1) * str.substr( -i, 1 );
// calculate sum modulo 11
var mod = sum % 11;
return ( mod < 2 && mod === checkDigit ) || ( mod >= 2 && mod + checkDigit === 11);
}
I'm not sure whether the same algorithm also used in ISBN-10, or if it's a different but similar one. Originally I thought it was an extension of the Luhn Algorithm, but this doesn't seem to be the case.
Could you help me identify its name?