Credit Card Validation
April 8, 2011
Most credit card numbers, and many other identification numbers including the Canadian Social Insurance Number, can be validated by an algorithm developed by Hans Peter Luhn of IBM, described in U. S. Patent 2950048 in 1954 (software patents are nothing new!), and now in the public domain. The Luhn algorithm will detect almost any single-digit error, almost all transpositions of adjacent digits except 09 and 90, and many other errors.
The Luhn algorithm works from right-to-left, with the right-most digit being the check digit. Alternate digits, starting with the first digit to left of the check digit, are doubled. Then the digit-sums of all the numbers, both undoubled and doubled, are added. The number is valid if the sum is divisible by ten.
For example, the number 49927398716 is valid according to the Luhn algorithm. Starting from the right, the sum is 6 + (2) + 7 + (1 + 6) + 9 + (6) + 7 + (4) + 9 + (1 + 8) + 4 = 70, which is divisible by 10; the digit-sums of the doubled digits have been shown in parentheses.
Your task is to write two functions, one that adds a check digit to a identifying number and one that tests if an identifying number is valid. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
My Haskell solution (see http://bonsaicode.wordpress.com/2011/04/08/programming-praxis-credit-card-validation/ for a version with comments):
[…] today’s Programming Praxis exercise our goal is to write to functions related to the Luhn credit card […]
I came up with two versions of my intermediate Luhn sum function: one that
creates a list and modifies it in-place, and another that uses iterators
instead. My solution is available on github.
Just numbers; just one recursive branch / two vectors.
First I missed that (- 10 (modulo 30 10)) is 10, not 0.
Then Ikarus thought that (modulo -30 10) => 10. Sigh.
Wanted to see if I still remember Linux assembly so I wrote my answer in it: github. It might be possible to make the answer more readable and shorter, but that would take me an additional hour…
Factor Language solution.
( scratchpad ) 2771 luhn? .
f
( scratchpad ) 2771 make-luhn dup . luhn? .
27714
t
( scratchpad ) 49927398716 luhn? .
t
( scratchpad ) 49927398716 make-luhn dup . luhn? .
499273987168
t
[…] or Canadian Social Insurance Numbers are validated you can take a look at this Programming Praxis article . It’s all about a simple, tiny, patented (now public domain) algorithm invented by […]
My python 3.x solution:
nice solution
My try in REXX
My solution in Python 3.2
def addCCDigits(number):
number = int(str(number)[::-1])
numString = str(number)
check = False
mySum = 0
for ch in numString:
num = int(ch)
if (check):
num = num * 2
strNum = str(num)
for c in strNum:
num2 = int(c)
mySum += num2
else:
mySum += num
check = not check
return mySum
def validate(number):
mySum = addCCDigits(number)
if (mySum % 10 == 0):
return True
else:
return False
def addCheckDigit(number):
mySum = addCCDigits(number * 10 + 1) – 1
digit1 = int(mySum / 10)
goal = (digit1 + 1) * 10;
diff = goal – mySum;
mySum = int(mySum / 10)
strNumber = str(number) + str(diff)
number = int(strNumber)
return number
[\code]