Jump to content

Pascal's triangle: Difference between revisions

Line 328:
<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.
 
=== 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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.