Pascal Triangle Row With In-Place Pass
Grow each row from all ones by folding right-to-left inside every level, so a single vector yields any Pascal row without recursion.
Code
fn pascal_row(row: usize) -> Vec<u64> {
// Build the row in place: each level folds right-to-left
let mut line = vec![1u64; row + 1];
for i in 2..=row {
for j in (1..i).rev() {
line[j] += line[j - 1];
}
}
line
}
fn main() {
for r in [0, 1, 4, 6] {
println!("row {r}: {:?}", pascal_row(r));
}
}Inside the snippet
Pascal Triangle Row With In-Place Pass reduces to the functions pascal_row and main.
Repetition is confined to for i in 2..=row — the rest of the file is straight-line.
Using it
The entry-point signature is fn pascal_row(row: usize) -> Vec<u64>
The run prints row — each prefix names the step that produced it.
main sequences pascal_row; executing the file runs that flow once.
Key statements
fn pascal_row(row: usize) -> Vec<u64> {
// Build the row in place: each level folds right-to-left
let mut line = vec![1u64; row + 1];
for i in 2..=row {
for j in (1..i).rev() {
line[j] += line[j - 1];
}
}
line
}
Interactive Tools for This Topic
Big-O Complexity VisualizerVisualize and compare algorithm time complexities like O(1), O(log n), O(n), O(n log n), and O(n²). Plot growth curves to understand performance scaling at a glance.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Pascal Triangle Row With In-Place Pass print when run?
- Run it as a complete program — the output lines carry labels such as `row`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.