Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Luhn Number Golf

by demerphq (Chancellor)
on Feb 26, 2002 at 15:24 UTC ( [id://147594]=note: print w/replies, xml ) Need Help??


in reply to Luhn Number Golf

Well this was fun.

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://147594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found