Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,23 @@
import sequtils, strutils
proc printPascalTriangle(n: int) =
## Print a Pascal triangle.
# Build the triangle.
var triangle: seq[seq[int]]
triangle.add @[1]
for _ in 1..<n:
triangle.add zip(triangle[^1] & @[0], @[0] & triangle[^1]).mapIt(it[0] + it[1])
# Build the lines to display.
let length = len($max(triangle[^1])) # Maximum length of number.
var lines: seq[string]
for row in triangle:
lines.add row.mapIt(($it).center(length)).join(" ")
# Display the lines.
let lineLength = lines[^1].len # Length of largest line (the last one).
for line in lines:
echo line.center(lineLength)
printPascalTriangle(10)

View file

@ -0,0 +1,13 @@
const ROWS = 10
const TRILEN = toInt(ROWS * (ROWS + 1) / 2) # Sum of arth progression
var triangle = newSeqOfCap[Natural](TRILEN) # Avoid reallocations
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)
printPascalTri(1, triangle)