Pascal's triangle: Difference between revisions

Content added Content deleted
Line 328: Line 328:
<lang groovy>pascal = { n -> n <= 1 ? [1] : (0..<n).collect { i -> ([0] + pascal(n - 1))[i] + (pascal(n - 1) + [0])[i]}}</lang>
<lang groovy>pascal = { n -> n <= 1 ? [1] : (0..<n).collect { i -> ([0] + pascal(n - 1))[i] + (pascal(n - 1) + [0])[i]}}</lang>
However, this solution is horribly inefficient (O(''n''!)). It slowly grinds to a halt on a reasonably powerful PC after about line 8 of the triangle.
However, this solution is horribly inefficient (O(''n''!)). It slowly grinds to a halt on a reasonably powerful PC after about line 8 of the triangle.

=== Resursive(better) ===
A better solution keeps the spirit of the list-based solution but calculates the recursive step-down only once per step. It may be less terse and functional, but it much more efficient (O(''n''**2)).


=={{header|Haskell}}==
=={{header|Haskell}}==