diff --git a/misc/abacaba.lua b/misc/abacaba.lua index b44743b..5887aa0 100644 --- a/misc/abacaba.lua +++ b/misc/abacaba.lua @@ -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 diff --git a/misc/collatz.lua b/misc/collatz.lua index 1f232c0..7028ec2 100644 --- a/misc/collatz.lua +++ b/misc/collatz.lua @@ -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') diff --git a/misc/luhn-lua/luhn.lua b/misc/luhn-lua/luhn.lua index 416150e..c37490f 100644 --- a/misc/luhn-lua/luhn.lua +++ b/misc/luhn-lua/luhn.lua @@ -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 diff --git a/misc/luhn-lua/main.lua b/misc/luhn-lua/main.lua index 4ec4c1f..d53d19e 100644 --- a/misc/luhn-lua/main.lua +++ b/misc/luhn-lua/main.lua @@ -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)))