0 Replies - 979 Views - Last Post: 21 September 2010 - 12:41 PM

#1 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

[LUA]Oblong Numbers

Posted 21 September 2010 - 12:41 PM

Description: Tested on Lua 5.1.4 Should work fine. Report bugs.Oblong numbers are the numbers produced by products of two consecutive integers. This is a function to get a table containing Oblong numbers starting from a point.
-- Given a count 'n' and a starting number 's', it will return a table with 'n' 
-- Oblong numbers starting from 's'. 's' can be omitted, and will default to '0'
function oblong(n, s)
    if n < 0 then 
        return nil
    elseif n == 0 then
        return {}
    else
        local t = {}

        -- Small note: Oblong numbers count from zero (against Lua's conventions :D)
        if not s then s = 0 -- if it was omitted
        else s = s-1 end
        
        for i = 1, n do
            t[i] = (s^2)+s
            s = s + 1
        end
        
        return t
    end
end

local p = oblong(5)
for i, v in ipairs(p) do 
    print(i, v)
end


Is This A Good Question/Topic? 0
  • +

Page 1 of 1