I just finished implementing the Verhoeff checksum algorithm in PHP, you can download it as verhoeff.zip. Yes, mathematically it’s a bit more elaborate than other checksum algorithms, but with look-up tables it’s fairly simple and lightweight.
The implementation provides two functions calcsum($number) and checksum($number). The first one return a check digit for the decimal number used as argument, just append the returned digit to the number and you got yourself a checkable number. The second function checks the number (including the appended check digit) used as argument, the number is only valid if the function return zero.
I noticed a small bug due to a misplace modulus (simple PHP syntax typo), the verhoeff.php is now updated, tested and in working order. The bug only affected the calculation of check digits, not the check function.
11 comments
Gracias por su Colaboración
IVE BEEN READING WIKIPEDIAS EXPLANATION OF TH VERHOEFF SCHEME. but when it comes to their example am not able to figure out where they get the values for the column titled previous c. please help me on this. send expalnation to my email.
For the benefit of all, I’ve realized that I could post my reply here as well:
You asked about where the previous c value came from. I guess it is easier to follow the example on Wikipedia if you take a look at my PHP implementation while following it along, not that it is any complicated matter. The c start with a value of zero, when the algorithm iterates over the string of digits c is both input to the d(j,k) function (the j value) and the output replacing the previous or default value. Thus, in the Wikipedia example, previous c represent the c value from the previous iteration or the default value on the first iteration.
thanks, still not clear what is the role of the calcsum() function. Also, what do you do when you have numbers with more than 8 digits total (like in a credit card)?
The
calcsum($number)function return the checksum digit for the series of digits used as argument, if you give it142857as argument it will return the checksum digit0. Append the digit to the number so it now reads1428570.The
checksum($number)do the opposite. Give it a number with check digit to check, like1428570, as argument and will return a0if the number is valid (it will always be a zero if valid no matter what the check digit was).The Verhoeff algorithm have no limitation on the number of digits (as many other algorithms have) it can check, it’s unlimited.
The results of the wikipedia article differ from the results of the recipe given in the “Numerical Recipes in C” book. I’ve found a website that agrees with the book (but maybe they used it!). I’ve retrieved Verhoeff’s original paper but I’m not up on group theory - it looks the same upside down to me!
Anyone know for sure that the wikipedia article is good? Are there any good other references around?
Many thanks
I don’t remember what text I used, don’t think it was the Wikipedia article but some textbook or report on check digits. I probably have a couple PDF files with the text that I’ve downloaded somewhere, I will try to find them.
Hmm… wonder what result my implementation match? Scary.
I can’t run PHP. If we take the example 142857, the wikipedia version gives 0 and the Numerical Recipes version gives 6. This site http://www.augustana.ab.ca/~mohrj/algorithms/checkdigit.html agrees with wikipedia, this one http://web.mit.edu/kenta/www/three/verhoeff-checksum.html.gz goes with Numerical Recipes. Num Rec has shot me in the foot before (giving code in a typeface where “one” and “ell” are identical!!), but it’s generally held to be scholarly. I was going to try and surf some pictures of old german banknotes…!
Here is a live version of my script to play around with:
http://labs.dahnielson.com/tmp/verhoeff-test.php?f=calc&n=142857fcan either be calc or checknis the number to calculate a check digit for or checkIf you calculate the check digit for
142857it return0(and if you check1428570it return0). So my code looks like to be on the Wikipedia side of the table.It turns out that my client has made up lots of “units” using the numerical recipes version, so right or wrong they’re going to go with that. I’ve retrieved Verhoeff’s original paper but it’s hard group theory maths -it looks the same upside down to me! I see the wikipedia article discussion cites yet another rendering of the scheme, also held to be incorrect.
Without the input of a skilled mathematician (Verhoeff - are you reading us?) I guess nobody will ever really know! If you find your references I’d be interested to hear about them.
Thanks for your input!
I believe this document “Numeric Department Identifiers” by the Australian Defense Force Academy was my main reference when implementing the Verhoeff algorithm.
Post a comment