There exists a very, very simple solution, it’s called the Luhn mod 10 algorithm.

Assuming the number is: 4000 1234 5678 9013

Chop off the last digit (400012345678901), then multiply each second digit, starting from the right, by 2:
(400012345678901) [2, 18, 14, 10, 6, 2, 0, 8], add together each digit in the products you just calculated [2 + 1 + 8 + 1 + 4 + 1 + 0 + 6 + 2 + 0 + 8] (33). Add together the digits you didn’t multiply in the initial step [0 + 0 + 2 + 4 + 6 + 8 + 0](20). Add the two sums (33 + 20) = (53), divide by 10, (5 R3) and the remainder (3) is your check digit.

Easy, huh? ;)

[ Here’s a prerolled PHP script, and some less-useful, less-portable perl code follows:]

sub cc_check { cc = split (//, $_[0]);lookup = (0,2,4,6,8,1,3,5,7,9); for $i (0..14) {$total += ($i%2 ? $cc[$i] : $lookup[$cc[$i]]);} return (($total % 10) == $cc15);
}

Previous Post Next Post