How to resolve the algorithm Sierpinski carpet step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski carpet step by step in the Wren 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 Wren programming language

Source code in the wren programming language

var inCarpet = Fn.new { |x, y|
    while (true) {
        if (x == 0 || y == 0) return true
        if (x%3 == 1 && y%3 == 1) return false
        x = (x/3).floor
        y = (y/3).floor
    }
}

var carpet = Fn.new { |n|
    var power = 3.pow(n)
    for (i in 0...power) {
        for (j in 0...power) {
            System.write(inCarpet.call(i, j) ? "#" : " ")
        }
        System.print()
    }
}

carpet.call(3)

  

You may also check:How to resolve the algorithm CSV data manipulation step by step in the C# programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the ABAP programming language
You may also check:How to resolve the algorithm Permutations step by step in the Glee programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Perl programming language