aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/deadmarshal/lua/ch-2.lua
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-21 04:57:01 +0000
committerGitHub <noreply@github.com>2022-11-21 04:57:01 +0000
commit054a1d856f6600be605d149f7830a7426f828fce (patch)
treeee38a9bf6eef686793ab957d964d2c70cb4d3ee7 /challenge-191/deadmarshal/lua/ch-2.lua
parent7ac92a4a7fd76a307feefe75fcb666f9525e1b7b (diff)
parent4a6109910c67852630b4d5b50d2a1bf465cfbeaa (diff)
downloadperlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.gz
perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.bz2
perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.zip
Merge pull request #7110 from deadmarshal/challenge191
Challenge191
Diffstat (limited to 'challenge-191/deadmarshal/lua/ch-2.lua')
-rw-r--r--challenge-191/deadmarshal/lua/ch-2.lua28
1 files changed, 28 insertions, 0 deletions
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))
+