How to resolve the algorithm Sierpinski carpet step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sierpinski carpet step by step in the Nim 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 Nim programming language
Source code in the nim programming language
import math
proc inCarpet(x, y: int): bool =
var x = x
var y = y
while true:
if x == 0 or y == 0:
return true
if x mod 3 == 1 and y mod 3 == 1:
return false
x = x div 3
y = y div 3
proc carpet(n: int) =
for i in 0 ..< 3^n:
for j in 0 ..< 3^n:
stdout.write if inCarpet(i, j): "* " else: " "
echo()
carpet(3)
You may also check:How to resolve the algorithm Even or odd step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the RPL programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the J programming language
You may also check:How to resolve the algorithm Partial function application step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Logical operations step by step in the ReScript programming language