diff options
| author | Abigail <abigail@abigail.be> | 2021-07-27 01:05:19 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-28 16:09:38 +0200 |
| commit | e604addad68c72201f22c43a611787e06983d7e9 (patch) | |
| tree | 32fa36fbf2316a3f4a059458d4ba3e5b5666aeb0 /challenge-123/abigail/lua | |
| parent | 331e624891c64fa73a8b0523cc514b8b910f6f11 (diff) | |
| download | perlweeklychallenge-club-e604addad68c72201f22c43a611787e06983d7e9.tar.gz perlweeklychallenge-club-e604addad68c72201f22c43a611787e06983d7e9.tar.bz2 perlweeklychallenge-club-e604addad68c72201f22c43a611787e06983d7e9.zip | |
Solutions in 9 languages for week 123, part 1
Diffstat (limited to 'challenge-123/abigail/lua')
| -rw-r--r-- | challenge-123/abigail/lua/ch-1.lua | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-123/abigail/lua/ch-1.lua b/challenge-123/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..4feb237189 --- /dev/null +++ b/challenge-123/abigail/lua/ch-1.lua @@ -0,0 +1,48 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +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 + end + print (ugly [n]) +end |
