From 4a6109910c67852630b4d5b50d2a1bf465cfbeaa Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Sun, 20 Nov 2022 10:46:28 +0330 Subject: Challenge191 --- challenge-191/deadmarshal/lua/ch-2.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-191/deadmarshal/lua/ch-2.lua (limited to 'challenge-191/deadmarshal/lua/ch-2.lua') diff --git a/challenge-191/deadmarshal/lua/ch-2.lua b/challenge-191/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..0c6131362a --- /dev/null +++ b/challenge-191/deadmarshal/lua/ch-2.lua @@ -0,0 +1,28 @@ +local function is_cute(t) + assert(type(t) == 'table', 't must be a table!') + for i=1, #t do + if i % t[i] ~= 0 and t[i] % i ~= 0 then return false end + end + return true +end + +local function permute(t, i, sz, t2) + assert(type(t) == 'table' and + type(i) == 'number' and + type(sz) == 'number' and + type(t2) == 'table', 'params assertions failed!') + if sz == i then + if is_cute(t) then t2.count = t2.count + 1 return end + end + for j=i, sz do + t[i], t[j] = t[j], t[i] + permute(t,i+1,sz,t2) + t[i], t[j] = t[j], t[i] + end + return +end + +local t,t2 = {1,2},{count = 0} +permute(t, 1, 2, t2) +print(string.format("%d",t2.count)) + -- cgit