PHP (5.6+), 9894 bytes
<?for(;$H>$e*=3or$e=($i+=$e&&print"$s
")<${$s=H}=pow(3,$argn);=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;
Used with -F
command line option. Assumes interpreter defaults (-n
). Will not work on versions previous to 5.6, due to the power operator.
Sample usage
$ echo 3|php -nF h-carpet.php
H H H H H H H H
HHH HHH HHH HHH
H H H H H H H H
H HH HH H H HH HH H
HHHHHHHHH HHHHHHHHH
H HH HH H H HH HH H
H H H H H H H H
HHH HHH HHH HHH
H H H H H H H H
H H H HH H H HH H H H
HHH HHHHHH HHHHHH HHH
H H H HH H H HH H H H
H HH HH HH HH HH HH HH HH H
HHHHHHHHHHHHHHHHHHHHHHHHHHH
H HH HH HH HH HH HH HH HH H
H H H HH H H HH H H H
HHH HHHHHH HHHHHH HHH
H H H HH H H HH H H H
H H H H H H H H
HHH HHH HHH HHH
H H H H H H H H
H HH HH H H HH HH H
HHHHHHHHH HHHHHHHHH
H HH HH H H HH HH H
H H H H H H H H
HHH HHH HHH HHH
H H H H H H H H