RosettaCodeData/Task/CUSIP/Pluto/cusip.pluto
2026-04-30 12:34:36 -04:00

33 lines
885 B
Text

local function is_cusip(s)
if #s != 9 then return false end
local sum = 0
for i = 1, 8 do
local c = s[i]
local v = switch true do
case c >= '0' and c <= '9' -> c:byte() - 48
case c >= 'A' and c <= 'Z' -> c:byte() - 55
case c == "*" -> 36
case c == "@" -> 37
case c == "#" -> 38
default -> nil
end
if !v then return false end
if i % 2 == 0 then v = v * 2 end -- check if even as using 1-based indexing
sum += v // 10 + v % 10
end
return s[9]:byte() - 48 == (10 - (sum % 10)) % 10
end
local candidates = {
"037833100",
"17275R102",
"38259P508",
"594918104",
"68389X106",
"68389X105"
}
for candidates as candidate do
local b = is_cusip(candidate) ? "correct" : "incorrect"
print($"{candidate} -> {b}")
end