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 incarpet(x,y)
set a = x
set b = y
while floor(a)>0 and floor(b)>0
if floor(a mod 3) = 1 and floor(b mod 3) = 1
set a = -1
set b = -1
else
set a = a / 3
set b = b / 3
end if
end while
if a < 0
set result = "_"
else
set result = "#" 
end if
end function
 
program carpet(n)
set d = pow(3,n)
set y = 0
while y < d
set x = 0
set result = " "
while x < d
set result = result . incarpet(x,y)
set x = x  + 1
end while
echo result
set y = y + 1
end while
end program
 
run carpet(3)
 