in reply to Luhn Number Golf
Since you werent too specific about the rules I made mine robust. It has no side effects. It runs under strictures and warnings with no complaints and it doesn't care about non digits in the passed string (ie spaces or hyphens between the groups are ignored).
It comes in at 101 characters.
sub demerphq_luhn{ my($r,$x,$d)="".reverse$_[0];$r=~s/(\d)\D*(\d)?/$x+=$1;$2&&($d.=$2*2)/ +ge;$d=~s/(\d)/$x+=$1/ge;$x%10<1 }
Yves / DeMerphq
--
When to use Prototypes?
|
|---|