Catalan numbers/Pascal's triangle: Difference between revisions

Added Mathematica
m (Moved Python 2.7 code into Python section)
(Added Mathematica)
Line 114:
->
</pre>
 
 
=={{header|Mathematica}}==
This builds the entire Pascal triangle that's needed and holds it in memory. Very inefficienct, but seems to be what is asked in the problem.
<lang Mathematica>nextrow[lastrow_] := Module[{output},
output = ConstantArray[1, Length[lastrow] + 1];
Do[
output[[i + 1]] = lastrow[[i]] + lastrow[[i + 1]];
, {i, 1, Length[lastrow] - 1}];
output
]
pascaltriangle[size_] := NestList[nextrow, {1}, size]
catalannumbers[length_] := Module[{output, basetriangle},
basetriangle = pascaltriangle[2 length];
list1 = basetriangle[[# *2 + 1, # + 1]] & /@ Range[length];
list2 = basetriangle[[# *2 + 1, # + 2]] & /@ Range[length];
list1 - list2
]
(* testing *)
catalannumbers[15]</lang>
{{out}}
<pre>{1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845}</pre>
 
 
 
=={{header|MATLAB}} / {{header|Octave}}==
Anonymous user