[Question]
  [
[The Sierpinsky Triangle](http://en.wikipedia.org/wiki/Sierpinski_triangle) is a fractal created by taking a triangle, decreasing the height and width by 1/2, creating 3 copies of the resulting triangle, and place them such each triangle touches the other two on a corner. This process is repeated over and over again with the resulting triangles to produce the Sierpinski triangle, as illustrated below.
![enter image description here](https://i.stack.imgur.com/MrjJN.png)
Write a program to generate a Sierpinski triangle. You can use any method you want to generate the pattern, either by drawing the actual triangles, or by using a random algorithm to generate the picture. You can draw in pixels, ascii art, or whatever you want, so long as the output looks similar to the last picture shown above. Fewest characters wins.
  
[Answer]
## HTML + JavaScript, 150 characters (see notes for 126 characters)
Whitespace inserted for readability and not counted.
```
<title></title><canvas></canvas><script>
for(x=k=128;x--;)for(y=k;y--;)
  x&y||document.body.firstChild.getContext("2d").fillRect(x-~y/2,k-y,1,1)
</script>
```
The core of it is applying the rule of coloring pixels for which `x & y == 0` by the conditional `x&y||`, which produces a “Sierpinski right triangle”; and `x-~y/2,k-y` are a coordinate transformation to produce the approximately equilateral display.
![enter image description here](https://i.stack.imgur.com/QUkRS.png)
A less correct (HTML-wise) version is 126 characters:
```
<canvas><script>
for(x=k=128;x--;)for(y=k;y--;)
  x&y||document.body.firstChild.getContext("2d").fillRect(x-~y/2,k-y,1,1)
</script>
```
(The way in which this is less correct is that it omits the `title` element and the end tag of the `canvas` element, both of which are required for a correct document even though omitting them does not change the *interpretation* of the document.)
Three characters can be saved by eliminating `k` in favor of the constant `64`, at the cost of a smaller result; I wouldn't count the `8` option as it has insufficient detail.
Note that a size of 256 or higher requires attributes on the `<canvas>` to increase the canvas size from the default.
[Answer]
## GolfScript (43 42 chars)
```
' /\ /__\ '4/){.+\.{[2$.]*}%\{.+}%+\}3*;n*
```
Output:
```
               /\               
              /__\              
             /\  /\             
            /__\/__\            
           /\      /\           
          /__\    /__\          
         /\  /\  /\  /\         
        /__\/__\/__\/__\        
       /\              /\       
      /__\            /__\      
     /\  /\          /\  /\     
    /__\/__\        /__\/__\    
   /\      /\      /\      /\   
  /__\    /__\    /__\    /__\  
 /\  /\  /\  /\  /\  /\  /\  /\ 
/__\/__\/__\/__\/__\/__\/__\/__\
```
Change the "3" to a larger number for a larger triangle.
[Answer]
**Mathematica - 32 characters** 
```
Nest[Subsuperscript[#,#,#]&,0,5]
```
![enter image description here](https://i.stack.imgur.com/4imXz.png)
**Mathematica - 37 characters** 
```
Grid@CellularAutomaton[90,{{1},0},31]
```
This will produce a 2D table of 0 and 1, where 1s are drawing Sierpinski Triangle.
![enter image description here](https://i.stack.imgur.com/yhyRK.png)
[Answer]
# Python (234)
Maximal golfing, tiny image:
```
#!/usr/bin/env python3
from cairo import*
s=SVGSurface('_',97,84)
g=Context(s)
g.scale(97,84)
def f(w,x,y):
 v=w/2
 if w>.1:f(v,x,y);f(v,x+w/4,y-v);f(v,x+v,y)
 else:g.move_to(x,y);g.line_to(x+v,y-w);g.line_to(x+w,y);g.fill()
f(1,0,1)
s.write_to_png('s.png')
```
Requires `python3-cairo`.
To get a nice large image I needed **239** characters.
![Sierpinski Triangle](https://i.stack.imgur.com/dlT7C.png)
[Answer]
## Python, 101 86
Uses the rule 90 automaton.
```
x=' '*31
x+='.'+x
exec"print x;x=''.join(' .'[x[i-1]!=x[i-62]]for i in range(63));"*32
```
This is longer, but prettier.
```
x=' '*31
x+=u'Δ'+x
exec u"print x;x=''.join(u' Δ'[x[i-1]!=x[i-62]]for i in range(63));"*32
```
Edit: playing with strings directly, got rid of obnoxiously long slicing, made output prettier.
Output:
```
                               Δ                               
                              Δ Δ                              
                             Δ   Δ                             
                            Δ Δ Δ Δ                            
                           Δ       Δ                           
                          Δ Δ     Δ Δ                          
                         Δ   Δ   Δ   Δ                         
                        Δ Δ Δ Δ Δ Δ Δ Δ                        
                       Δ               Δ                       
                      Δ Δ             Δ Δ                      
                     Δ   Δ           Δ   Δ                     
                    Δ Δ Δ Δ         Δ Δ Δ Δ                    
                   Δ       Δ       Δ       Δ                   
                  Δ Δ     Δ Δ     Δ Δ     Δ Δ                  
                 Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ                 
                Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ                
               Δ                               Δ               
              Δ Δ                             Δ Δ              
             Δ   Δ                           Δ   Δ             
            Δ Δ Δ Δ                         Δ Δ Δ Δ            
           Δ       Δ                       Δ       Δ           
          Δ Δ     Δ Δ                     Δ Δ     Δ Δ          
         Δ   Δ   Δ   Δ                   Δ   Δ   Δ   Δ         
        Δ Δ Δ Δ Δ Δ Δ Δ                 Δ Δ Δ Δ Δ Δ Δ Δ        
       Δ               Δ               Δ               Δ       
      Δ Δ             Δ Δ             Δ Δ             Δ Δ      
     Δ   Δ           Δ   Δ           Δ   Δ           Δ   Δ     
    Δ Δ Δ Δ         Δ Δ Δ Δ         Δ Δ Δ Δ         Δ Δ Δ Δ    
   Δ       Δ       Δ       Δ       Δ       Δ       Δ       Δ   
  Δ Δ     Δ Δ     Δ Δ     Δ Δ     Δ Δ     Δ Δ     Δ Δ     Δ Δ  
 Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ   Δ 
Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ Δ
```
[Answer]
## J
```
,/.(,~,.~)^:6,'o'
```
Not ideal, since the triangle is lopsided and followed by a lot of whitespace - but interesting nonetheless I thought.
Output:
```
o                                                               
oo                                                              
o o                                                             
oooo                                                            
o   o                                                           
oo  oo                                                          
o o o o                                                         
oooooooo                                                        
o       o                                                       
oo      oo                                                      
o o     o o                                                     
oooo    oooo                                                    
o   o   o   o                                                   
oo  oo  oo  oo                                                  
o o o o o o o o                                                 
oooooooooooooooo                                                
o               o                                               
oo              oo                                              
o o             o o                                             
oooo            oooo                                            
o   o           o   o                                           
oo  oo          oo  oo                                          
o o o o         o o o o                                         
oooooooo        oooooooo                                        
o       o       o       o                                       
oo      oo      oo      oo                                      
o o     o o     o o     o o                                     
oooo    oooo    oooo    oooo                                    
o   o   o   o   o   o   o   o                                   
oo  oo  oo  oo  oo  oo  oo  oo                                  
o o o o o o o o o o o o o o o o                                 
oooooooooooooooooooooooooooooooo                                
o                               o                               
oo                              oo                              
o o                             o o                             
oooo                            oooo                            
o   o                           o   o                           
oo  oo                          oo  oo                          
o o o o                         o o o o                         
oooooooo                        oooooooo                        
o       o                       o       o                       
oo      oo                      oo      oo                      
o o     o o                     o o     o o                     
oooo    oooo                    oooo    oooo                    
o   o   o   o                   o   o   o   o                   
oo  oo  oo  oo                  oo  oo  oo  oo                  
o o o o o o o o                 o o o o o o o o                 
oooooooooooooooo                oooooooooooooooo                
o               o               o               o               
oo              oo              oo              oo              
o o             o o             o o             o o             
oooo            oooo            oooo            oooo            
o   o           o   o           o   o           o   o           
oo  oo          oo  oo          oo  oo          oo  oo          
o o o o         o o o o         o o o o         o o o o         
oooooooo        oooooooo        oooooooo        oooooooo        
o       o       o       o       o       o       o       o       
oo      oo      oo      oo      oo      oo      oo      oo      
o o     o o     o o     o o     o o     o o     o o     o o     
oooo    oooo    oooo    oooo    oooo    oooo    oooo    oooo    
o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   
oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  oo  
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
```
**A quick explanation:**
The verb `(,~,.~)` is what's doing the work here. It's a [hook](http://www.jsoftware.com/help/primer/hook.htm) which first stitches `,.` the argument to itself (`o` -> `oo`) and then appends the original argument to the output:
```
oo
```
becomes
```
oo
o
```
This verb is repeated 6 times `^:6` with the output of each iteration becoming the input of the next iteration. So
```
oo
o
```
becomes
```
oooo
o o
oo
o
```
which in turn becomes
```
oooooooo
o o o o 
oo  oo
o   o
oooo
o o
oo
o
```
etc. I've then used the oblique adverb on append `,/.` to read the rows diagonally to straighten(ish) the triangle. I didn't need to do this, as [randomra](https://codegolf.stackexchange.com/users/7311/randomra) points out. I could have just reversed `|.` the lot to get the same result. Even better, I could have just used `(,,.~)^:6,'o'` to save the reverse step completely.
Ah well, you live and learn. :-)
[Answer]
# 8086 Machine code - 30 bytes.
NOTE: This is **not my code and should not be accepted as an answer**. I found this while working on a different CG problem [to emulate an 8086 CPU](https://codegolf.stackexchange.com/questions/4732/emulate-an-intel-8086-cpu/9044#9044). The included text file credits **David Stafford**, but that's the best I could come up with.
I'm posting this because it's clever, short, and I thought you'd want to see it.
It makes use of overlapping opcodes to pack more instructions in a smaller space. Amazingly clever. Here is the machine code:
```
B0 13 CD 10 B3 03 BE A0 A0 8E DE B9 8B 0C 32 28 88 AC C2 FE 4E 75 F5 CD 16 87 C3 CD 10 C3
```
A straight-up decode looks like this:
```
0100: B0 13              mov AL, 13h
0102: CD 10              int 10h
0104: B3 03              mov BL, 3h
0106: BE A0 A0           mov SI, A0A0h
0109: 8E DE              mov  DS, SI
010B: B9 8B 0C           mov CX, C8Bh
010E: 32 28              xor  CH, [BX+SI]
0110: 88 AC C2 FE        mov  [SI+FEC2h], CH
0114: 4E                 dec SI
0115: 75 F5              jne/jnz -11
```
When run, when the jump at 0x0115 happens, notice it jumps back to 0x010C, right into the middle of a previous instruction:
```
0100: B0 13              mov AL, 13h
0102: CD 10              int 10h
0104: B3 03              mov BL, 3h
0106: BE A0 A0           mov SI, A0A0h
0109: 8E DE              mov  DS, SI
010B: B9 8B 0C           mov CX, C8Bh
010E: 32 28              xor  CH, [BX+SI]
0110: 88 AC C2 FE        mov  [SI+FEC2h], CH
0114: 4E                 dec SI
0115: 75 F5              jne/jnz -11
010C: 8B 0C              mov  CX, [SI]
010E: 32 28              xor  CH, [BX+SI]
0110: 88 AC C2 FE        mov  [SI+FEC2h], CH
0114: 4E                 dec SI
0115: 75 F5              jne/jnz -11
010C: 8B 0C              mov  CX, [SI]
```
Brilliant! Hope you guys don't mind me sharing this. I know it's not an answer per se, but it's of interest to the challenge.
Here it is in action:
![Running](https://i.stack.imgur.com/XOidp.png)
[Answer]
## Python (42)
I originally wanted to post a few suggestions on [boothbys solution](https://codegolf.stackexchange.com/a/6310/4626) (who actually uses rule 18 :), but i didn't have enough reputation to comment, so i made it into another answer. Since he changed his approach, i added some explanation.
My suggestions would have been:
1. use '%d'\*64%tuple(x) instead of ''.join(map(str,x)
2. shift in zeros instead of wraping the list around
which would have led to the following code (93 characters):
```
x=[0]*63
x[31]=1
exec"print'%d'*63%tuple(x);x=[a^b for a,b in zip(x[1:]+[0],[0]+x[:-1])];"*32
```
But i optimzed further, first by using a longint instead of an integer array and just printing the binary representation (75 characters):
```
x=2**31
exec"print'%d'*63%tuple(1&x>>i for i in range(63));x=x<<1^x>>1;"*32
```
And finally by printing the octal representation, which is already supported by printf interpolation (42 characters):
```
x=8**31
exec"print'%063o'%x;x=x*8^x/8;"*32
```
All of them will print:
```
000000000000000000000000000000010000000000000000000000000000000
000000000000000000000000000000101000000000000000000000000000000
000000000000000000000000000001000100000000000000000000000000000
000000000000000000000000000010101010000000000000000000000000000
000000000000000000000000000100000001000000000000000000000000000
000000000000000000000000001010000010100000000000000000000000000
000000000000000000000000010001000100010000000000000000000000000
000000000000000000000000101010101010101000000000000000000000000
000000000000000000000001000000000000000100000000000000000000000
000000000000000000000010100000000000001010000000000000000000000
000000000000000000000100010000000000010001000000000000000000000
000000000000000000001010101000000000101010100000000000000000000
000000000000000000010000000100000001000000010000000000000000000
000000000000000000101000001010000010100000101000000000000000000
000000000000000001000100010001000100010001000100000000000000000
000000000000000010101010101010101010101010101010000000000000000
000000000000000100000000000000000000000000000001000000000000000
000000000000001010000000000000000000000000000010100000000000000
000000000000010001000000000000000000000000000100010000000000000
000000000000101010100000000000000000000000001010101000000000000
000000000001000000010000000000000000000000010000000100000000000
000000000010100000101000000000000000000000101000001010000000000
000000000100010001000100000000000000000001000100010001000000000
000000001010101010101010000000000000000010101010101010100000000
000000010000000000000001000000000000000100000000000000010000000
000000101000000000000010100000000000001010000000000000101000000
000001000100000000000100010000000000010001000000000001000100000
000010101010000000001010101000000000101010100000000010101010000
000100000001000000010000000100000001000000010000000100000001000
001010000010100000101000001010000010100000101000001010000010100
010001000100010001000100010001000100010001000100010001000100010
101010101010101010101010101010101010101010101010101010101010101
```
Of course there is also a graphical solution (131 characters):
```
from PIL.Image import*
from struct import*
a=''
x=2**31
exec"a+=pack('>Q',x);x=x*2^x/2;"*32
fromstring('1',(64,32),a).save('s.png')
```
![very small sierpinsky triangle](https://i.stack.imgur.com/PJai5.png)
:D
[Answer]
## Haskell (291)
I'm not very good at golfing haskell codes.
```
solve n = tri (putStrLn "") [2^n] n
tri m xs 1 =
  do putStrLn (l1 1 xs "/\\" 0)
     putStrLn (l1 1 xs "/__\\" 1)
     m
tri m xs n=tri m' xs (n-1)
  where m'=tri m (concat[[x-o,x+o]|x<-xs]) (n-1)
        o=2^(n-1)
l1 o [] s t=""
l1 o (x:xs) s t=replicate (x-o-t) ' '++s++l1 (x+2+t) xs s t
```
Output of `solve 4` is:
```
               /\
              /__\
             /\  /\
            /__\/__\
           /\      /\
          /__\    /__\
         /\  /\  /\  /\
        /__\/__\/__\/__\
       /\              /\
      /__\            /__\
     /\  /\          /\  /\
    /__\/__\        /__\/__\
   /\      /\      /\      /\
  /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\
```
[Answer]
**QBasic 151 Characters**
As an example, here is how it can be done in QBasic. 
```
SCREEN 9
H=.5
P=300
FOR I=1 TO 9^6
    N=RND
    IF N > 2/3 THEN
        X=H+X*H:Y=Y*H
    ELSEIF N > 1/3 THEN
        X=H^2+X*H:Y=H+Y*H    
    ELSE
        X=X*H:Y=Y*H
    END IF
    PSET(P-X*P,P-Y*P)
NEXT
```
![enter image description here](https://i.stack.imgur.com/KErlU.png)
[Answer]
## APL (51)
```
      A←67⍴0⋄A[34]←1⋄' ○'[1+32 67⍴{~⊃⍵:⍵,∇(1⌽⍵)≠¯1⌽⍵⋄⍬}A]
```
Explanation:
* `A←67⍴0`: A is a vector of 67 zeroes
* `A[34]←1`: the 34th element is 1
* `{...}A`: starting with A, do:
* `~⊃⍵:`: if the first element of the current row is zero
* `⍵,∇`: add the current row to the answer, and recurse with:
* `(1⌽⍵)≠¯1⌽⍵`: the vector where each element is the XOR of its neighbours in the previous generation
* `⋄⍬`: otherwise, we're done
* `32 67⍴`: format this in a 67x32 matrix
* `1+`: add one to select the right value from the character array
* `' ○'[`...`]`: output either a space (not part of the triangle) or a circle (when it is part of the triangle)
Output:
```
                                 ○                                 
                                ○ ○                                
                               ○   ○                               
                              ○ ○ ○ ○                              
                             ○       ○                             
                            ○ ○     ○ ○                            
                           ○   ○   ○   ○                           
                          ○ ○ ○ ○ ○ ○ ○ ○                          
                         ○               ○                         
                        ○ ○             ○ ○                        
                       ○   ○           ○   ○                       
                      ○ ○ ○ ○         ○ ○ ○ ○                      
                     ○       ○       ○       ○                     
                    ○ ○     ○ ○     ○ ○     ○ ○                    
                   ○   ○   ○   ○   ○   ○   ○   ○                   
                  ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○                  
                 ○                               ○                 
                ○ ○                             ○ ○                
               ○   ○                           ○   ○               
              ○ ○ ○ ○                         ○ ○ ○ ○              
             ○       ○                       ○       ○             
            ○ ○     ○ ○                     ○ ○     ○ ○            
           ○   ○   ○   ○                   ○   ○   ○   ○           
          ○ ○ ○ ○ ○ ○ ○ ○                 ○ ○ ○ ○ ○ ○ ○ ○          
         ○               ○               ○               ○         
        ○ ○             ○ ○             ○ ○             ○ ○        
       ○   ○           ○   ○           ○   ○           ○   ○       
      ○ ○ ○ ○         ○ ○ ○ ○         ○ ○ ○ ○         ○ ○ ○ ○      
     ○       ○       ○       ○       ○       ○       ○       ○     
    ○ ○     ○ ○     ○ ○     ○ ○     ○ ○     ○ ○     ○ ○     ○ ○    
   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   ○   
  ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○  
```
[Answer]
# 80x86 Code / MsDos - 10 Bytes
As a sizecoder specialized for very tiny intros on MsDos i managed to come up with a program that occupies only 10 bytes.
in hex:
```
04 13 CD 10 20 E9 B4 0C E2 F6
```
[![enter image description here](https://i.stack.imgur.com/XIOJV.gif)](https://i.stack.imgur.com/XIOJV.gif)
in asm:
```
X: add al,0x13
int 0x10
and cl,ch
mov ah,0x0C
loop X
```
The first version i coded was "Colpinski" which is 16 bytes in size, and even interactive in a way that you can change the color with the keyboard and the mouse. Together with "Frag" - another sizecoder - we brought that one down to 13 bytes, allowing for a 10 byte program which just contains the core routine.
It gets a bit more interesting when things are animated, so i'll mention another version, [Zoompinski 64](http://www.pouet.net/prod.php?which=67594) - trying to mimic the exact behavior of "Zoompinski C64" in 512 bytes - also for MsDos, 64 bytes in size as the name suggests.
It's possible to optimize this further downto 31 Bytes, while losing elegance, colors and symmetry (source and executable available behind the link above)
[Download the original and comment on "Pouet"](http://www.pouet.net/prod.php?which=62079)
[Answer]
## C ~~127~~ ~~119~~ ~~116~~ ~~108~~ 65
This one uses the trick of the HTML answer of `^ i & j` getting it to print pretty output would take 1 more char (you can get really ugly output by sacrificing the `a^`).
```
a=32,j;main(i){for(;++i<a;)putchar(a^i&j);++j<a&&main(puts(""));}
```
To make it pretty turn `(32^i&j)` to `(32|!(i&j))` and turn it from `++i<a` to `++i<=a`. However wasting chars on looks seems ungolfish to me.
Ugly output:
```
 ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
""  ""  ""  ""  ""  ""  ""  ""
"# !"# !"# !"# !"# !"# !"# !"#
  $$$$    $$$$    $$$$    $$$$
 !$%$% ! !$%$% ! !$%$% ! !$%$%
""$$&&  ""$$&&  ""$$&&  ""$$&&
"#$%&' !"#$%&' !"#$%&' !"#$%&'
      ((((((((        ((((((((
 ! ! !()()()() ! ! ! !()()()()
""  ""((**((**  ""  ""((**((**
"# !"#()*+()*+ !"# !"#()*+()*+
  $$$$((((,,,,    $$$$((((,,,,
 !$%$%()(),-,- ! !$%$%()(),-,-
""$$&&((**,,..  ""$$&&((**,,..
"#$%&'()*+,-./ !"#$%&'()*+,-./
              0000000000000000
 ! ! ! ! ! ! !0101010101010101
""  ""  ""  ""0022002200220022
"# !"# !"# !"#0123012301230123
  $$$$    $$$$0000444400004444
 !$%$% ! !$%$%0101454501014545
""$$&&  ""$$&&0022446600224466
"#$%&' !"#$%&'0123456701234567
      ((((((((0000000088888888
 ! ! !()()()()0101010189898989
""  ""((**((**0022002288::88::
"# !"#()*+()*+0123012389:;89:;
  $$$$((((,,,,000044448888<<<<
 !$%$%()(),-,-010145458989<=<=
""$$&&((**,,..0022446688::<<>>
"#$%&'()*+,-./0123456789:;<=>?
```
I actually kind of like how it looks. But if you insist on it being pretty you can dock four chars. Pretty Output:
```
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
  !!  !!  !!  !!  !!  !!  !!  !
  !   !   !   !   !   !   !   !
!!    !!!!    !!!!    !!!!    !
!     ! !     ! !     ! !     !
      !!      !!      !!      !
      !       !       !       !
!!!!!!        !!!!!!!!        !
! ! !         ! ! ! !         !
  !!          !!  !!          !
  !           !   !           !
!!            !!!!            !
!             ! !             !
              !!              !
              !               !
!!!!!!!!!!!!!!                !
! ! ! ! ! ! !                 !
  !!  !!  !!                  !
  !   !   !                   !
!!    !!!!                    !
!     ! !                     !
      !!                      !
      !                       !
!!!!!!                        !
! ! !                         !
  !!                          !
  !                           !
!!                            !
!                             !
                              !
                              !
```
Leaving up the older 108 char, cellular automata version.
```
j,d[99][99];main(i){d[0][31]=3;for(;i<64;)d[j+1][i]=putchar(32|d[j][i+2]^d[j][i++]);++j<32&&main(puts(""));}
```
~~So I don't think I'm going to get it much shorter than this so I'll explain the code.~~ I'll leave this explanation up, as some of the tricks could be useful.
```
j,d[99][99]; // these init as 0
main(i){ //starts at 1 (argc)
  d[0][48]=3; //seed the automata (3 gives us # instead of !)
  for(;i<98;) // print a row
    d[j+1][i]=putchar(32|d[j][i+2]]^d[j][i++]);
    //relies on undefined behavoir. Works on ubuntu with gcc ix864
    //does the automata rule. 32 + (bitwise or can serve as + if you know
    //that (a|b)==(a^b)), putchar returns the char it prints
  ++j<32&&main(puts(""));
  // repeat 32 times
  // puts("") prints a newline and returns 1, which is nice
}
```
Some output
```
                             # #                               
                            #   #                              
                           # # # #                             
                          #       #                            
                         # #     # #                           
                        #   #   #   #                          
                       # # # # # # # #                         
                      #               #                        
                     # #             # #                       
                    #   #           #   #                      
                   # # # #         # # # #                     
                  #       #       #       #                    
                 # #     # #     # #     # #                   
                #   #   #   #   #   #   #   #                  
               # # # # # # # # # # # # # # # #                 
              #                               #                
             # #                             # #               
            #   #                           #   #              
           # # # #                         # # # #             
          #       #                       #       #            
         # #     # #                     # #     # #           
        #   #   #   #                   #   #   #   #          
       # # # # # # # #                 # # # # # # # #         
      #               #               #               #        
     # #             # #             # #             # #       
    #   #           #   #           #   #           #   #      
   # # # #         # # # #         # # # #         # # # #     
  #       #       #       #       #       #       #       #    
 # #     # #     # #     # #     # #     # #     # #     # #   
#   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #  
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
```
[Answer]
# PostScript, 120 chars
```
-7 -4 moveto
14 0 rlineto
7{true upath dup
2{120 rotate uappend}repeat[2 0 0 2 7 4]concat}repeat
matrix setmatrix
stroke
```
Ghostscript output:
![Rendered Ghostscript output](https://i.stack.imgur.com/FR0FX.png)
This is drawing the figure by recursively tripling what's already drawn.
First step is drawing a line. The line is saved as a userpath, then the userpath is added two more times after rotating 120 degrees each time. `[2 0 0 2 7 4]concat` moves the "rotation point" to the center of the next big white "center triangle" that is to be enclosed by replications of the triangle we already have. Here, we're back to step 1 (creating a upath that's tripled by rotation).
The number of iterations is controlled by the first number in line 3.
[Answer]
# J (9 characters)
Easily the ugliest, you really need to squint to see the output ;)
```
2|!/~i.32
```
produces the output
```
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
```
of course you can display it graphically:
```
load 'viewmat'
viewmat 2|!/~i.32
```
[![Image](https://i.stack.imgur.com/Z2pZo.jpg)](https://i.stack.imgur.com/Z2pZo.jpg)
[Answer]
# Python (75)
I'm two years late to the party, but I'm surprised that nobody has taken this approach yet
```
from pylab import*
x=[[1,1],[1,0]]
for i in'123':x=kron(x,x)
imsave('a',x)
```
![level7](https://i.stack.imgur.com/ZYv1I.png)
Uses the [Kronecker product](http://en.wikipedia.org/wiki/Kronecker_product) to replace a matrix by multiple copies of itself.
I could save two chars by using `x=kron(x,x);x=kron(x,x)` in line three to get a 16x16 pixel image with three visible levels or add another char to the iterator and end up with a 2^16 x 2^16 = 4.3 Gigapixel image and 15 triangle levels.
[Answer]
# APL, ~~37~~ 32 (~~28~~ 23)
### Upright triangle (~~37~~ 32-char)
```
({((-1⌷⍴⍵)⌽⍵,∊⍵)⍪⍵,⍵}⍣⎕)1 2⍴'/\'
```
**Explanation**
* `1 2⍴'/\'`: Create a 1×2 character matrix `/\`
* `{((-1⌷⍴⍵)⌽⍵,∊⍵)⍪⍵,⍵}`: A function that pad the right argument on both sides with blanks to create a matrix double as wide, then laminates the right argument itself doubled onto the bottom.  
E.g. `/\` would become
```
 /\ 
/\/\
```
* `⍣⎕`: Recur the function (user input) times.
**Example output**
```
               /\               
              /\/\              
             /\  /\             
            /\/\/\/\            
           /\      /\           
          /\/\    /\/\          
         /\  /\  /\  /\         
        /\/\/\/\/\/\/\/\        
       /\              /\       
      /\/\            /\/\      
     /\  /\          /\  /\     
    /\/\/\/\        /\/\/\/\    
   /\      /\      /\      /\   
  /\/\    /\/\    /\/\    /\/\  
 /\  /\  /\  /\  /\  /\  /\  /\ 
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
```
### Skewed Triangle (~~28~~ 23-char)
```
({(⍵,∊⍵)⍪⍵,⍵}⍣⎕)1 1⍴'○'
```
**Explaination**
* `1 1⍴'○'`: Create a 1×1 character matrix `○`
* `{(⍵,∊⍵)⍪⍵,⍵}`: A function that pad the right argument on the right with blanks to create a matrix double as wide, then laminates the right argument itself doubled onto the bottom.  
E.g. `○` would become
```
○ 
○○
```
* `⍣⎕`: Recur the function (user input) times.
**Example output**
```
○               
○○              
○ ○             
○○○○            
○   ○           
○○  ○○          
○ ○ ○ ○         
○○○○○○○○        
○       ○       
○○      ○○      
○ ○     ○ ○     
○○○○    ○○○○    
○   ○   ○   ○   
○○  ○○  ○○  ○○  
○ ○ ○ ○ ○ ○ ○ ○ 
○○○○○○○○○○○○○○○○
```
[Answer]
**matlab 56**
```
v=[1;-1;j];plot(filter(1,[1,-.5],v(randi(3,1,1e4))),'.')
```
![enter image description here](https://i.stack.imgur.com/fXJNh.png)
[Answer]
**Logo, 75 characters**
59 characters for just the first function, the second one calls the first with the size and the depth/number of iterations. So you could just call the first function from the interpreter with the command: e 99 5, or whatever size you want to output
```
to e :s :l
if :l>0[repeat 3[e :s/2 :l-1 fd :s rt 120]]
end
to f
e 99 5
end
```
[Answer]
# J (18 characters)
```
' *'{~(,,.~)^:9 ,1
```
## Result
```
*                               
**                              
* *                             
****                            
*   *                           
**  **                          
* * * *                         
********                        
*       *                       
**      **                      
* *     * *                     
****    ****                    
*   *   *   *                   
**  **  **  **                  
* * * * * * * *                 
****************                
*               *               
**              **              
* *             * *             
****            ****            
*   *           *   *           
**  **          **  **          
* * * *         * * * *         
********        ********        
*       *       *       *       
**      **      **      **      
* *     * *     * *     * *     
****    ****    ****    ****    
*   *   *   *   *   *   *   *   
**  **  **  **  **  **  **  **  
* * * * * * * * * * * * * * * * 
********************************
```
[Answer]
# Python (90 chars)
```
from turtle import*
def l():left(60)
def r():right(60)
def f():forward(1)
def L(n):
 if n:n-=1;R(n);l();L(n);l();R(n)
 else:f()
def R(n):
 if n:n-=1;L(n);r();R(n);r();L(n)
 else:f()
l();L(8)
```
[**Try it online**](https://trinket.io/python/8acda70c9b)
Draw fractal line filling Sierpinsky Triangle
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 12 bytes
```
×.≥∘⍉⍨↑,⍳⎕⍴2
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/4fnq73qHPpo44Zj3o7H/WueNQ2UedR72agoke9W4z@A5X8T@MyBQA "APL (Dyalog Classic) – Try It Online")
[Answer]
# Asymptote, 152 bytes
I'll add this, mostly since I've seen more or less no answers in asymptote on this site. A few wasted bytes for nice formatting and generality, but I can live with that. Changing A,B and C will change where the corners of the containing triangle are, but probably not in the way you think. Increase the number in the inequality to increase the depth.
```
pair A=(0,0),B=(1,0),C=(.5,1);void f(pair p,int d){if(++d<7){p*=2;f(p+A*2,d);f(p+B*2,d);f(p+C*2,d);}else{fill(shift(p/2)*(A--B--C--cycle));}}f((0,0),0);
```
or ungolfed and readable
```
pair A=(0,0), B=(1,0), C=(.5,1);
void f(pair p, int d) {
    if (++d<7) {
        p *= 2;
        f(p+A*2,d);
        f(p+B*2,d);
        f(p+C*2,d);
    } else {
        fill(shift(p/2)*(A--B--C--cycle));
    }
}
f((0,0),0);
```
So asymptote is a neat vector graphics language with somewhat C-like syntax. Quite useful for somewhat technical diagrams. Output is of course in a vector format by default (eps, pdf, svg) but can be converted into basically everything imagemagick supports. Output:
[![Sierpinski triangle](https://i.stack.imgur.com/1XyYE.png)](https://i.stack.imgur.com/1XyYE.png)
[Answer]
# [Mathematica](https://www.wolfram.com/mathematica/), 29 bytes
```
Image@Array[BitAnd,{2,2}^9,0]
```
![Image@Array[BitAnd,{2,2}^9,0]](https://i.stack.imgur.com/7IR6g.jpg)
The Sierpinski tetrahedron can be drawn in a similar way:
```
Image3D[1-Array[BitXor,{2,2,2}^7,0]]
```
![Image3D[1-Array[BitXor,{2,2,2}^7,0]]](https://i.stack.imgur.com/TUzsq.jpg)
[Answer]
# [J](http://jsoftware.com/), ~~37~~ 35 bytes
*-2 bytes thanks to FrownyFrog*
```
(,.~,~' '&,.^:#)@[&0' /\',:'/__\'"_
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/NXT06nTq1BXU1XT04qyUNR2i1QzUFfRj1HWs1PXj42PUleL/a3Il2uoZKZiogCQUQKIK6lx@TnoKqckZ@QqJXGAqTcFQAcJSV4eJGKELGKMLmHBx/QcA "J – Try It Online")
This is Peter Taylor's ascii art version converted to J. Could save bytes with a less pretty version, but why?
```
       /\       
      /__\      
     /\  /\     
    /__\/__\    
   /\      /\   
  /__\    /__\  
 /\  /\  /\  /\ 
/__\/__\/__\/__\
```
[Answer]
# [Lua](http://golly.sourceforge.net/Help/lua.html) script in [Golly](http://golly.sourceforge.net/), 54 bytes
```
g=golly()
g.setrule("W60")
g.setcell(0,0,1)
g.run(512)
```
Golly is a cellular automata simulator with Lua and Python scripting support.
This script sets the rule to Wolfram Rule 60, sets the cell at (0,0) to 1, and runs 512 steps.
[![enter image description here](https://i.stack.imgur.com/kTOs5.png)](https://i.stack.imgur.com/kTOs5.png)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
1=IGx^Db,
```
[Try it online!](https://tio.run/##yy9OTMpM/f/f0NbTvSLOJUnn/39jIwA "05AB1E – Try It Online")
The number of rows is equal to the input.
# Explanation:
```
1=IGx^Db,
1=         print "1" and push 1
  IG       for N in range(1, I): (where I is the input)
    x      push last element on stack multiplied by 2
     ^     pop last two elements, push their bitwise xor (output is in decimal)
      D    duplicate last element of stack
       b,  replace last element of stack with its binary representation, then print/pop
```
This takes advantage of the fact that a(n+1) = a(n) XOR 2\*a(n), which I found on [this relevant OEIS page](https://oeis.org/A001317)
[Answer]
**Mathematica 63**
```
ListPlot@ReIm@NestList[#/2+RandomChoice[{0,1,-1}^(1/3)]&,0.,8!]
```
[![enter image description here](https://i.stack.imgur.com/zF0bb.png)](https://i.stack.imgur.com/zF0bb.png)
**Mathematica 67**
```
Region@Polygon@ReIm@Nest[Tr/@Tuples[{p,#}/2]&,{p={0,1,-1}^(1/3)},6]
```
[![enter image description here](https://i.stack.imgur.com/WbSgv.png)](https://i.stack.imgur.com/WbSgv.png)
[Answer]
# 8086 Machine code - 18 bytes (unknown author) / 27 bytes (mine)
In early 1990s, I organized a small competition on programmer's board on a Croatian BBS, who can write the smallest program to draw the Sierpinski triangle.
My best solution was 27 bytes long, but there were several even smaller and unique solutions, with smallest one being only 18 bytes of machine code. Unfortunately, I forgot the name of the person who wrote it - so if the original author is reading this, please contact me. :)
---
**18 bytes solution by an unknown Croatian programmer**:
```
Bytes: B0 13 CD 10 B5 A0 8E D9 30 84 3F 01 AC 32 04 E2 F7 C3
0100 B013          MOV     AL,13
0102 CD10          INT     10
0104 B5A0          MOV     CH,A0
0106 8ED9          MOV     DS,CX
0108 30843F01      XOR     [SI+013F],AL
010C AC            LODSB
010D 3204          XOR     AL,[SI]
010F E2F7          LOOP    0108
0111 C3            RET
```
Which draws this:
[![Sierpinski triangle in 18 bytes of 8086 machine code](https://i.stack.imgur.com/xH4s2.png)](https://i.stack.imgur.com/xH4s2.png)
---
**My 27 bytes solution**:
```
Bytes: B0 13 CD 10 B7 A0 8E C3 8E DB BF A0 00 AA B9 C0 9E BE 61 FF AC 32 04 AA E2 FA C3
0100 B013          MOV     AL,13
0102 CD10          INT     10
0104 B7A0          MOV     BH,A0
0106 8EC3          MOV     ES,BX
0108 8EDB          MOV     DS,BX
010A BFA000        MOV     DI,00A0
010D AA            STOSB
010E B9C09E        MOV     CX,9EC0
0111 BE61FF        MOV     SI,FF61
0114 AC            LODSB
0115 3204          XOR     AL,[SI]
0117 AA            STOSB
0118 E2FA          LOOP    0114
011A C3            RET
```
Which draws this:
[![Sierpinski triangle in 27 bytes of 8086 machine code](https://i.stack.imgur.com/UXaO2.png)](https://i.stack.imgur.com/UXaO2.png)
[Answer]
## C, 106 chars
```
i,j;main(){for(;i<32;j>i/2?puts(""),j=!++i:0)
printf("%*s",j++?4:33-i+i%2*2,i/2&j^j?"":i%2?"/__\\":"/\\");}
```
(It still amuses me that `puts("")` is the shortest way to output a newline in C.)
Note that you can create larger (or smaller) gaskets by replacing the `32` in the `for` loop's test with a larger (smaller) power of two, as long as you also replace the `33` in the middle of the `printf()` with the power-of-two-plus-one.
]