Pascal's triangle: Difference between revisions
Content added Content deleted
m (→Recursive) |
|||
Line 326: | Line 326: | ||
=== Recursive === |
=== Recursive === |
||
In the spirit of the Haskell "think in whole lists" solution here is a list-driven, minimalist solution: |
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] : GroovyCollections.transpose([[0] + pascal(n - 1), pascal(n - 1) + [0]]).collect { it.sum() } }</lang> |
<lang groovy>def pascal = { n -> (n <= 1) ? [1] : GroovyCollections.transpose([[0] + pascal(n - 1), pascal(n - 1) + [0]]).collect { it.sum() } }</lang> |
||
However, this solution is horribly inefficient (O(''n''**2)). It slowly grinds to a halt on a reasonably powerful PC after about line 25 of the triangle. |
However, this solution is horribly inefficient (O(''n''**2)). It slowly grinds to a halt on a reasonably powerful PC after about line 25 of the triangle. |
||