Pascal's triangle: Difference between revisions

Content added Content deleted
Line 4,147: Line 4,147:
A more optimized solution that doesn't require importing, but produces, naturally, uglier output, would look like this:
A more optimized solution that doesn't require importing, but produces, naturally, uglier output, would look like this:
<lang nim>const ROWS = 10
<lang nim>const ROWS = 10
const TRILEN = toInt(ROWS * (ROWS + 1) / 2) # Sum of arth progression
const TRILEN = toInt(ROWS * (ROWS + 1) / 2) # Sum of arth progression
var triangle = newSeq[Natural](TRILEN) # Avoid reallocations


proc printPascalTri(row: Natural, result: var seq[Natural]) =
proc printPascalTri(row: Natural, result: var seq[Natural]) =
Line 4,153: Line 4,154:
for i in 2..row-1: add(result, result[^row] + result[^(row-1)])
for i in 2..row-1: add(result, result[^row] + result[^(row-1)])
add(result, 1)
add(result, 1)

echo result[^row..^1]
echo result[^row..^1]
if row + 1 <= ROWS: printPascalTri(row + 1, result)
if row + 1 <= ROWS: printPascalTri(row + 1, result)


var triangle = newSeq[Natural](TRILEN) # Avoid reallocations
printPascalTri(1, triangle)</lang>
printPascalTri(1, triangle)</lang>