How to resolve the algorithm Floyd's triangle step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Floyd's triangle step by step in the Rust programming language
Table of Contents
Problem Statement
Floyd's triangle lists the natural numbers in a right triangle aligned to the left where
The first few lines of a Floyd triangle looks like this:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Floyd's triangle step by step in the Rust programming language
Source code in the rust programming language
fn main() {
floyds_triangle(5);
floyds_triangle(14);
}
fn floyds_triangle(n: u32) {
let mut triangle: Vec<Vec<String>> = Vec::new();
let mut current = 0;
for i in 1..=n {
let mut v = Vec::new();
for _ in 0..i {
current += 1;
v.push(current);
}
let row = v.iter().map(|x| x.to_string()).collect::<Vec<_>>();
triangle.push(row);
}
for row in &triangle {
let arranged_row: Vec<_> = row
.iter()
.enumerate()
.map(|(i, number)| {
let space_len = triangle.last().unwrap()[i].len() - number.len() + 1;
let spaces = " ".repeat(space_len);
let mut padded_number = spaces;
padded_number.push_str(&number);
padded_number
})
.collect();
println!("{}", arranged_row.join(""))
}
}
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Slate programming language
You may also check:How to resolve the algorithm Special variables step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Arrays step by step in the Java programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Phixmonti programming language