Verhoeff algorithm
Decimal error detection code
title: "Verhoeff algorithm" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["modular-arithmetic", "checksum-algorithms", "error-detection-and-correction", "1969-introductions"] description: "Decimal error detection code" topic_path: "technology/algorithms" source: "https://en.wikipedia.org/wiki/Verhoeff_algorithm" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Decimal error detection code ::
The Verhoeff algorithm is a checksum for error detection first published by Dutch mathematician Jacobus Verhoeff in 1969. It was the first decimal check digit algorithm that detects all single-digit errors, and all transposition errors involving two adjacent digits, which was at the time thought impossible with such a code.
The method was independently discovered by H. Peter Gumm in 1985, this time including a formal proof and an extension to any base.
Goals
Verhoeff had the goal of finding a decimal code—one where the check digit is a single decimal digit—which detected all single-digit errors and all transpositions of adjacent digits. At the time, supposed proofs of the nonexistence of these codes made base-11 codes popular, for example in the ISBN check digit.
His goals were also practical, and he based the evaluation of different codes on live data from the Dutch postal system, using a weighted points system for different kinds of error. The analysis broke the errors down into a number of categories: first, by how many digits are in error; for those with two digits in error, there are transpositions (ab → ba), twins (aa → bb), jump transpositions (abc → cba), phonetic (1a → a0), and jump twins (aba → cbc). Additionally there are omitted and added digits. Although the frequencies of some of these kinds of errors might be small, some codes might be immune to them in addition to the primary goals of detecting all singles and transpositions.
The phonetic errors in particular showed linguistic effects, because in Dutch, numbers are typically read in pairs; and also while 50 sounds similar to 15 in Dutch, 80 does not sound like 18.
Taking six-digit numbers as an example, Verhoeff reported the following classification of the errors:. ::data[format=table]
| Digits in error | Classification | Count | Frequency |
|---|---|---|---|
| 1 | Transcription | 9,574 | 79.05% |
| 2 | Transpositions | 1,237 | 10.21% |
| Twins | 67 | 0.55% | |
| Phonetic | 59 | 0.49% | |
| Other adjacent | 232 | 1.92% | |
| Jump transpositions | 99 | 0.82% | |
| Jump Twins | 35 | 0.29% | |
| Other jump errors | 43 | 0.36% | |
| Other | 98 | 0.81% | |
| 3 | 169 | 1.40% | |
| 4 | 118 | 0.97% | |
| 5 | 219 | 1.81% | |
| 6 | 162 | 1.34% | |
| Total | 12,112 | ||
| :: |
Description
The general idea of the algorithm is to represent each of the digits (0 through 9) as elements of the dihedral group D5. That is, map digits to D5, manipulate these, then map back into digits. Let this mapping be m : [0, 9] → D5 : m = \begin{pmatrix} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\ e & r & r^2 & r^3 & r^4 & s & rs & r^2s & r^3s & r^4s\end{pmatrix}
Let the nth digit be a**n and let the number of digits be k.
For example given the code 942 then k is 3 and .
Now define the permutation f : D5 → D5 : f = \begin{pmatrix} e & r & r^2 & r^3 & r^4 & s & rs & r^2s & r^3s & r^4s\ r & s & r^2s & rs & r^2 & r^3s & r^3 & e & r^4s & r^4\end{pmatrix}
For example, f(r^3) = rs. Another example is f^2(r^3) = r^3 since f(f(r^3)) = f(rs) = r^3.
Using multiplicative notation for the group operation of D5, the check digit is then simply a value c such that : f^0(c) \cdot f^1(a_k) \cdot \ldots \cdot f^{k-1}(a_2) \cdot f^k(a_1) = e
c is explicitly given by multiplicative inverse: : c = \left(\prod_{n=1}^k f^n(a_{k+1-n})\right)^{-1}
For example the check digit for 942 is 7. To verify this, use the mapping to D5 and insert into the LHS of the previous equation : f^0(r^{2}s) \cdot f^1(r^2) \cdot f^2(r^4) \cdot f^3(r^{4}s) = e
To evaluate this permutation quickly use that : f^3(r^4s) = f^2(r^4) = f^1(r^2) = f^0(r^2 s) = r^2 s to get that : r^2 s \cdot r^2 s \cdot r^2 s \cdot r^2 s = e
This is the same reflection being iteratively multiplied. Use that reflections are their own inverse. : (r^2 s \cdot r^2 s) \cdot (r^2 s \cdot r^2 s) = e^2 = e
In practice, the algorithm is implemented using simple lookup tables without needing to understand how to generate those tables from the underlying group and permutation theory. This is more properly considered a family of algorithms, as other permutations work too. Verhoeff's notes that the particular permutation, given above, is special as it has the property of detecting 95.3% of the phonetic errors.
The strengths of the algorithm are that it detects all transliteration and transposition errors, and additionally most twin, twin jump, jump transposition and phonetic errors.
The main weakness of the Verhoeff algorithm is its complexity. The calculations required cannot easily be expressed as a formula in say Z / 10Z. Lookup tables are required for easy calculation. A similar code is the Damm algorithm, which has similar qualities.
Table-based algorithm
The Verhoeff algorithm can be implemented using three tables: a multiplication table d, an inverse table inv, and a permutation table p.
::data[format=table]
| d | k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | j | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |||||||||||||
| 1 | 2 | 3 | 4 | 0 | 6 | 7 | 8 | 9 | 5 | |||||||||||||
| 2 | 3 | 4 | 0 | 1 | 7 | 8 | 9 | 5 | 6 | |||||||||||||
| 3 | 4 | 0 | 1 | 2 | 8 | 9 | 5 | 6 | 7 | |||||||||||||
| 4 | 0 | 1 | 2 | 3 | 9 | 5 | 6 | 7 | 8 | |||||||||||||
| 5 | 9 | 8 | 7 | 6 | 0 | 4 | 3 | 2 | 1 | |||||||||||||
| 6 | 5 | 9 | 8 | 7 | 1 | 0 | 4 | 3 | 2 | |||||||||||||
| 7 | 6 | 5 | 9 | 8 | 2 | 1 | 0 | 4 | 3 | |||||||||||||
| 8 | 7 | 6 | 5 | 9 | 3 | 2 | 1 | 0 | 4 | |||||||||||||
| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |||||||||||||
| :: |
::data[format=table]
| j | inv(j) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | |||||||||||
| 4 | |||||||||||
| 3 | |||||||||||
| 2 | |||||||||||
| 1 | |||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| :: |
::data[format=table] | p | num | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | va=middle| pos (mod 8) }} | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | | | | | | | | | | | | 1 | 5 | 7 | 6 | 2 | 8 | 3 | 0 | 9 | 4 | | | | | | | | | | | | | 5 | 8 | 0 | 3 | 7 | 9 | 6 | 1 | 4 | 2 | | | | | | | | | | | | | 8 | 9 | 1 | 6 | 0 | 4 | 3 | 5 | 2 | 7 | | | | | | | | | | | | | 9 | 4 | 5 | 3 | 1 | 2 | 6 | 8 | 7 | 0 | | | | | | | | | | | | | 4 | 2 | 8 | 6 | 5 | 7 | 3 | 9 | 0 | 1 | | | | | | | | | | | | | 2 | 7 | 9 | 3 | 8 | 0 | 6 | 4 | 1 | 5 | | | | | | | | | | | | | 7 | 0 | 4 | 6 | 9 | 1 | 3 | 2 | 5 | 8 | | | | | | | | | | | | ::
The first table, d, is based on multiplication in the dihedral group D5. and is simply the Cayley table of the group. Note that this group is not commutative, that is, for some values of j and k, d(j,k) ≠ d(k, j).
The inverse table inv represents the multiplicative inverse of a digit, that is, the value that satisfies .
The permutation table p applies a permutation to each digit based on its position in the number. This is actually a single permutation (1 5 8 9 4 2 7 0)(3 6) applied iteratively; i.e. .
The Verhoeff checksum calculation is performed as follows:
- Create an array n out of the individual digits of the number, taken from right to left (rightmost digit is n0, etc.).
- Initialize the checksum c to zero.
- For each index i of the array n, starting at zero, replace c with d(c, p(i mod 8, n**i)).
The original number is valid if and only if .
To generate a check digit, append a 0, perform the calculation: the correct check digit is inv(c).
Examples
Generate a check digit for 236: ::data[format=table]
| i | ni | p(i, ni) | c |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 6 | 3 | 3 |
| 2 | 3 | 3 | 1 |
| 3 | 2 | 1 | 2 |
| :: |
c is 2, so the check digit is inv(2), which is 3. Validate the check digit in 2363: ::data[format=table]
| i | ni | p(i, ni) | c |
|---|---|---|---|
| 0 | 3 | 3 | 3 |
| 1 | 6 | 3 | 1 |
| 2 | 3 | 3 | 4 |
| 3 | 2 | 1 | 0 |
| :: |
c is zero, so the check is correct.
Uses
The Verhoeff algorithm is used in a variety of systems, including:
- German Deutschemark banknotes
- Indian Aadhaar numbers
- Irish Meter Registration System Operator
- The SNOMED Clinical Terms dictionary
References
References
- (January 1985). "A new class of check-digit methods for arbitrary number systems (Corresp.)". IEEE Transactions on Information Theory.
- {{harvnb. Verhoeff. 1969
- {{harvnb. Verhoeff. 1969
- (2010). "EIMI 2010 Proceedings". Educational Interfaces between Mathematics and Industry.
- "Implementation of Verhoeff Algorithm by banks for Aadhaar related applications". National Payments Corporation of India.
- "Meter Point Reference Number (MPRN)". Meter Registration System Operator, Ireland.
- "Check digit computation". SNOMED International.
- Kirtland, Joseph. (2001). "Identification Numbers and Check Digit Schemes". Mathematical Association of America.
- Salomon, David. (2005). "Coding for Data and Computer Communications". Springer.
- (2006). "The Edge of the Universe: Celebrating Ten Years of Math Horizons". Mathematical Association of America.
- Sisson, Roger L.. (May 1958). "An improved decimal redundancy check". Communications of the ACM.
- Gallian, Joseph A.. (2010). "Contemporary Abstract Algebra". Brooks/Cole.
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::