DEV Community

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

Posted on

2

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay