The nth Catalan number can be calculated using either recursion or dynamic programming. Here's how to do it in Swift using recursion:
main.swift201 chars12 lines
Here's how to do the same thing using dynamic programming, which is more efficient:
main.swift258 chars12 lines
Both functions take an integer n
as input and return the nth Catalan number.
gistlibby LogSnag