Daniel Martin <martin / snowplow.org> writes:

> I was trying to go for "most compact and obfuscated version of the
> Luhn algorithm", but unfortunately the complications of the algorithm
> meant that I had to debug my implementation, which left it slightly
> more readable.  Oh well.  It's still pretty dense and while my type
> method might be readable it's as dense as I care to make it.

You know, it's longer, but I think I like this definition of the luhn
algorithm better, in terms of its conceptual conciseness:

def luhn(s)
  s.scan(/\d/).map{|x|x.to_i}.inject([0,0]){
  |(a,b),c|[b+c%9,a+2*c%9]}[0]%10 == s.scan(/9/).size%10
end

(Now - the challenge is to figure out why that works)

An interesting observation based on this representation is that the
Luhn algorithm will fail to catch the transposition of an adjacent "0"
and "9":

both '446-667-6097' and '446-667-6907' pass.

-- 
s=%q(  Daniel Martin -- martin / snowplow.org
       puts "s=%q(#{s})",s.to_a[1]       )
       puts "s=%q(#{s})",s.to_a[1]