Jump to content

Pascal's triangle: Difference between revisions

(ivy, an APL-like language)
Line 4,144:
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1 </pre>
 
A more optimized solution that doesn't require importing, but produces, naturally, uglier output, would look like this:
<lang nim>const ROWS = 10
const TRILEN = toInt(ROWS * (ROWS + 1) / 2) # Sum of arth progression
 
proc printPascalTri(row: Natural, result: var seq[Natural]) =
add(result, 1)
for i in 2..row-1: add(result, result[^row] + result[^(row-1)])
add(result, 1)
echo result[^row..^1]
if row + 1 <= ROWS: printPascalTri(row + 1, result)
 
var triangle = newSeq[Natural](TRILEN) # Avoid reallocations
printPascalTri(1, triangle)</lang>
 
{{out}}
<pre>@[1]
@[1, 1]
@[1, 2, 1]
@[1, 3, 3, 1]
@[1, 4, 6, 4, 1]
@[1, 5, 10, 10, 5, 1]
@[1, 6, 15, 20, 15, 6, 1]
@[1, 7, 21, 35, 35, 21, 7, 1]
@[1, 8, 28, 56, 70, 56, 28, 8, 1]
@[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]</pre>
 
=={{header|OCaml}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.