Anonymous user
Catalan numbers/Pascal's triangle: Difference between revisions
Added Lua version
No edit summary |
(Added Lua version) |
||
Line 717:
[510,5.491717746183512e+302]
[511,null]</lang>
=={{header|Lua}}==
For each line of odd-numbered length from Pascal's triangle, subtract the middle number from the one immediately to its right. This solution is heavily based on the Lua code to generate Pascal's triangle on the page for that task.
<lang Lua>function nextrow (t)
local ret = {}
t[0], t[#t + 1] = 0, 0
for i = 1, #t do ret[i] = t[i - 1] + t[i] end
return ret
end
function catalans (n)
local t, middle = {1}
for i = 1, n do
middle = math.ceil(#t / 2)
io.write(t[middle] - (t[middle + 1] or 0) .. " ")
t = nextrow(nextrow(t))
end
end
catalans(15)</lang>
{{out}}
<pre>1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
|