Last updated on 2016年11月21日
Luhn算法,也被称作“模10算法”。主要用来计算信用卡、银行卡、会员卡、手机IMEI等号码的合法性。
Luhn算法会通过校验码对一串数字进行验证,校验码通常会被加到这串数字的末尾处,从而得到一个完整的身份识别码。
1、数字从右向左开始,偶数位乘以2,如果乘以2的结果是两位数,将两个位上数字相加保存。
2、把所有数字相加,得到总和。
3、如果总和可以被10整除,则号码合法,反之不合法。
英文原文描述:
1.Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
2.Sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5) together with the undoubled digits from the original number.
3.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.
优点和缺点
Luhn 算法会检测到任何单码的错误以及几乎所有的相邻数字换位的错误。但是它不会检测两个数字序列09转90的错误(反之亦然)。它会检测到十分之七的相同双位数错误(不会检测到22和55的互换,33和66的互换,44和77的互换)。其他更复杂的检查数字算法,如费尔赫夫算法,可以检测出更多的转录错