The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, Canadian Social Insurance Numbers. The LUHN formula was created in the late 1960s by a group of mathematicians. Shortly thereafter, credit card companies adopted it. Because the algorithm is in the public domain, it can be used by anyone. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers. It was designed to protect against accidental errors, not malicious attacks.
Steps involved in Luhn algorithm
Let’s understand the algorithm with an example:
Consider the example of an account number “79927398713“.
Step 1 – Starting from the rightmost digit double the value of every second digit,
Step 2 – If doubling of a number results in a two digits number i.e greater than 9(e.g., 6 × 2 = 12), then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), to get a single digit number.
Step 3 – Now take the sum of all the digits.
Step 4 – 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.
Since the sum is 70 which is a multiple of 10, therefore the account number is possibly valid.
The idea is simple, we traverse from end. For every second digit, we double it before adding. We add two digits of the number obtained after doubling.
This is a valid card
The Luhn algorithm detects any single-digit error, as well as almost all transpositions of adjacent digits.
This article is contributed by Vishal Kumar Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm
- KMP Algorithm for Pattern Searching
- Boyer Moore Algorithm for Pattern Searching
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Online algorithm for checking palindrome in a stream
- Longest Common Prefix using Divide and Conquer Algorithm
- RSA Algorithm in Cryptography
- Burrows - Wheeler Data Transform Algorithm
- Move To Front Data Transform Algorithm
- RSA Algorithm using Multiple Precision Arithmetic Library
- How to solve RSA Algorithm Problems?
- ElGamal Encryption Algorithm
- One Time Password (OTP) algorithm in Cryptography
- Blowfish Algorithm with Examples
- Simplified International Data Encryption Algorithm (IDEA)
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Applications and Limitations of Diffie-Hellman algorithm