Rosetta Code
Pascal's triangle
Generate the opening rows of Pascal's triangle iteratively.
Source
rosettacode/popular/pascals_triangle.vibe
# title: Pascal's triangle
# source: https://rosettacode.org/wiki/Pascal%27s_triangle
# category: Rosetta Code
# difficulty: Intro
# summary: Generate the opening rows of Pascal's triangle iteratively.
# tags: popular, math, arrays, iteration
# vibe: 0.2
def next_row(previous)
if previous.empty?
return [1]
end
row = [1]
index = 1
while index < previous.size
row = row.push(previous[index - 1] + previous[index])
index = index + 1
end
row = row.push(1)
row
end
def build_triangle(rows_count)
rows = []
current = []
produced = 0
while produced < rows_count
current = next_row(current)
rows = rows.push(current)
produced = produced + 1
end
rows
end
def run
build_triangle(7)
end
Output
Press run to execute run from this example.