Jump to content

Pascal's triangle: Difference between revisions

m (→‎{{header|Octave}}: adjusted the formatting, still acceptable even with 3-digits numbers)
Line 322:
END SUBROUTINE Print_Triangle</lang>
 
=={{header|Groovy}}==
=== Recursive(slow) ===
In the spirit of the Haskell "think in whole lists" solution here is a list-driven, minimalist solution:
<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.