aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-27 23:53:21 +0100
committerGitHub <noreply@github.com>2021-07-27 23:53:21 +0100
commitdc97e7590a50e62edfb2ae426fc73ef5e6ac5bd9 (patch)
tree1fe8edaecf85d4f7ffecc0d8f6493b179a229d5e
parent92be61e2ecf68097a1e9fc0ece08cc6400fa90fb (diff)
parent7429f34b47a4479a50b1fc07dc08c6656d605bae (diff)
downloadperlweeklychallenge-club-dc97e7590a50e62edfb2ae426fc73ef5e6ac5bd9.tar.gz
perlweeklychallenge-club-dc97e7590a50e62edfb2ae426fc73ef5e6ac5bd9.tar.bz2
perlweeklychallenge-club-dc97e7590a50e62edfb2ae426fc73ef5e6ac5bd9.zip
Merge pull request #4610 from stuart-little/stuart-little_123_lua
1st commit on 123_lua
-rwxr-xr-xchallenge-123/stuart-little/lua/ch-1.lua25
-rwxr-xr-xchallenge-123/stuart-little/lua/ch-2.lua29
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-123/stuart-little/lua/ch-1.lua b/challenge-123/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..376211dc70
--- /dev/null
+++ b/challenge-123/stuart-little/lua/ch-1.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env lua
+
+-- run <script> <number $n> to return the first $n ugly numbers
+
+local memo = {[1]=1,[2]=1,[3]=1,[5]=1}
+
+function smth5p(n)
+ if memo[n] then return true end
+ for _,prm in ipairs({2,3,5}) do
+ if n % prm == 0 and memo[n/prm] then
+ memo[n]=1
+ return true
+ end
+ end
+ return false
+end
+
+local count,nr=0,0
+while (count < tonumber(arg[1])) do
+ nr=nr+1
+ if smth5p(nr) then
+ count=count+1
+ print(nr)
+ end
+end
diff --git a/challenge-123/stuart-little/lua/ch-2.lua b/challenge-123/stuart-little/lua/ch-2.lua
new file mode 100755
index 0000000000..6fe2c55aad
--- /dev/null
+++ b/challenge-123/stuart-little/lua/ch-2.lua
@@ -0,0 +1,29 @@
+#!/usr/bin/env lua
+
+-- run <script> <x1 y1 x2 y2 ..>
+
+function sqDist(coords)
+ return (coords[3]-coords[1])^2 + (coords[4]-coords[2])^2
+end
+
+function sqDistHash(coords)
+ local t={}
+ for i=1,3 do
+ for j=i+1,4 do
+ local d=sqDist({coords[2*i-1],coords[2*i],coords[2*j-1],coords[2*j]})
+ t[d] = t[d] and t[d]+1 or 1
+ end
+ end
+ return t
+end
+
+function isSq(coords)
+ local records=0
+ for k,v in pairs(sqDistHash(coords)) do
+ if k==0 then return false end
+ if v==2 or v==4 then records=records+1 end
+ end
+ return records==2
+end
+
+print(isSq(table.pack(table.unpack(arg,1,8))) and 1 or 0)