From 76a1cabdcb450ae50377df0ce0b4fa5998667647 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Fri, 15 Jul 2022 15:25:22 +0430 Subject: Challenge173 --- challenge-173/deadmarshal/lua/ch-1.lua | 23 +++++++++++++++++++++++ challenge-173/deadmarshal/lua/ch-2.lua | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-173/deadmarshal/lua/ch-1.lua create mode 100644 challenge-173/deadmarshal/lua/ch-2.lua (limited to 'challenge-173/deadmarshal/lua') diff --git a/challenge-173/deadmarshal/lua/ch-1.lua b/challenge-173/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..c25a62fb47 --- /dev/null +++ b/challenge-173/deadmarshal/lua/ch-1.lua @@ -0,0 +1,23 @@ +if #arg ~= 1 then + io.stderr:write('No args provided!') + os.exit(1) +end + +local function is_esthetic_number(n) + assert(type(n) == 'number', 'n must be a number!') + local function uabs(a, b) + if a < b then return b - a else return a - b end + end + if n == 0 then return false end + local i = n % 10 + n = n // 10 + while n > 0 do + local j = n % 10 + if uabs(i, j) ~= 1 then return false end + n = n // 10 + i = j + end + return true +end + +print(is_esthetic_number(tonumber(arg[1]))) diff --git a/challenge-173/deadmarshal/lua/ch-2.lua b/challenge-173/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..1cca518763 --- /dev/null +++ b/challenge-173/deadmarshal/lua/ch-2.lua @@ -0,0 +1,24 @@ +-- https://github.com/edubart/lua-bint +-- To install this library: luarocks install bint +local bint = require 'bint'(1024) + +local function product(t, last) + assert(type(t) == 'table' and + type(last) == 'number', + 't, last must be table, number, number respectively!') + local prod = bint(1) + for i=1, last do prod = prod * t[i] end + return prod +end + +local function sylvesters_sequence() + local t = {2,3} + while(#t ~= 10) do + t[#t+1] = bint(product(t, #t) + 1) + end + return t +end + +local t = sylvesters_sequence() +for i=1, #t do print(t[i]) end + -- cgit