DEV Community

Cover image for Algorithm for x-mass
Antonov Mike
Antonov Mike

Posted on

Algorithm for x-mass

When I started learning Rust (2021.11.03), the first thing I encountered was printing a square and a triangle. I was confused at first. Then, when I understood how to write simple algorithms, I decided to write an algorithm for printing a Christmas tree. And in January 2022 I got this:
simple
This is the algorithm

use std::thread;
use std::time::Duration;

fn main() { tannenbaum(7) }

fn tannenbaum(x: i32) {
    let mut top = 0;
    let mut i = 1;

    while top < x/2 {
        for _empty_space in 0..2     { print!(" ") }
        top += 1
    }

    if x%2 == 1                      { print!(" ") }

    println!("A");

    while i <= x {
        for _empty_space in 0..(x-i) { print!(" ") }
        for _left_side   in 0..i     { print!("L") }
        for _middle      in i..i+1   { print!("M") }
        for _right_side  in 0..i     { print!("R") }

        println!();

        thread::sleep(Duration::from_millis(120));
        i += 1
    }

    for _stem in 0..(x-1)            { print!(" ") }

    println!("ШШШ");
}
Enter fullscreen mode Exit fullscreen mode

Try to colorize it using

[dependencies]
colored = "2"
Enter fullscreen mode Exit fullscreen mode

colorized

Happy Holidays

Top comments (0)