Credit cards carry a 16 digit number. These numbers are not allocated sequentially, which means that only certain numbers are valid. This exercise involved coding the algorithm used to validate these numbers.
Details of exactly how credit card numbers work are widely available. The first fifteen digits form the actual card number (some of these digits also represent the issuing bank), and the sixteenth digit is a checksum calculated from these. This means that it is possible to test a sequence of sixteen digits to determine if it could represent a valid card number.
The algorithm used to generate the card number’s checksum is called the Luhn Algorithm, details of which are also widely available.
A file exists that contains many randomly generated possible credit card numbers. They are formatted in the usual way, as four sets of four digits separated by spaces, and there is one on each line of the file. The task is to write a program that will sift this file and print out the numbers in it that could possibly be valid card numbers. The program could also report the percentage of the numbers that are possibly valid.
It is probably best to ignore the extra complexity of the digits that represent the issuing bank, but lists of these are available on-line for enthusiasts.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.