Luhn’s checksum algorithm
Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 then the number is valid!
Take for example the following American Express number, 378734493671000. Starting from the second-to-last-digit, multiply the last number by 2.
7*2 + 7*2 + 4*2 + 9*2 + 6*2 + 1*2 + 0*2
14 + 14 + 8 + 18 +12 + 2 + 0
Adding the product digits:
1 + 4 + 1 + 4 + 8 + 1 + 8 +1 + 2 + 2 + 0 = 32
Finally add the digits that were not multiplied to the sum
32 + 3 + 8 + 3 + 4 + 3 + 7 + 0 + 0 = 60
The checksum 60 ends with the number 0, therefore it a syntactically sound credit card number
Identifying Credit Card Types
Aside from the checksum, credit card number numbers also identify the type of credit card company. Visa cards start with the number 4. MasterCards start with the number 51, 52, 53, 54, or 55. American Express starts with the number 34 or 37.
The Big Picture
The solution can be broken down into two parts:
- Check if the card number is valid.
- Identify the type of credit card.
Let’s take a look at the syntax for C and walkthrough the code.
Card Length Validation
To check if the card number is valid, there is a preliminary check that we can check for before calculating the checksum. We know that a credit card number can only be either be 13, 15, or 16. We can do that with a simple while or for loop.
The user’s card number is stored in the variable card number and on each iteration of the length of the number the last digit will be removed and counted. The count of the length of the number will then be checked with if it is either 13, 15, or 16 digits.
After passing the first test, the next step would be to see if the checksum is valid. Again we, will take a look at the syntax in C.
In this example, the array number is declared and the card numbers are enumerated through and each digit is saved in the array number. The digits that are stored in the array using this method is reversed from the original card number because of the operation of removing the last digit first and storing it in the first index.
Take for example the credit card number is 4012 8888 8888 1881. Using the modulo and divide by 10 method to store the array the resulting array would be [1,8,8,1,8,8,8,8,8,8,8,8,2,1,0,4].
The checksum for C was cleaner to implement for this reason that there were less type conversions needed to manipulate the data. The array used simple for loops and conditional statements to validate the number.
In the C code above, a new array was created to clone the number array and starting from the second-to-last value the value was multiplied by 2.
The bulk of the validation occurs in this nested if statement. First the length of the card number is determined. Then the arrays digits are added up and checked if the checksum is valid in this one line.
sumdigit = (number[i] % 10) + (number[i]/10 % 10);
The type of card is validated by checking the first and second index of the array. In this example, Visa cards start with the number 4.
cardarray == 4 && accumulator % 10 == 0
The array that has been multiplied by 2 is then summed and added into the array that has not being multiplied using the reduce method. If the checksum passes, then the first two digits of the card array is sliced to check what type of card. The implementation of the card type check is similar to the C syntax. By using a conditional statement the digits of the card array is then evaluated for each type.
This post is more of a self reflection on the differences in programming in a lower level language and a higher level language. In attempting to create a credit card checker, I have found that the lower level language syntax to be more concise in getting to the solution, whereas using the higher level language requires data type conversions to use the higher order functions.
If you want to create a credit card checker and test your own code. I used Paypal’s dummy credit card numbers to test the checksum and the credit card type. If you want to check it out here’s the link. https://developer.paypal.com/docs/classic/payflow/payflow-pro/payflow-pro-testing/#credit-card-numbers-for-testing
I’m still learning more about code every day. I would love to hear from you, if you have any tips or suggestions.