A Russian nesting doll, more commonly known as a Matryoshka doll, is a doll which contains a smaller version of itself, which then contains another smaller version of itself, which contains a smaller version of itself, which contains a smaller version of itself, which contains a smaller version of itself... - until finally, the last one is empty.

Today your goal is to emulate this Russian tradition by writing a program or function that, when it contains itself N times, will print itself containing N-1 copies of itself times.

For example, the doll program abcd will have the N=3 program abababcdcdcd, which will print the N=2 program ababcdcd, which prints the original N=1 program abcd, which finally prints N=0, which is empty. This should theoretically work for any reasonable value of N.


  • Here is a TIO program to help generate doll programs based on your program
  • Standard Quine Rules apply
  • Standard Loopholes apply
  • 'Contains' means directly in the center of the previous version, so your solution must have a positive even number of bytes. A program of length 10 will have a copy of the original inserted after the fifth byte, then another after the tenth byte etc.
  • A single trailing whitespace is allowed in the output
  • As this is , your goal is to make your N=1 program as short as possible.
  • An explanation of your code would be appreciated
  • \$\begingroup\$ Sandbox post (deleted) \$\endgroup\$
    – Jo King
    Commented Feb 2, 2018 at 9:12
  • \$\begingroup\$ For what N is the code size measured? \$\endgroup\$
    – flawr
    Commented Feb 2, 2018 at 9:52
  • \$\begingroup\$ @flawr N=1..... \$\endgroup\$
    – Jo King
    Commented Feb 2, 2018 at 10:06
  • 1
    \$\begingroup\$ Related: One More Program and I'm Out! \$\endgroup\$ Commented Feb 2, 2018 at 11:15
  • 1
    \$\begingroup\$ @noodleman hmm, tough question. i would say that as long as the output encoding matches the program encoding (as should be mandatory for quines), and that the program can be split evenly, e.g. 3 bytes split into 1.5+1.5 bytes \$\endgroup\$
    – Jo King
    Commented Sep 30, 2023 at 16:36

7 Answers 7


Underload, 4 bytes

N=1: Try it online.


N=2: Try it online.

 ( ()S)S

N=3: Try it online.

 ( ( ()S)S)S


Self-explanatory, but I add it anyway.

  • (...)S prints anything between the parenthesis to STDOUT
  • The space before it is a no-op to make the byte-count even and comply to the rules of the challenge.

JavaScript, 36 32 bytes

Takes advantage of the fact that Function.prototype.toString() takes no arguments and will therefore ignore any that are passed to it.

Partly inspired by user202729's solution.

f=_=>f.toString( ).slice(14,-16)

Try it

o.innerHTML=["<span>Five</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Four</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Three</span>",(f=_=>f.toString(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16) ).slice(14,-16))(),"<span>Two</span>",(f=_=>f.toString(f=_=>f.toString( ).slice(14,-16) ).slice(14,-16))(),"<span>One</span>",(f=_=>f.toString( ).slice(14,-16))(),"<span>Thunderbirds Are Go!</span>"].join`\n`
<pre id=o></pre>


JavaScript (Node.js), 46 bytes

Full program. So console.log is necessary.

Use an idea from this answer to save some bytes.

l=console.log;   g=_=>{};l((''+g).slice(4,-1))

Try it online! Try it online twice! Try it online, three times!

My approach is similar to that used in Kevin Cruijssen's answer, find a nestable structure (a function in this case).


Jelly, 16 bytes


Try it online!

Doubled: Try it online!

Tripled: Try it online!

Jelly doesn't have any nestable structure, but its string literals are auto-terminated.

Ṿḣ-9Ḋ    First chain. (monadic)
Ṿ        Uneal. (to string)
 ḣ-9     Take the ead, ends at the -9'th character.
    Ḋ    equeue, remove the first character.

     ð             Terminate the first chain, start a new one.
      }            Convert the last monadic chain to a dyadic one.
       “““““““““   String literal.
                   This matches the pattern <dyad> <nilad>, so applies
                   the the corresponding rules. This way a link can take data
                   to the right of it.

Will try different approaches to see if they can be shorter.


DipDup, 2 bytes


It pushes the list onto the stack, and prints it without the outmost brackets.

N = 1: Try it online!

N = 2: Try it online!

N = 3: Try it online!


dc, 4 bytes


Similar to some other answers, since strings in dc have start ([) and end (]) delimiters (that is, " doesn't perform both duties, etc.), they're nestable without any real effort. p to print.

N=1: Try it online!

N=2: Try it nested!

N=3: Try it nesteder!


Tcl, 12 bytes

puts {}     

Try it online!

It's just another language that also has this.


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.