Skip to content

Instantly share code, notes, and snippets.

@O-I
Last active Aug 22, 2016
Embed
What would you like to do?
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