Make a Quine.

Seems easy right? Well this quine must output itself plus its first character, which then outputs itself plus its second character, and so on.

This way the quine should in several generations output two copies.

Example: Lets your code be x. Running it should output x + x[:1]. Running the resulting program should output x + x[:2] and so on...

If your code was foobar, running this should output foobarf. Running this should output foobarfo. And so on and so forth following this pattern:


Your Program must be longer than 2 Bytes and must output only ONE extra character of its own code each iteration.

  • 3
    \$\begingroup\$ I suspect that this challenge is impossible in most languages, given that reading source code is forbidden by default. \$\endgroup\$ May 4, 2017 at 18:12
  • 13
    \$\begingroup\$ @ØrjanJohansen and then Dennis shows up \$\endgroup\$
    – Rod
    May 4, 2017 at 18:13
  • 2
    \$\begingroup\$ @Rod Well I didn't say all, it's just that many/most languages have no obvious way to add arbitrary fragments of code to the end in such a way that (1) it doesn't give a syntax error (2) the program can detect the change. \$\endgroup\$ May 4, 2017 at 18:23
  • 3
    \$\begingroup\$ Since this is a very unusual quine, are the usual quine loopholes still disallowed? \$\endgroup\$
    – Draconis
    May 4, 2017 at 20:14

7 Answers 7


Zsh, 110 108 100 bytes

a=`<&0`<<''<<<t;b=${a:0:50};printf $b$b${a:0:-50}
a=`<&0`<<''<<<t;b=${a:0:50};printf $b$b${a:0:-50}

Try it online!

So it is possible.


a=`<&0`<<''<<<t;       # Set A to everything following this line, until eof or
                       #   an empty line (which never happens before eof) encountered.
                       # A "t" is appended to prevent automatic trimming of newlines.
b=${a:0:50};           # Set B to the first line.
printf $b$b${a:0:-50}  # Print two copies of B and
                       #   A with 50 trailing characters removed.

R, 289 bytes

s<-c("s<-", "i=get0('i',ifnotfound=0)+1;p=paste0(s,substr(get0('p',ifnotfound=s),1,i),collapse='');cat(s[1]);dput(s);cat(paste0(s[2],substr(p,1,i)))#")

credit to this quine for inspiration. Only works if run in the same R environment as the previous quine is run.

  • \$\begingroup\$ an explanation will be forthcoming...I haven't tested it 288 times yet, but I'm fairly convinced it's correct \$\endgroup\$
    – Giuseppe
    May 4, 2017 at 19:14
  • \$\begingroup\$ It provably should be 289 bytes since the quine adds a newline character there but anyway its great you've solved it! \$\endgroup\$
    – IQuick 143
    May 4, 2017 at 19:17
  • \$\begingroup\$ ah, you're right, stupid cat adding newlines. \$\endgroup\$
    – Giuseppe
    May 4, 2017 at 19:18
  • \$\begingroup\$ But is this a full program? Are the generated code full programs? \$\endgroup\$
    – jimmy23013
    May 4, 2017 at 19:48
  • \$\begingroup\$ @jimmy23013 As far as I can tell, this answer and the generated code are full programs. There is no main or any other mandatory structure like that in R. Besides, the question doesn't explicitly ask for a full program, so a function or similar would do. \$\endgroup\$
    – Steadybox
    May 4, 2017 at 20:11

Alice, 29 bytes


Try it online!

The unprintable character is 0x18.


The trouble with the usual "-based Fungeoid quines is that if we repeat the entire source code, then we also get additional " and the string no longer covers the entire source code. I assume that's why the existing answer uses the cheat-y g approach instead.

This answer does use the "-based approach, but instead of including a " in the source, we write it into the program at runtime. That way, there will only ever be one " regardless of how often the program is repeated (because we only write it to one specific coordinate, independently of program size).

The general idea is then that we create a representation of the whole source code on the stack, but only cycle through the first 29 of the characters (i.e. the program length) with the length of the loop determined by the size of the code. Therefore, we can actually append arbitrary characters (except linefeeds) after @ and the result will always be a cyclic repetition of the core program, one character longer than the source.

4P   Push 4! = 24. This is the code point of the unprintable, which we're 
     using as a placeholder for the quote.
.a+  Duplicate it and add 10, to get 34 = '"'.
80p  Write '"' to cell (8,0), i.e. where the first unprintable is.
    Placeholder, becomes " by the time we get here, and pushes the code
     points of the entire program to the stack. However, since we're already
     a good bit into the program, the order will be messed up: the bottom
     of the stack starts at the 24 (the unprintable) followed by all 
     characters after it (including those from extraneous repetitions). Then 
     on top we have the characters that come in front of the `"`. 
     So if the initial program has structure AB, then any valid program has
     the form ABC (where C is a cyclic repetition of the initial program),
     and the stack ends up holding BCA. We don't care about C, except to
     determine how big the program is. So the first thing we need to do is
     bring B to the top, so that we've got the initial program on top of
     the stack:
a2*  Push 10*2 = 20.
&w   Run the following section 21 times, which is the length of B.

  dt,  Pull up the value at the bottom of the stack.

k    End of loop.
d&w  Run the following section D+1 times, where D is the length of ABC.

  74*  Push 28, one less than the number of characters in AB.
  ,    Pull up the 29th stack element, which is the next character to print.
  .o   Print a copy of that character.

k    End of loop.
@    Terminate the program.
  • \$\begingroup\$ Great solution. I like the explanation. \$\endgroup\$
    – IQuick 143
    Mar 21, 2018 at 14:11

Perl 5, 83 bytes (including final newline)


Try it online!

The good ol' __DATA__ token makes it easy to append an arbitrary string to any Perl program, which the main program can then access via the <DATA> file handle (and actually using __END__, which does the same thing for backwards compatibility, instead of __DATA__ saves two extra bytes).

Note that this program does not read its own source code, but only the extra input data appended to its source after the __END__ token. In effect, the __END__ token and everything after it functions kind of like a string literal terminated by the end of the input.

Also note that, in order to meet the spec exactly, this program must end in a newline. If it does not, the newline actually gets automatically appended after the second __END__ anyway, but then the first iteration output won't be precisely equal to the code plus its first byte any more.


Befunge-98, 30 bytes


Try it online!

My try using Befunge-98 which uses a space terminated quine which also counts how many characters have been outputed. Does however use the g command.

  • \$\begingroup\$ You might want to mention in the first line that it's non-competing/cheating, just to discourage any downvotes it might otherwise receive. \$\endgroup\$
    – quintopia
    May 5, 2017 at 22:38

PHP, 146 bytes


It should be run using -r in command line.

  • \$\begingroup\$ Doesn't seem to work when I Try it online!, it's just an ordinary quine. \$\endgroup\$ May 6, 2017 at 10:55
  • \$\begingroup\$ @ØrjanJohansen You should run it with php -r 'command'. \$\endgroup\$
    – jimmy23013
    May 6, 2017 at 11:01
  • \$\begingroup\$ Gah, cannot get it to work. TIO seems to just ignore the -r arguments. \$\endgroup\$ May 6, 2017 at 11:54
  • \$\begingroup\$ @ØrjanJohansen This is how you should test it. \$\endgroup\$
    – jimmy23013
    Mar 17, 2018 at 15:07
  • \$\begingroup\$ Aha. I must have misunderstood something back then. Now I managed to get it to work with PHP as the language setting, too. \$\endgroup\$ Mar 17, 2018 at 15:20

Runic Enchantments, 61 bytes


Try it online!

Uses a similar approach as the Alice answer: reflectively writes the string " command into the code so that there Is Only One. There rest is a lot of string and stack manipulation to recover the original program, how many extra bytes are needed, and print the necessary hunks.

The sequence rotates the in-memory string representation around so that the 803X4+kw appears at the beginning instead of at the end, due to the position of the " and there isn't an easier way of handling this operation without having to compute a lot of awkward numbers.

While the original program is 61 bytes its string-length is only 50, which is easy to construct as 5X and it was only coincidence that this didn't need to be padded out after containing all necessary functionality (e.g. a length 49 program would be easier to encode as 50 with one byte of padding than to encode a literal 49, while 51 would be encoded as 5X3+ or 53, having to account for its own extra bytes).


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.