Skip to content

Instantly share code, notes, and snippets.

@sowawa
Last active December 17, 2015 12:29
Show Gist options
  • Save sowawa/5610150 to your computer and use it in GitHub Desktop.
Save sowawa/5610150 to your computer and use it in GitHub Desktop.
luhn check sum(ISO 7812)
"4242424242424242".reverse.chars.each_with_index.map{|ch, index| index.even? ? ch.to_i : (ch.to_i * 2) % 10 + (ch.to_i * 2) / 10 }.inject(:+) % 10 == 0
@sowawa
Copy link
Author

sowawa commented May 20, 2013

luhn check

usage

def luhn number
  number.reverse.chars.each_with_index.map{|ch, index| index.even? ? ch.to_i : (ch.to_i * 2) % 10 + (ch.to_i * 2) / 10 }.inject(:+) % 10 == 0
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment