Catalan numbers: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Lua}}: Minor tidying)
Line 3,017: Line 3,017:
=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang="lua">-- recursive with memoization
<syntaxhighlight lang="lua">-- recursive with memoization
catalan = {[0] = 1}
local catalan = { [0] = 1 }
setmetatable(catalan, {
setmetatable(catalan, {
__index = function(c, n)
__index = function(c, n)
c[n] = c[n-1]*2*(2*n-1)/(n+1)
c[n] = c[n - 1] * 2 * (2 * n - 1) / (n + 1)
return c[n]
return c[n]
end
end
}
})
)


for i=0,14 do
for i = 0, 14 do
print(catalan[i])
print(string.format("%d", catalan[i]))
end</syntaxhighlight>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>1
1
1
1
2
2
Line 3,046: Line 3,044:
742900
742900
2674440</pre>
2674440</pre>

=={{header|MAD}}==
=={{header|MAD}}==