php - Generating unique numbers that pass Luhn algorithm verification -
i trying generate unique numbers can verified luhn algorithm. aim provide account numbers, speak, members on p2p lending platform. i've been looking luhn algorithm , couple of other check digit algorithms, information on verifying numbers these algorithms i'm looking pointers on generating numbers pass check digit algorithm. i'm using php project.
any pointers or recommendation appreciated.
the luhn algorithm generates additional checksum digit arbitrary number. means can turn number n digits luhn-compatible number n + 1 digits.
the wikipedia article on luhn algorithm explains how generate such check digit: set rightmost digit, check digit, zero. work right left , add digits. alternate between digit , digitsum of 2 times digit. determine check digit whole sum's last digit zero.
if have unique number can make luhn compatible with:
function luhnify($number) { $sum = 0; // luhn checksum w/o last digit $even = true; // start digit $n = $number; // lookup table digitsums of 2*$i $evendig = array(0, 2, 4, 6, 8, 1, 3, 5, 7, 9); while ($n > 0) { $d = $n % 10; $sum += ($even) ? $evendig[$d] : $d; $even = !$even; $n = ($n - $d) / 10; } $sum = 9*$sum % 10; return 10 * $number + $sum; }
this code uses number input. take care not overflow allowable range of numbers, because luhnified number 10 times large original number.
a better solution use string of digits, i'm not familiar php, i've chosen easier way numbers.
Comments
Post a Comment