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, y)
	while x <> 0 and y <> 0
		if(x mod 3) = 1 and (y mod 3) = 1 then return False
		y = int(y / 3): x = int(x / 3)
	end while
	return True
end function
 
Subroutine carpet(n)
	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 subroutine
 
for k = 0 to 3
	print "N = "; k
	call carpet(k)
	print
next k
end
 