Sierpinski carpet

From Rosetta Code
Revision as of 23:13, 14 March 2008 by 64.131.185.25 (talk) (Sierpinski carpet task, J example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Sierpinski carpet
You are encouraged to solve this task according to the task description, using any language you may know.

Produce an ASCII representation of a Sierpinski carpet of order N. For example, the Sierpinski carpet of order 2 should look like this:

+-------------+-------------+-------------+
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
+-------------+-------------+-------------+
|+---+---+---+|             |+---+---+---+|
||   |   |   ||             ||   |   |   ||
|+---+---+---+|             |+---+---+---+|
||   |   |   ||             ||   |   |   ||
|+---+---+---+|             |+---+---+---+|
||   |   |   ||             ||   |   |   ||
|+---+---+---+|             |+---+---+---+|
+-------------+-------------+-------------+
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
||   |   |   |||   |   |   |||   |   |   ||
|+---+---+---+|+---+---+---+|+---+---+---+|
+-------------+-------------+-------------+

See also Sierpinski triangle

J

Like the sierpinski triangle, the carpet is easy to produce in J. One way:

   (a:(<1;1)}3 3$<)^:2'   '
   

or another:

   ((#:7 5 7){_2{.<)^:2'   '