The Math Trick Hidden in Your Credit Card Number
scientificamerican.com
scientificamerican.com
import Data.Char
card = "4024 6072 3695 0748"
cardDigits = map digitToInt . filter (not . isSpace)
doubleDigitSum d = 2 * d - if d<5 then 0 else 9
doubleEveryOther = reverse . zipWith ($) (cycle [doubleDigitSum,id]) . reverse
main = let
cd = cardDigits $ card
sd = sum . doubleEveryOther . init $ cd
checkDigit = (- (sd + last cd)) `mod` 10
in print checkDigitI did credit card processing 20 years ago - AmEx was very often the fly in the ointment.
1. Write out all but the last digit of the card number.
2. Write out all but the last digit of the card number.
3. Starting from the right, double every other number.