diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:52:50 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:52:50 +0800 |
| commit | b82ed7c55b6a912b237e7fa396e433ac33ca79c8 (patch) | |
| tree | af2ad93bf0190b04d9c3775c87ab3dd61abd99dd /challenge-003/abigail/lua | |
| parent | 415b77f3f489715d7888de8794affe9afe8d1ffb (diff) | |
| parent | f0bd80b2369212d923d8c6a537ba5379067afbf9 (diff) | |
| download | perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.tar.gz perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.tar.bz2 perlweeklychallenge-club-b82ed7c55b6a912b237e7fa396e433ac33ca79c8.zip | |
-
Diffstat (limited to 'challenge-003/abigail/lua')
| -rw-r--r-- | challenge-003/abigail/lua/ch-1.lua | 54 | ||||
| -rw-r--r-- | challenge-003/abigail/lua/ch-2.lua | 2 |
2 files changed, 37 insertions, 19 deletions
diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua index 70de9b1923..3f0bf5b9fe 100644 --- a/challenge-003/abigail/lua/ch-1.lua +++ b/challenge-003/abigail/lua/ch-1.lua @@ -1,30 +1,48 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- -- Run as: lua ch-1.lua < input-file -- -for max in io . lines () do - local max = tonumber (max) - local base2 = 1 - -- - -- Lua doesn't have a for (expr; expr; expr) syntax. - -- This is missed here. - -- - while base2 <= max do - local base3 = base2 - while base3 <= max do - local base5 = base3 - while base5 <= max do - print (base5) - base5 = base5 * 5 - end - base3 = base3 * 3 +local ugly = {} +ugly [1] = 1 +local next_2 = 1 +local next_3 = 1 +local next_5 = 1 +local count = 1 + +for n in io . lines () do + n = tonumber (n) + while count < n do + local c2 = 2 * ugly [next_2] + local c3 = 3 * ugly [next_3] + local c5 = 5 * ugly [next_5] + + count = count + 1 + + if c2 <= c3 and c2 <= c5 then + ugly [count] = c2 + end + if c3 <= c2 and c3 <= c5 then + ugly [count] = c3 + end + if c5 <= c2 and c5 <= c3 then + ugly [count] = c5 + end + + if 2 * ugly [next_2] <= ugly [count] then + next_2 = next_2 + 1 + end + if 3 * ugly [next_3] <= ugly [count] then + next_3 = next_3 + 1 + end + if 5 * ugly [next_5] <= ugly [count] then + next_5 = next_5 + 1 end - base2 = base2 * 2 end + print (ugly [n]) end diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua index 0e30b0a9c6..aed77c8e32 100644 --- a/challenge-003/abigail/lua/ch-2.lua +++ b/challenge-003/abigail/lua/ch-2.lua @@ -1,7 +1,7 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- |
