Luhn Algorithm Complete Guide
The mathematical formula behind SA ID validation - also known as the "Mod 10 Algorithm"
What is the Luhn Algorithm?
The Luhn Algorithm (also called Luhn formula or Mod 10 algorithm) is a simple checksum formula used to validate various identification numbers. It was created by IBM scientist Hans Peter Luhn in 1954.
In South Africa, this algorithm is used to calculate and verify the 13th digit (check digit) of every SA ID number. If the check digit doesn't match the Luhn calculation, the ID is invalid.
π Where is Luhn Algorithm Used?
- South African ID Numbers
- Credit Card Numbers (Visa, MasterCard, Amex)
- IMEI Numbers (Mobile phones)
- Canadian Social Insurance Numbers
- Greek Social Security Numbers (AMKA)
How Luhn Algorithm Works (Step by Step)
Start from the RIGHT
Take the number (without the check digit) and start from the rightmost digit.
Double Every Second Digit
Moving left, double every second digit (positions 2, 4, 6, etc. from right).
Handle Two-Digit Numbers
If doubling gives a two-digit number (10-18), add those digits together (or subtract 9).
1Γ2=2 β 2
1Γ2=2 β 2
2Γ2=4 β 4
4Γ2=8 β 8
8Γ2=16 β 1+6=7
Sum All Digits
Add all digits (both doubled and non-doubled) together.
Calculate the Check Digit
The check digit is the number needed to make the total sum divisible by 10.
Next multiple of 10 = 40
Check Digit = 40 - 36 = 4
Complete ID: 8501011234084
π’ Live Luhn Calculator
Enter any 12-digit number to calculate its Luhn check digit, or enter a 13-digit number to validate it.
π Try These Examples
- β Check digit 4
- β Check digit 3
- β Check digit 6
- β Valid SA ID
π Mathematical Formula
Luhn Algorithm Formula:
For a number with digits dβ dβ dβ ... dβ (from right to left):
1. For i = 1 to n:
if (i % 2 == 0):
dα΅’ = dα΅’ Γ 2
if (dα΅’ > 9): dα΅’ = dα΅’ - 9
2. Sum = Ξ£ dα΅’ (for all i)
3. Check Digit = (10 - (Sum % 10)) % 10
Validation: (Sum + Check Digit) % 10 == 0
π‘ Pro Tip: In programming, the Luhn algorithm can be implemented in just 5-10 lines of code in any language!
π» Code Examples
function luhnCheckDigit(id) {
let sum = 0;
for (let i = id.length - 1; i >= 0; i--) {
let digit = parseInt(id[i]);
if ((id.length - 1 - i) % 2 === 1) {
digit = digit * 2;
if (digit > 9) digit = digit - 9;
}
sum += digit;
}
return (10 - (sum % 10)) % 10;
}
def luhn_check_digit(id_num):
total = 0
for i, digit in enumerate(reversed(id_num)):
n = int(digit)
if i % 2 == 1:
n = n * 2
if n > 9:
n = n - 9
total += n
return (10 - (total % 10)) % 10
function luhnCheckDigit($id) {
$sum = 0;
$len = strlen($id);
for ($i = $len - 1; $i >= 0; $i--) {
$digit = (int)$id[$i];
if (($len - 1 - $i) % 2 == 1) {
$digit = $digit * 2;
if ($digit > 9) $digit = $digit - 9;
}
$sum += $digit;
}
return (10 - ($sum % 10)) % 10;
}
public static int luhnCheckDigit(String id) {
int sum = 0;
for (int i = id.length() - 1; i >= 0; i--) {
int digit = id.charAt(i) - '0';
if ((id.length() - 1 - i) % 2 == 1) {
digit = digit * 2;
if (digit > 9) digit = digit - 9;
}
sum += digit;
}
return (10 - (sum % 10)) % 10;
}
πΏπ¦ Luhn Algorithm in SA ID Numbers
In South African ID numbers, the Luhn algorithm validates the 13th digit. Here's the complete breakdown:
SA ID Structure: 8501011234084
- 85 = Year (1985)
- 01 = Month (January)
- 01 = Day (1st)
- 1234 = Gender (Female)
- 0 = Citizen
- 8 = Race (Historical)
- 4 = Luhn Check Digit β
Validation Process:
- Take first 12 digits: 850101123408
- Apply Luhn algorithm
- Calculated check digit = 4
- Matches provided digit β Valid β
Frequently Asked Questions
Why is it called "Mod 10 Algorithm"?
Because the check digit makes the total sum divisible by 10 (modulo 10 equals 0).
Can the Luhn algorithm detect all errors?
No. It catches single-digit errors and most adjacent transpositions, but not all errors. About 98% of common errors are detected.
Is Luhn algorithm secure?
No. It's not a cryptographic hash. It's only for catching typing errors, not for security or encryption.
Who invented the Luhn algorithm?
Hans Peter Luhn, an IBM scientist, created it in 1954. It was patented in 1960.
Where can I test Luhn validation?
Use our SA ID Validator tool, or try the live calculator on this page!