Task
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.



Related task

   Sierpinski triangle


 
Function in_carpet(x As Uinteger, y As Uinteger) As Boolean
    While x <> 0 And y <> 0
        If(x Mod 3) = 1 And (y Mod 3) = 1 Then Return False
        y = y \ 3: x = x \ 3
    Wend 
    Return True
End Function
 
Sub carpet(n As Uinteger)
    Dim As Uinteger i, j, k = (3^n)-1
 
    For i = 0 To k
        For j = 0 To k
            If in_carpet(i, j) Then Print("#"); Else Print(" ");
        Next j
        Print
    Next i
End Sub
 
For k As Byte = 0 To 4
    Print !"\nN ="; k
    carpet(k)
Next k
Sleep
 