Last active
August 22, 2016 01:27
-
-
Save O-I/14e0b8e20310da498ecea22d74097556 to your computer and use it in GitHub Desktop.
Damm Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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