Skip to content

Instantly share code, notes, and snippets.

@O-I
Last active August 22, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save O-I/14e0b8e20310da498ecea22d74097556 to your computer and use it in GitHub Desktop.
Save O-I/14e0b8e20310da498ecea22d74097556 to your computer and use it in GitHub Desktop.
Damm Algorithm
module Damm
# See http://en.wikipedia.org/wiki/Damm_algorithm
TABLE=["0317598642","7092154863","4206871359","1750983426","6123045978",
"3674209581","5869720134","8945362017","9438617205","2581436790"]
def lookup number
number.to_s.each_char.inject(0) do |m,v|
TABLE[m][v.to_i].to_i
end
end
def check number
lookup(number.to_i / 10) == number % 10
end
def generate number
number.to_i * 10 + lookup(number.to_i)
end
extend(Damm)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment