diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-23 19:46:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-23 19:46:16 +0000 |
| commit | a3ef8061e8b28fd26ff9aa4ee3fc9f39ceb55dc1 (patch) | |
| tree | e65b7cbfb63e9b6bbcdb6b173bfec9215e70151d /challenge-148/abigail/lua | |
| parent | 919e4948116f4a0f1c1c2580b856dd221568dcb0 (diff) | |
| parent | 895121c6f016a222efcfe4a99df34b8138fb2055 (diff) | |
| download | perlweeklychallenge-club-a3ef8061e8b28fd26ff9aa4ee3fc9f39ceb55dc1.tar.gz perlweeklychallenge-club-a3ef8061e8b28fd26ff9aa4ee3fc9f39ceb55dc1.tar.bz2 perlweeklychallenge-club-a3ef8061e8b28fd26ff9aa4ee3fc9f39ceb55dc1.zip | |
Merge pull request #5552 from Abigail/abigail/week-148
Abigail/week 148
Diffstat (limited to 'challenge-148/abigail/lua')
| -rw-r--r-- | challenge-148/abigail/lua/ch-1.lua | 21 | ||||
| -rw-r--r-- | challenge-148/abigail/lua/ch-2.lua | 109 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-148/abigail/lua/ch-1.lua b/challenge-148/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..246f1db062 --- /dev/null +++ b/challenge-148/abigail/lua/ch-1.lua @@ -0,0 +1,21 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-148 +-- + +-- +-- Run as: lua ch-1.lua +-- + +for i = 0, 100 do + local t = math . floor (i / 10) + local u = i % 10 + if not (i == 0 or t == 1 or u == 1 or t == 7 or u == 7 + or t == 8 or u == 8 or t == 9 or u == 9 + or t == 2 or u == 3 or u == 5 or i == 100) then + io . write (i, " ") + end +end + +io . write ("\n") diff --git a/challenge-148/abigail/lua/ch-2.lua b/challenge-148/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..a03897d859 --- /dev/null +++ b/challenge-148/abigail/lua/ch-2.lua @@ -0,0 +1,109 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-148 +-- + +-- +-- Run as: lua ch-2.lua +-- + +local COUNT = 5 +local A = 0 +local B = 1 +local C = 2 +local SUM = 3 +local out = {} +for i = 1, COUNT do + out [i] = {} + out [i] [A] = 999999 + out [i] [B] = 999999 + out [i] [C] = 999999 + out [i] [SUM] = out [i] [A] + out [i] [B] + out [i] [C] +end + +local k = 0 +local max_index = 1 + +while 3 * k + 2 <= out [max_index] [SUM] do + local a = 3 * k + 2 + local f1 = k + 1 + local f2 = 8 * k + 5 + + -- + -- Find divisors of f1 + -- + local d1 = {} + local i = 0 + while i * i <= f1 do + if f1 % i == 0 then + d1 [#d1 + 1] = i + if f1 / i ~= i then + d1 [#d1 + 1] = f1 / i + end + end + i = i + 1 + end + + local d2 = {} + i = 0 + while i * i <= f2 do + if f2 % i == 0 then + local s1 = math . floor (math . sqrt (i)) + local s2 = math . floor (math . sqrt (f2 / i)) + if s1 * s1 == i then + d2 [#d2 + 1] = s1 + end + if s2 * s2 == f2 / i and s1 ~= s2 then + d2 [#d2 + 1] = s2 + end + end + i = i + 1 + end + + local d1v + local d2v + + for _, d1v in ipairs (d1) do + for _, d2v in ipairs (d2) do + local b = d1v * d2v + local c = f1 * f1 * f2 / (b * b) + if (a + b + c) < out [max_index] [SUM] then + local i + for i = 1, COUNT do + if out [i] [A] == a and out [i] [B] == b then + goto end_loop + end + end + + out [max_index] [A] = a + out [max_index] [B] = b + out [max_index] [C] = c + out [max_index] [SUM] = a + b + c + + -- + -- Find new max_index + -- + + max_index = 1 + local max_sum = out [max_index] [SUM] + + local l + for l = 2, COUNT do + if max_sum < out [l] [SUM] then + max_index = l + max_sum = out [l] [SUM] + end + end + end + ::end_loop:: + end + end + + k = k + 1 +end + +for i = 1, COUNT do + print (out [i] [A] .. " " .. out [i] [B] .. " " .. out [i] [C]) +end + |
