public class Credit { public static void main(String[] args) { // This reads some number from the terminal. // Don't wor
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
public class Credit { public static void main(String[] args) { // This reads some number from the terminal. // Don't wor
Luhn's Algorithm So what's the secret formula? Well, most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhn's algorithm, you can determine if a credit card number is (syntactically) valid as follows: 1. Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together. 2. Add the sum to the sum of the digits that weren't multiplied by 2. 3. If the total's last digit is 0 (or, put more formally, if the total modulo 10 is congruent to o), the number is valid! Example That's kind of confusing, so let's try an example with David's Visa: 4003600000000014. 1. For the sake of discussion, let's first underline every other digit, starting with the number's second-to-last digit: 4003 6 0 0 0 0 0 0 0 0 0 14 Okay, let's multiply each of the underlined digits by 2:
Okay, let's multiply each of the underlined digits by 2: 1:2, 0:2, 0:2, 0:2, 0:2, 6:2, 0:2, 4.2 That gives us: 2, 0, 0, 0, 0, 12, 0, 8 1 1 Now let's add those products' digits (i.e., not the products themselves) together: 2+0+0+0+0+1+2+0+ 8 = 13 2. Now let's add that sum (13) to the sum of the digits that weren't multiplied by 2 (starting from the end): 13+ 4+ 0 + 0 + 0 + 0 + 0 + 3 + 0 = 20 3. Yup, the last digit in that sum (20) is a O, SO David's card is legit! So, validating credit card numbers isn't hard, but it does get a bit tedious by hand. Let's write a program.