The verification of the credit card number is based on the Luhn algorithm: the old IBM engineer Hans Peter Luhn invented in 1954.
The patent was applied at that time and is now open to the public domain of knowledge as a standard for international standards organizations: ISO/EC 7812-1.
- Starting with the last digit of the card number, the inverse adds the odd digit number to the sum
- Starting with the last digit of the card number, multiply the even digits by 2, and if the product is a two-digit number, subtract 9 (or two-digit sum) and sum
- The sum of odd digits is combined with the sum of even digits, and the result can be divisible by 10.
1 intLuhncheck (vector<int> &digitnums)2 {3 intSZ =digitnums.size ();4 intsum =0;5 for(intI=1; I <= sz; i++)6 {7 if(i%2==0)8 {9 intval = digitnums[sz-i] *2;Ten if(Val >9) One { AVal-=9; - } -Sum + =Val; the } - Else - { -Sum + = digitnums[sz-i]; + } - } +cout<<"Check sum:"<<sum<<Endl; A if(sum%Ten==0) at { - return 1; - } - -}return 0;
View Code
Luhn algorithm--verification of credit card numbers