aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <chirvasitua@gmail.com>2021-07-26 12:58:54 -0400
committerchirvasitua <chirvasitua@gmail.com>2021-07-26 12:58:54 -0400
commit7429f34b47a4479a50b1fc07dc08c6656d605bae (patch)
tree2cd3f06b3c740a40046f3195f1e6375f65d5f03e
parent4231c2f762b397e1cacd2cb7e3c2799254fcc1a4 (diff)
downloadperlweeklychallenge-club-7429f34b47a4479a50b1fc07dc08c6656d605bae.tar.gz
perlweeklychallenge-club-7429f34b47a4479a50b1fc07dc08c6656d605bae.tar.bz2
perlweeklychallenge-club-7429f34b47a4479a50b1fc07dc08c6656d605bae.zip
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)