自然数の分割
Posted feedbacks - Lua
Lua です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function split(n, m)
if m == 0 then
if n == 0 then coroutine.yield{} end
else
for i=n,0,-1 do
for t in coroutine.wrap(function () split(n-i, m-1) end) do
table.insert(t, 1, i) -- prepend
coroutine.yield(t)
end
end
end
end
function split_iter(n, m)
return coroutine.wrap(function () split(n, m) end)
end
for i in split_iter(5, 3) do
print(table.concat(i, ", "))
end
|


herumi
#4099()
Rating1/1=1.00
[ reply ]