retab lua files
This commit is contained in:
parent
a22880ca8e
commit
d1f006dd54
4 changed files with 36 additions and 36 deletions
|
@ -1,6 +1,6 @@
|
||||||
if #arg < 1 then
|
if #arg < 1 then
|
||||||
print("iteration argument required")
|
print("iteration argument required")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "q", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", }
|
alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "q", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", }
|
||||||
|
@ -8,7 +8,7 @@ prev_pttr = ""
|
||||||
curr_pttr = ""
|
curr_pttr = ""
|
||||||
iterations = arg[1] % #alpha
|
iterations = arg[1] % #alpha
|
||||||
for i = 1, iterations do
|
for i = 1, iterations do
|
||||||
curr_pttr = prev_pttr .. alpha[i] .. prev_pttr
|
curr_pttr = prev_pttr .. alpha[i] .. prev_pttr
|
||||||
prev_pttr = curr_pttr
|
prev_pttr = curr_pttr
|
||||||
print(curr_pttr)
|
print(curr_pttr)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
function collatz(n)
|
function collatz(n)
|
||||||
io.write(n)
|
io.write(n)
|
||||||
while n > 1 do
|
while n > 1 do
|
||||||
if (n % 2) == 0 then
|
if (n % 2) == 0 then
|
||||||
n = n // 2
|
n = n // 2
|
||||||
else
|
else
|
||||||
n = 3 * n + 1
|
n = 3 * n + 1
|
||||||
end
|
end
|
||||||
io.write(' ' .. n)
|
io.write(' ' .. n)
|
||||||
end
|
end
|
||||||
io.write('\n')
|
io.write('\n')
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(#arg ~= 0, 'expected initial number')
|
assert(#arg ~= 0, 'expected initial number')
|
||||||
|
|
|
@ -3,38 +3,38 @@ local luhn = {}
|
||||||
|
|
||||||
-- main part of the algorithm, double each 2 digits, clamp them and return the sum.
|
-- main part of the algorithm, double each 2 digits, clamp them and return the sum.
|
||||||
function luhn:_sum_digits(num)
|
function luhn:_sum_digits(num)
|
||||||
assert(num >= 0, "number must be positive")
|
assert(num >= 0, "number must be positive")
|
||||||
local sum = 0
|
local sum = 0
|
||||||
local digits_n = 0
|
local digits_n = 0
|
||||||
|
|
||||||
repeat -- iterate over digits from right to left
|
repeat -- iterate over digits from right to left
|
||||||
local tmp = num % 10 -- get current unit
|
local tmp = num % 10 -- get current unit
|
||||||
-- each 2 digits, double the current digit
|
-- each 2 digits, double the current digit
|
||||||
if (digits_n % 2) == 0 then tmp = tmp * 2 end
|
if (digits_n % 2) == 0 then tmp = tmp * 2 end
|
||||||
if tmp > 9 then tmp = tmp - 9 end
|
if tmp > 9 then tmp = tmp - 9 end
|
||||||
|
|
||||||
digits_n = digits_n + 1
|
digits_n = digits_n + 1
|
||||||
sum = sum + tmp
|
sum = sum + tmp
|
||||||
-- remove unit from number
|
-- remove unit from number
|
||||||
num = math.floor(num / 10)
|
num = math.floor(num / 10)
|
||||||
until num == 0
|
until num == 0
|
||||||
return sum
|
return sum
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns the computed check digit
|
-- returns the computed check digit
|
||||||
function luhn:compute_check_digit(num)
|
function luhn:compute_check_digit(num)
|
||||||
return (self:_sum_digits(num) * 9) % 10
|
return (self:_sum_digits(num) * 9) % 10
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns true if its valid, false otherwise
|
-- returns true if its valid, false otherwise
|
||||||
function luhn:validate_check_digit(num)
|
function luhn:validate_check_digit(num)
|
||||||
local check_digit = num % 10 -- get check digit
|
local check_digit = num % 10 -- get check digit
|
||||||
return self:compute_check_digit(math.floor(num / 10)) == check_digit
|
return self:compute_check_digit(math.floor(num / 10)) == check_digit
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns the original number with the check digit concatenated at the end
|
-- returns the original number with the check digit concatenated at the end
|
||||||
function luhn:make_number(num)
|
function luhn:make_number(num)
|
||||||
return ((num * 10) + self:compute_check_digit(num))
|
return ((num * 10) + self:compute_check_digit(num))
|
||||||
end
|
end
|
||||||
|
|
||||||
return luhn
|
return luhn
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
luhn = require "luhn"
|
luhn = require "luhn"
|
||||||
|
|
||||||
if #arg ~= 0 then
|
if #arg ~= 0 then
|
||||||
local a = tonumber(arg[1]) -- 7992739871
|
local a = tonumber(arg[1]) -- 7992739871
|
||||||
print("check digit: " .. luhn:compute_check_digit(a))
|
print("check digit: " .. luhn:compute_check_digit(a))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function bool_str(bool)
|
local function bool_str(bool)
|
||||||
return bool and "yes" or "no"
|
return bool and "yes" or "no"
|
||||||
end
|
end
|
||||||
|
|
||||||
print("49927398716: " .. bool_str(luhn:validate_check_digit(49927398716)))
|
print("49927398716: " .. bool_str(luhn:validate_check_digit(49927398716)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue