How to resolve the algorithm Sierpinski carpet step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski carpet step by step in the V (Vlang) programming language

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a Sierpinski carpet of order   N.

For example, the Sierpinski carpet of order   3   should look like this: The use of the   #   character is not rigidly required for ASCII art. The important requirement is the placement of whitespace and non-whitespace characters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski carpet step by step in the V (Vlang) programming language

Source code in the v programming language

import math

fn main() {
	carpet(3)
}

fn carpet(n int) {
    power := int(math.pow(3.0, n))
    for i in 0..power {
        for j in 0..power {
			if in_carpet(i, j) == true {print("*")} else{print(" ")}
		}
		println('')
    }
}

fn in_carpet(x int, y int) bool {
    mut xx := x
    mut yy := y
    for xx != 0 && yy != 0 {
        if xx % 3 == 1 && yy % 3 == 1 {return false}
        xx /= 3
        yy /= 3
    }
    return true
}

  

You may also check:How to resolve the algorithm Factorial step by step in the Red programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Ceylon programming language