lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Your code is awesome, many thanks,
I will study it carefully.

------------------------------------
C++, Lua, living in Dalian
http://sunxiunan.com/
http://twitter.com/sagasw
------------------------------------


On Tue, Dec 29, 2009 at 10:21 PM, Wim Couwenberg <wim.couwenberg@gmail.com> wrote:
Not sure what you want to do exactly.  The following code prints all partitions.

Bye,
Wim

local function p(set, N, max)
   if N == 0 then
       print(unpack(set))
   else
       for i = max < N and max or N, 1, -1 do
           set[#set + 1] = i
           p(set, N - i, i)
           set[#set] = nil
       end
   end
end

function partitions(n)
   p({}, n, n)
end

partitions(tonumber(...))