Luhn check digits

Does anyone know an efficient implementation of an algorith to calculate the Luhn check digit of a number? Or know of a library that provides this?

Here's a definition:

Luhn Formula:

1) Double the value of alternate digits beginning with the first right-hand digit (i.e. low order).

2) Add the individual digits comprising the products obtained in step 1 to each unaffected digit in the original number.

3) Subtract the total obtained in step 2 from the next higher multiple of 10. If the total obtained in step 2 is a number ending in zero, the check digit is zero.

Example: The Luhn check digit for 4992739871 is 6, calculated as follows:

1) 418 94 76 916 72

2) 4 + 1 + 8 + 9 + 4 + 7 + 6 + 9 + 1 + 6 + 7 + 2 = 64

3) 70 - 64 = 6

others:

check digit of 111122223333444 is 4

check digit of 1111000000000 is 4

check digit of 1234567890 is 3

check digit of 49927398715 is 0

thanks :)

[1025 byte] By [Parmenion0a] at [2007-9-29 17:04:17]
# 1
This has been asked recently in one of the forums! Do a serach for Luhn!
sabre150a at 2007-7-15 15:42:02 > top of Java-index,Other Topics,Algorithms...
# 2
Well, I searched this forum. I'll check out some others.
Parmenion0a at 2007-7-15 15:42:02 > top of Java-index,Other Topics,Algorithms...
# 3
http://www.webopedia.com/TERM/L/Luhn_formula.htmlThe above link says to start with the second to last digit.
rkippena at 2007-7-15 15:42:02 > top of Java-index,Other Topics,Algorithms...
# 4
If the rightmost digit of the input is supposed to be the check digit, then you ignore it and start with its neighbour to the left. If you don't have the check digit and want to calculate it, you start with the rightmost digit.
DrClapa at 2007-7-15 15:42:02 > top of Java-index,Other Topics,Algorithms...