Fun With Tables

lua-users home
wiki

A small (only 13 lines!) program that tells whether a number from 1 to 100 is divisible by 3, 5, neither, or both. Using Lua's tables, it's possible to create something like a Ruby range and iterate over everything, accomplishing the task in just 13 lines, a testament to Lua's expressiveness.

n, m = {}, {}; for x=1,100 do n[x] = x end
for i,v in ipairs(n) do
  if v % 3 == 0 and v % 5 == 0 then
    m[i] = v .. " is divisible by both 3 and 5!\n"
  elseif v % 3 ~= 0 and v % 5 ~= 0 then
    m[i] = v .. " is not divisible by either 3 or 5!\n"
  elseif v % 3 == 0 then
    m[i] = v .. " is divisible by 3 only!\n"
  elseif v % 5 == 0 then
    m[i] = v .. " is divisible by 5 only!\n"
  end
end
for i,v in ipairs(m) do	print(m[i]) end

I'm working on making it even leaner, I'll keep you posted!

--CarloDiCelico

This is really not a good programming sample, even if you were to make the following fixes...

--JohnBelmonte

I didn't even know "for x=1,100 do" was possible! That definitely makes a difference. I was working on how to keep the program from recalculating those values and essentially came up with the same solution, storing them in local variables. I'm learning Lua as I go, so forgive me for not coding as eloquently as I could in some other language! Besides, the whole point was to do it using tables, not to do it without tables. I just wanted to play with the tables, hence the name of the page.

--CarloDiCelico

"for x=1,100 do" is in the very first line of your code... Anyway there is nothing wrong with learning the ropes, but this seems more appropriate as a local file on your computer rather than a public wiki page.

Half-baked content might also be temporarily placed on one's personal page (e.g. CarloDiCelico) and possibly later migrated to some more permanent place on the wiki once it's more clear where it should go. Below is another way to write that. --DavidManura

local text = {
  [false] = {
    [false] = "%d is not divisible by either %d or %d!",
    [true]  = "%d is not divisible by %d but is divisible by %d!",
  },
  [true] = {
    [false] = "%d is divisible by %d but not by %d!",
    [true]  = "%d is divisible by both %d and %d!"
  }
}
for x=1,100 do
  print(string.format(text[x % 3 == 0][x % 5 == 0], x, 3, 5))
end

Here are two more variants. --ShmuelZeigerman

-- common to both variants
local a, b, c, d  =
  " is not divisible by either 3 or 5",
  " is divisible by 3 but not by 5",
  " is divisible by 5 but not by 3",
  " is divisible by both 3 and 5"

-- variant #1 --
local t = { a,b,c,d }
for x=1,100 do
  print(x .. t[(x%3==0 and 1 or 0) + (x%5==0 and 2 or 0) + 1])
end

-- variant #2 --
for x=1,100 do
  print(x .. (x%3==0 and (x%5==0 and d or b) or (x%5==0 and c or a)))
end

I would argue that the discussion my poor code has provoked is more than reason enough to leave it up here! This is great and the positive remarks and code samples are useful to newcomers who are trying to learn Lua and get a feel for Lua's community.

-- CarloDiCelico


FindPage · RecentChanges · preferences
edit · history
Last edited September 5, 2008 1:25 am GMT (diff)