Your task: given an integer n, output an embedded triangle to the nth depth.

Standard methods of input and output, input in any convenient format, output should be as a string. Examples are 0-indexed, you can use 1-indexed if you want. Standard loopholes apply

The left and right sides of each figure is 2^n, when 0 indexed. The top is twice that.



\/ /

\/ /   /
 \/   /
  \  /

This is , so shortest code wins!

This is NOT a duplicate of the Embedded Hexagons! challenge. I don't think any answers from that question could be trivially copied over to this one.

  • 1
    \$\begingroup\$ @BetaDecay Aww, come on. same author, no big deal. \$\endgroup\$ Apr 15, 2017 at 12:20
  • 3
    \$\begingroup\$ Sure, but where does it end? Embedded squares? Embedded Circles? Embedded dodecagons? One challenge IMO is enough \$\endgroup\$
    – Beta Decay
    Apr 15, 2017 at 12:23
  • 6
    \$\begingroup\$ @BetaDecay How does one have an ASCII circle? More to the point, I don't think any answer to the hexagon challenge could be trivially copied over. And what is the problem with similar challenges that are substantially different? If there is one, please let me know. \$\endgroup\$
    – sporkl
    Apr 15, 2017 at 12:26
  • 1
    \$\begingroup\$ I'm nominating to reopen this question because I don't see how the hexagon challenge's answers could be trivially copied to this challenge. \$\endgroup\$
    – hyper-neutrino
    Apr 15, 2017 at 14:08
  • 1
    \$\begingroup\$ I'm voting to reopen, as I don't believe they're dupes. However, I've downvoted the challenge. It's kinda boring to have many sets of embedded <x>, and you've got at least 1 more in teh sandbox. \$\endgroup\$
    – Riker
    Apr 15, 2017 at 14:14

9 Answers 9


Retina, 44 bytes

+`( *)/1
$1/$1 $1/

Try it online!

I only want to print successful replacements, but I don't really know how to do that.

As I'm no longer allowed to add another answer, here's a Batch answer for 192 bytes:

@echo off
set d=-
set l=\
set r=/
for /l %%i in (1,1,%1)do @call set r=%%r%%%%d:-= %%/&call set d=%%d%%-%%d%%
echo -%d%
echo %l%%r%
set l= %l%
set r=%r:~2%
if not "%r%"=="" goto l

And here's a JavaScript (ES7) port of @lanlock4's excellent Mathematica answer for 124 bytes:

n=>[...Array(2**n+1)].map((_,i)=>[...Array(2<<n)].map((_,j)=>i?i+~j?i>j?` `:i+j-(i+j&-i-j)?` `:`/`:`\\`:`-`).join``).join`
<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>

And here's a Charcoal answer for 16 bytes:


Try it online! Link is to verbose version of code. Explanation:

  N                 Input number
 E                  Map over implicit range
    ²               Literal 2
     ι              Current index
   X                Exponentiate
F     «             Loop over resulting powers of 2
       ι            Print right
        ι           Print right again
         ↙          Move down and left
          ↙ι        Print down and left
            ↑       Move up
             ↖ι     Print up and left
               →    Move right
  • \$\begingroup\$ Now you could add another answer, go ahead! (It's quite weird putting irrelevant answers all-in-one.) \$\endgroup\$
    – user92069
    Jun 21, 2020 at 5:04
  • \$\begingroup\$ @Memberfor3months I could, but I don't know what I'd do with my ES7 answer, seeing as Arnauld outgolfed it anyway. \$\endgroup\$
    – Neil
    Jun 21, 2020 at 9:42

Charcoal, 27 bytes


Try it online!

Just modified my submission from the hexagon challenge a bit to make a triangle.

  • \$\begingroup\$ 16 bytes: FENX²ι«ιι↙↙ι↑↖ι→ (1-indexed). \$\endgroup\$
    – Neil
    Dec 27, 2017 at 21:19
  • \$\begingroup\$ @Neil I think this solution is different enough to warrant its own answer \$\endgroup\$
    – user41805
    Dec 28, 2017 at 18:00

Mathematica, 105 bytes

""<>Table[Which[i<1,"-",i==j,"\\",IntegerQ@Log2[i+j-1]&&j>i,"/",1>0," "],{i,0,2^#},{j,2*2^#}]~Riffle~"\n"&

where \n is replaced with a newline.

Explanation: Makes an array of characters (indexed from i = 0 to 2^n vertically, j = 1 to 2^(n+1) horizontally) according to these rules:

  • If i<1 (so i = 0), put a "-" (for the top row)
  • If i==j, put a "\" (escaped, for the left-hand side)
  • If Log2[i+j-1] is an integer (so i+j-1 is a power of 2) and j>i, put a "/" (for the other edges)
  • And put " " everywhere else.

Then ""<>...~Riffle~"\n"& turns this array into a string.


JavaScript (JavaScript Shell), 136 132 bytes

t=2**readline();p=print;p(v='-'.repeat(t*2));for(y=0;y<t;y++)p(' '.repeat(y)+'\\'+v.replace(/-/g,(_,i)=>i&-~i?' ':'/').slice(y*2+1))

Try it:

readline = () => prompt();
print = (...args) => output += (args.join(' ') + '\n');
output = '';

t=2**readline();p=print;p(v='-'.repeat(t*2));for(y=0;y<t;y++)p(' '.repeat(y)+'\\'+v.replace(/-/g,(_,i)=>i&-~i?' ':'/').slice(y*2+1))


  • \$\begingroup\$ readline = prompt; probably suffices. \$\endgroup\$
    – Neil
    Apr 15, 2017 at 13:41

C, 200 bytes

#define P printf(

Try it online!


JavaScript (ES6), 108 95 93 92 bytes

let f =

n=>(g=x=>y>1<<n?'':x<2<<n?' /\\-'[y?~x+y?+!(k=x+y,x<y|k&-k-k):2:3]+g(x+1):`



05AB1E, 45 bytes

I made it! (It's a fun challenge, I don't get why they VTC'd it)


Try it online!


Canvas, 22 bytes

╵rH2;^/L ×;+n}l\nL-×;∔

Try it here!


'rH2;^/L ×;+n}l\nL-×;∔
'                      increment input integer n
 r                     range 0..n
  H          }         push an empty string and start a loop
   2;^                 get 2^i
      /                antidiagonal of that length
       L               get width(don't pop)
         ×;_           add that many spaces to the left
            n          overlap with prev iteration
              l        get height(don't pop)
               \       create a diagonal of that length 
                n      and overlap with the created sides
                 L     get width(don't pop)
                  -×;∔ add that many minuses to the top

PHP, 137 Bytes

for(;$i<1+2**$a=$argn;++$i)for($n=0;$n<$m=2**($a+1);$s[]=2**++$n)echo$n%$m?"":"\n",$i?$i-1!=$n?$i<=$n&in_array($i+$n,$s)?"/":" ":"\\":"-";

Online Version


for($n=0;$n<$m=2**($a+1);$s[]=2**++$n) # fill array for sum compare after loop
echo$n%$m?"":"\n", # print line feed
$i  # false for first line
 ?$i-1!=$n # row number - 1 equal column print \
  ?$i<=$n&in_array($i+$n,$s) # if row number lte column and sum of both in array print /
   :" " # else print space
 :"-"; # fill first line

A recursive way for 199 Bytes

function f($a){global$r;for(;$i<1+2**$a;$i++)for($n=0;$n<$m=2**($a+1);$n++)$r[$i][$n]=$i?$i-1!=$n?$i<=$n&&$i+$n==2**($a+1)?"/":" ":"\\":"-";$a?f($a-1):0;}f($argn);echo join("\n",array_map("join",$r));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.