Given a number determine whether or not it is valid per the Luhn formula.
The Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.
The task is to check if a given string is valid.
Strings of length 1 or less are not valid. Spaces are allowed in the input, but they should be stripped before checking. All other non-digit characters are disallowed.
4539 1488 0343 6467
The first step of the Luhn algorithm is to double every second digit, starting from the right. We will be doubling
4_3_ 1_8_ 0_4_ 6_6_
If doubling the number results in a number greater than 9 then subtract 9 from the product. The results of our doubling:
8569 2478 0383 3437
Then sum all of the digits:
8+5+6+9+2+4+7+8+0+3+8+3+3+4+3+7 = 80
If the sum is evenly divisible by 10, then the number is valid. This number is valid!
8273 1232 7352 0569
Double the second digits, starting from the right
7253 2262 5312 0539
Sum the digits
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
57 is not evenly divisible by 10, so this number is not valid.
To run the tests, run the command busted
from within the exercise directory.
For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.
The Luhn Algorithm on Wikipedia http://en.wikipedia.org/wiki/Luhn_algorithm
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
local luhn = require('luhn')
describe('luhn', function()
it('should indicate that single digits are invalid', function()
assert.equal(false, luhn.valid('1'))
assert.equal(false, luhn.valid('0'))
end)
it('should check a valid Canadian SIN', function()
assert.equal(true, luhn.valid('046 454 286'))
end)
it('should check an invalid Canadian SIN', function()
assert.equal(false, luhn.valid('046 454 287'))
end)
it('should check an invalid credit card', function()
assert.equal(false, luhn.valid('8273 1232 7352 0569'))
end)
it('should not allow non-digits', function()
assert.equal(false, luhn.valid('a46 454 286'))
end)
end)
local Luhn = {}
Luhn.__index = Luhn
local function new(input)
local o = {_input = input }
return setmetatable(o, Luhn)
end
local function create(base)
local new_base = base..'0'
local check_sum = new(new_base):checksum()
local last_digit = (10 - check_sum % 10)
return base..(last_digit % 10)
end
function Luhn:valid()
local check_sum = self:checksum()
return (check_sum % 10 == 0)
end
function Luhn:checksum()
local addends = self:addends()
local sum = 0
for i = 1, #addends do
sum = sum + addends[i]
end
return sum
end
function Luhn:addends()
local translated = {}
local even = nil
if #self._input % 2 == 0 then even = true end
for i = 1, #self._input do
local n = tonumber(self._input:sub(i,i))
if even then n = n * 2 even = nil else even = true end
table.insert(translated, n > 9 and n-9 or n)
end
return translated
end
function Luhn:check_digit()
return tonumber(self._input:sub(-1))
end
return {
new = new,
create = create
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
I'm surprised that this is valid syntax: then n = n * 2 even = nil
I would have thought that a semicolon would have been required before even.
@ryanplusplus That's what I thought. but it worked~! :)