Permalink
Browse files

took off half a second

  • Loading branch information...
1 parent 02e72c5 commit e89449b72337af96867f6ddac4988d1c84b41516 @thegorgon committed Nov 16, 2011
Showing with 2 additions and 1 deletion.
  1. +2 −1 solutions/ruby/lib.rb
View
@@ -5,6 +5,7 @@ class NumberMask
MASKED_DIGIT = "X"
MIN_LENGTH = 14
MAX_LENGTH = 16
+ DOUBLED_NUMS = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
def initialize(string="")
@input = string
@@ -67,7 +68,7 @@ def valid_length?(numbers)
def valid_luhn?(array)
sum = 0
array.reverse.each_with_index do |num, idx|
- sum += idx.odd?? (num * 2).to_s.split('').inject(0) { |memo, x| memo += x.to_i } : num
+ sum += idx.odd?? DOUBLED_NUMS[num] : num
end
sum.modulo(10) == 0
end

0 comments on commit e89449b

Please sign in to comment.