Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Luhn Number Golf

by jmcnamara (Monsignor)
on Feb 26, 2002 at 15:27 UTC ( [id://147595]=note: print w/replies, xml ) Need Help??


in reply to Luhn Number Golf


82 chars (updated). Returns 1 for valid, undef otherwise. Your solution seems to be out by one for input that has an odd number of digits.
sub luhn { $r=$t=0; split//,pop; $r.=$_[$_]*(1+($_+$#_)%2)for+0..@_; $r=~s/(.)/$t+=$1/eg; !chop$t }
Update: Here is another non-reverse variation at 72 chars. Still a mile behind chipmunk however.
sub luhn2 { $_=$i=0; split//,pop; $_.=(1+$i++%2)*pop while@_; s/(.)/+$1/g; $_=eval; !chop }

--
John.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found