diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-09 06:04:01 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-09 06:04:01 +0000 |
| commit | 29b9eeeb95555dbcf1f375c89910c83ac83abd8d (patch) | |
| tree | 857c2d4c063b88bedcebd76079cf488df977b330 /challenge-003/abigail/lua | |
| parent | d933e4e040eae5d2d4d69b6b4da2d312cd4887e4 (diff) | |
| parent | f2e062cd585f30ecbcf0257e72ccb8f0c82136a7 (diff) | |
| download | perlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.tar.gz perlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.tar.bz2 perlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.zip | |
Merge remote-tracking branch 'upstream/master'
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 -- -- |
