retab lua files

This commit is contained in:
tocariimaa 2025-01-25 00:00:09 -03:00
parent a22880ca8e
commit d1f006dd54
4 changed files with 36 additions and 36 deletions

View file

@ -1,6 +1,6 @@
if #arg < 1 then
print("iteration argument required")
return
print("iteration argument required")
return
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", }
@ -8,7 +8,7 @@ prev_pttr = ""
curr_pttr = ""
iterations = arg[1] % #alpha
for i = 1, iterations do
curr_pttr = prev_pttr .. alpha[i] .. prev_pttr
prev_pttr = curr_pttr
print(curr_pttr)
curr_pttr = prev_pttr .. alpha[i] .. prev_pttr
prev_pttr = curr_pttr
print(curr_pttr)
end

View file

@ -1,14 +1,14 @@
function collatz(n)
io.write(n)
while n > 1 do
if (n % 2) == 0 then
n = n // 2
else
n = 3 * n + 1
end
io.write(' ' .. n)
end
io.write('\n')
io.write(n)
while n > 1 do
if (n % 2) == 0 then
n = n // 2
else
n = 3 * n + 1
end
io.write(' ' .. n)
end
io.write('\n')
end
assert(#arg ~= 0, 'expected initial number')

View file

@ -3,38 +3,38 @@ local luhn = {}
-- main part of the algorithm, double each 2 digits, clamp them and return the sum.
function luhn:_sum_digits(num)
assert(num >= 0, "number must be positive")
local sum = 0
local digits_n = 0
assert(num >= 0, "number must be positive")
local sum = 0
local digits_n = 0
repeat -- iterate over digits from right to left
local tmp = num % 10 -- get current unit
-- each 2 digits, double the current digit
if (digits_n % 2) == 0 then tmp = tmp * 2 end
if tmp > 9 then tmp = tmp - 9 end
repeat -- iterate over digits from right to left
local tmp = num % 10 -- get current unit
-- each 2 digits, double the current digit
if (digits_n % 2) == 0 then tmp = tmp * 2 end
if tmp > 9 then tmp = tmp - 9 end
digits_n = digits_n + 1
sum = sum + tmp
-- remove unit from number
num = math.floor(num / 10)
until num == 0
return sum
digits_n = digits_n + 1
sum = sum + tmp
-- remove unit from number
num = math.floor(num / 10)
until num == 0
return sum
end
-- returns the computed check digit
function luhn:compute_check_digit(num)
return (self:_sum_digits(num) * 9) % 10
return (self:_sum_digits(num) * 9) % 10
end
-- returns true if its valid, false otherwise
function luhn:validate_check_digit(num)
local check_digit = num % 10 -- get check digit
return self:compute_check_digit(math.floor(num / 10)) == check_digit
local check_digit = num % 10 -- get check digit
return self:compute_check_digit(math.floor(num / 10)) == check_digit
end
-- returns the original number with the check digit concatenated at the end
function luhn:make_number(num)
return ((num * 10) + self:compute_check_digit(num))
return ((num * 10) + self:compute_check_digit(num))
end
return luhn

View file

@ -2,12 +2,12 @@
luhn = require "luhn"
if #arg ~= 0 then
local a = tonumber(arg[1]) -- 7992739871
print("check digit: " .. luhn:compute_check_digit(a))
local a = tonumber(arg[1]) -- 7992739871
print("check digit: " .. luhn:compute_check_digit(a))
end
local function bool_str(bool)
return bool and "yes" or "no"
return bool and "yes" or "no"
end
print("49927398716: " .. bool_str(luhn:validate_check_digit(49927398716)))