diff options
Diffstat (limited to 'challenge-003/paulo-custodio/lua')
| -rw-r--r-- | challenge-003/paulo-custodio/lua/ch-1.lua | 36 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/lua/ch-2.lua | 30 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-003/paulo-custodio/lua/ch-1.lua b/challenge-003/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..525469191a --- /dev/null +++ b/challenge-003/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,36 @@ +#!/usr/bin/env lua + +--[[ +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less +or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. +--]] + +-- sequence is a merge of all multiples of 2, 3 and 5 +seq2 = {1} +seq3 = {1} +seq5 = {1} + +function next_hamming() + -- get the smallest of the queue heads + n = math.min(seq2[1], seq3[1], seq5[1]) + + -- shift used multiples + if n == seq2[1] then table.remove(seq2, 1); end + if n == seq3[1] then table.remove(seq3, 1); end + if n == seq5[1] then table.remove(seq5, 1); end + + -- push next multiples + table.insert(seq2, 2*n) + table.insert(seq3, 3*n) + table.insert(seq5, 5*n) + + return n +end + +for i=1,tonumber(arg[1]) do + io.write(next_hamming(), "\n") +end diff --git a/challenge-003/paulo-custodio/lua/ch-2.lua b/challenge-003/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..d8c661014c --- /dev/null +++ b/challenge-003/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/usr/bin/env lua + +--[[ +Challenge 003 + +Challenge #2 +Create a script that generates Pascal Triangle. Accept number of rows from +the command line. The Pascal Triangle should have at least 3 rows. For more +information about Pascal Triangle, check this wikipedia page. +--]] + +function draw_pascal(rows) + data = {1} + for row=1,rows do + -- print current row + for col=1,rows-row do io.write(" "); end + for col=1,row do io.write(data[col], " "); end + io.write("\n") + + -- compute next row + nxt = {1} + for col=1,row-1 do + table.insert(nxt, data[col] + data[col+1]) + end + table.insert(nxt, 1) + data = nxt + end +end + +draw_pascal(tonumber(arg[1])) |
