It looks like the Luhn algorithm has the two approaches.
The first approach is as follows:
From the rightmost digit, which is the check digit, and moving left, double the value of every second digit. The check digit is not doubled; the first digit doubled is immediately to the left of the check digit. If the result of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then add the digits of the result (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same final result can be found by subtracting 9 from that result (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).
Take the sum of all the digits.
If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.
Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:
The sum of all the digits in the third row is 67+x.
The check digit (x) is obtained by computing the sum of the other digits (third row) then subtracting the units digit from 10 (67 => Units digit 7; 10 − 7 = check digit 3). In algorithm form:
Compute the sum of the non-check digits (67).
Take the units digit (7).
Subtract the units digit from 10.
The result (3) is the check digit. In case the sum of digits ends in 0 then 0 is the check digit.
The second approach is as follows:
(Alternative method)
The check digit (x) is obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10 (in equation form, ((67 × 9) mod 10)). In algorithm form:
Compute the sum of the non-check digits (67).
Multiply by 9 (603).
The units digit (3) is the check digit. Thus, x=3.
It seems that we use the first approach to implement this algorithm.
I don't have the experiment to evaluate which these two approaches are faster.
It looks like the Luhn algorithm has the two approaches.
The first approach is as follows:
Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:
The sum of all the digits in the third row is 67+x.
The check digit (x) is obtained by computing the sum of the other digits (third row) then subtracting the units digit from 10 (67 => Units digit 7; 10 − 7 = check digit 3). In algorithm form:
The second approach is as follows:
(Alternative method) The check digit (x) is obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10 (in equation form, ((67 × 9) mod 10)). In algorithm form:
It seems that we use the first approach to implement this algorithm.
I don't have the experiment to evaluate which these two approaches are faster.
Reference: Luhn_algorithm