44
\$\begingroup\$

Write a program (or function) (let's call it P1), that when run, outputs another program P2 of the same language and exactly 1 byte longer than P1.

Program P2 when run, should output a 3rd program P3 that's 1 byte longer than P2. P3 must output a program P4 that's one byte longer than P3, etc. The same for P5, P6, ..., P∞.

The program chain should go indefinitely, or to a place where the interpreter can't handle anymore (but must remain as a theoretically valid program in the language)

Rules

  • Standard loopholes forbidden
  • All programs in the chain should be in one language
  • No input is given. Output goes to stdout or function return value
  • The program must end after a period of time. A program that stops generating output after a certain time spot but never terminates does not qualify

The shortest program P1 in bytes in each language wins!

\$\endgroup\$
9
  • 2
    \$\begingroup\$ @Οurous What??? I did not add that tag myself... \$\endgroup\$
    – iBug
    Commented Mar 17, 2018 at 6:45
  • 6
    \$\begingroup\$ @iBug Regardless, are submissions allowed to read their own source code? \$\endgroup\$ Commented Mar 17, 2018 at 6:45
  • 3
    \$\begingroup\$ @iBug The "quine" tag forbids it by default, and it usually makes answers more interesting to do so. It's up to you though. \$\endgroup\$ Commented Mar 17, 2018 at 8:15
  • 1
    \$\begingroup\$ "puts <<2*2,2\nputs <<2*2,2\n\n2" grows by 2 at each iteration in Ruby. I couldn't find anything better. :-/. Interesting challenge! \$\endgroup\$ Commented Mar 17, 2018 at 20:17
  • 1
    \$\begingroup\$ Possible duplicate of Program that creates larger versions of itself (quine-variant) \$\endgroup\$
    – kepe
    Commented Dec 5, 2018 at 15:52

29 Answers 29

30
\$\begingroup\$

JavaScript (ES6), 14 12 bytes

-2 bytes thanks to @Shaggy

f=_=>"f=_"+f

Testing snippet

s=`f=_=>"f=_"+f`;

for(i=10;i--;s=eval(s)())
  o.innerText+=s+"\n";
<pre id=o></pre>

\$\endgroup\$
3
  • \$\begingroup\$ Took me a second to spot that. Sneaky! \$\endgroup\$
    – Shaggy
    Commented Mar 17, 2018 at 8:40
  • 4
    \$\begingroup\$ Can someone please explain, I cannot wrap my head around it, how does it increase? \$\endgroup\$ Commented Mar 17, 2018 at 11:29
  • 2
    \$\begingroup\$ @htmlcoderexe "f=_" prepends an extra _ before the parameter name, which causes it to increase in length every iteration. \$\endgroup\$
    – Endenite
    Commented Mar 17, 2018 at 11:34
9
\$\begingroup\$

7, 4 bytes of ASCII

1603

Try it online!

I know that 7 isn't normally encoded in ASCII, but this time it's a more convenient encoding so that we're adding 1 byte with each run, not 3 bits.

I'm also not sure whether this counts as cheating or not. (It's usual to be unclear whether or not a 7 quine is cheating, as it straddles the borderline in a number of ways.) You can make a decent argument that the 0 encodes the 6, but in general it's unclear where the resulting characters "come from" in 7 because it has so many, fairly bizarre, implicit behaviours.

This program prints itself with 1 appended, and will do so even if you append a number of 1s to it. Here's a commented debug trace of 160311:

|| 160311      Initial data ||; initial program 160311
||7 60311      1 command = append 7 to data
|1 0311        6 command = escape from the last | onwards (7 escapes to 1)
|16e77         0311 commands = append 6e77 to data
|16e77 16e77   Implicit (program is empty): copy data past last | to program
|16e777 6e77   1 command = append 7 to data
71603111 e77   6 command = escape from the last | onwards
71603111 e77   e7 command = output in same encoding as the source

(There are no | left in the program, so e will immediately quit the program as a side effect, meaning that the final 7s never run).

The basic confusion about where all the characters are coming from is that most commands in 7 just produce data when run, and then 6 attempts to reconstruct a sequence of commands that would produce the given fragment of data; this often ends up close to, but not identical to, the original. (For quining purposes, you normally write a 7 program in such a way that the result will be almost the same, normally differing in leading or trailing 7s.) So for example, the 1 in the data becomes 716, which is the easiest way to append 1 to the current data string. We originally produced it with 16, a different (but similar) character sequence, destructively removing one of the | markers the data started with. (I guess perhaps the best argument that this isn't a literal-only quine is that the output is different from the input!)

\$\endgroup\$
9
\$\begingroup\$

Haskell, 74 66 bytes

EDIT:

  • -2 bytes by H.PWiz by using <>, then -6 by moving the (10*)<$>.

This now uses the newly free <> operator (Semigroup multiplication, requires GHC 8.4 to work without an import.)

main=putStr$fst<>show$(10*)<$>("main=putStr$fst<>show$(10*)<$>",1)

Try it online! (Cheats with an import since TIO doesn't have GHC 8.4 yet.)

How it works

  • main=putStr$ is boilerplate to output the following string value.
  • fst<>show is a function that takes a tuple, and returns a string consisting of the first element of the tuple concatenated with the tuple's string representation. I.e.

    (fst<>show)(s,t) = fst(s,t)<>show(s,t) = s++show(s,t)
    
  • (10*)<$> multiplies the last element of the following tuple by 10, adding a digit 0 to its string representation.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save at least 2 bytes with (<>) \$\endgroup\$
    – H.PWiz
    Commented Apr 2, 2018 at 19:31
  • \$\begingroup\$ @H.PWiz Thanks, got some more by moving (10*)<$>. \$\endgroup\$ Commented Apr 3, 2018 at 0:50
8
\$\begingroup\$

C (gcc), 134 132 bytes

Slight reworking of the canonical C quine. Dreadfully long.

x;*s="x;*s=%c%s%c;main(i){for(i=__LINE__;i--;puts(&x));printf(s,34,s,34);}";main(i){for(i=__LINE__;i--;puts(&x));printf(s,34,s,34);}

Try it online!

\$\endgroup\$
7
\$\begingroup\$

Fission 2, 7 bytes

'!+OR!"

Try it online!

The standard Fission quine, but with an extra ! that prints an additional 0x01 at the beginning of the code on each iteration.

\$\endgroup\$
7
\$\begingroup\$

Pari/GP, 35 bytes

(f=(x)->print1("(f="f")("1x")"))(1)

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python 2, 38 bytes

s="print's=%r;exec s'%(s+'#')";exec s

Try it online!

\$\endgroup\$
5
\$\begingroup\$

brainfuck, 420 bytes

->+++>>+++>+>++>+++>+>+>+++>>>>>>>>>>>>>>>>>>>>+>+>++>+++>++>>+++>+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+>>+++>>+++>>>>>+++>+>>>>>>>>>++>+++>+++>+>>+++>+++>+>++>>+++>+>+++>+>++>+++>>>+>+>++>+++>+>+>>+++>>>>>>>+>+>>>+>+>++>+++>+++>+>>+++>>>+++>+>++>+++>++>>+>+>++>+++>+>+>>+++>>>>>+++>+>>>>>++>+++>+++>+>>+++>>>+++>+>+++>+>>+++>>+++>>++[[>>+[>]++>++[<]<-]>+[>]<+<+++[<]<+]>>->[>]++++>++[[<++++++++++++++++>-]<+++++++++.<]>.

Try it online!

This is a modification on the "standard" BrainFuck quine, with a . at the end that tacks on an extra . each iteration.

The quine itself encodes Brainfuck characters as a stack of hex digits: specifically, the hex digits of c-0x2b, which conveniently are the following:

+: 0x00
-: 0x02
[: 0x30
]: 0x32
<: 0x11
>: 0x13
,: 0x01
.: 0x03

The encoding covers two snippets of code: >++[[>>+[>]++>++[<]<-]>+[>]<+<+++[<]<+]>>->[>]++++>++ pushes the encoding of the encoding itself, and [[<++++++++++++++++>-]<+++++++++.<] walks the stack and prints everything.

\$\endgroup\$
3
\$\begingroup\$

Dirty, 9 bytes

'"n[!]a!␛

Try it online!

'   start and end a string literal
"   push a literal '
n   remove newlines
[!] print the string
a   push the alphabet
!   print the first character
␛   end the program

If source-code reading is allowed:

Dirty, 8 bytes

Q[!]W33!

Try it online!

Explained:

Q   push the source code
[!] print each character
W   clear the now-empty stack
33! print an exclaimation mark

Might be valid:

Dirty, 4 bytes

Q[‼]

Try it online!

It prints the source code with a trailing newline.
(And a bunch of spaces, due to a bug. It works the same without them though.)

Note that it only works in the native character set, and not when you use the UTF8 front-end - so to try it on TIO you need to replace the character it outputs between the []s with , which is the UTF8 equivalent for what it's printing.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The 4-byte version is definitely invalid. \$\endgroup\$ Commented Mar 17, 2018 at 11:30
3
\$\begingroup\$

GolfScript, 9 bytes

{'.~1'}.~

Try it online!

CJam, 9 bytes

{"_~1"}_~

Try it online!

I'm posting both of these solutions in the same answer, since they're just trivial variations of each other and work in exactly the same way. They're both based on the common GolfScript quine {'.~'}.~ (or {"_~"}_~ in CJam), which is described in more detail e.g. in this previous answer of mine.

The only difference is that this variant appends a 1 byte to the end of its output. As it happens, any string of 1s (or any other integer literal without leading zeros) is itself a trivial quine in both GolfScript and CJam, so any ones already present at the end of the code above will simply be copied verbatim to the output. Since GolfScript (and CJam) use arbitrary-length integers, this will work for arbitrarily long programs, at least as long as the computer running the code has enough memory to store it.

\$\endgroup\$
3
\$\begingroup\$

Java 8, 162 146 bytes

v->{String s="v->{String s=%c%s%1$c+1;return s.format(s,34,s).replaceAll(%1$c1+$%1$c,%1$c%1$c);}"+1;return s.format(s,34,s).replaceAll("1+$","");}

Try it online.
Try the first output program; Try the second output program; Try the third output program.

Explanation:

v->{                       // Method with empty unused parameter and String return-type
  String s="v->{String s=%c%s%1$c+1;return s.format(s,34,s).replaceAll(%1$c1+$%1$c,%1$c%1$c);}"
                           //  The unformatted source code
           +1;             //  Plus a random digit (1 in this case)
  return s.format(s,34,s)  //  Create the quine
          .replaceAll("1+$","");}
                           //  Then remove any trailing 1s

-part:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

Challenge part:

  • +1 adds a 1 to both the unformatted and formatted program.
  • .replaceAll("1+$","");}: Because we only want to increase the program byte-count by one instead of two, we remove any trailing 1s before returning.
\$\endgroup\$
3
\$\begingroup\$

Jstx, 4 bytes

£↕♣:

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

><>, 9 bytes

#o<:}-1:"

Try it online!

Variation on a classic ><> quine, which simply adds another dupe command to copy the # in the front.

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 32 bytes

$_=q{say"\$_=q{$_
};eval"};eval

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Attache, 76 72 61 bytes

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]",Repr[x+sp]]

Try it online!

Standard quine which adds a space to the end of x after each iteration.

First few iterations:

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]",Repr[x+sp]]

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]] ",Repr[x+sp]]

Print!Format[x:="Print!Format[x:=%s,Repr[x+sp]]  ",Repr[x+sp]]

etc.

Attache, 72 bytes

y:=1Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

Try it online!

This is simply a variation of the standard format quine, with a variable y that is set to 10*y after each iteration

The first few iterations:

y:=1Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

y:=10Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

y:=100Print!Format[x:="y:=%sPrint!Format[x:=%s,y*10,Repr!x]",y*10,Repr!x]

etc.

\$\endgroup\$
2
\$\begingroup\$

Runic Enchantments, 6 bytes

"'<S@>

Try it online!

This one was weird. All I had to do was remove a ~ from the original quine found by Jo King.

Every additional run appends another < to the end, e.g.:

"'<S@><<<<<<<<<

All of which do nothing.

Direct copy of this answer on a related challenge. It just so happened to already grow by 1 byte every iteration (strong argument for this challenge being a duplicate of that one or vice versa).

\$\endgroup\$
1
\$\begingroup\$

Swift 4, 89 bytes

let(a,b)=(";print(\"let(a,b)=\\((a,b+\"-\"))\"+a)", "");print("let(a,b)=\((a,b+"-"))"+a)

Includes a trailing newline

Try it online!

\$\endgroup\$
0
1
\$\begingroup\$

Haskell, 88 bytes

main=putStr$snd(span(<'m')s)++show s;s='#':"main=putStr$snd(span(<'m')s)++show s;s='#':"

Try it online! Grows by prepending # to the data string.

\$\endgroup\$
3
  • \$\begingroup\$ You can save a bit by showing more than just a string and using pattern matching. Try it online! \$\endgroup\$ Commented Mar 17, 2018 at 15:59
  • \$\begingroup\$ @ØrjanJohansen Nice! Except for the underlying standard Haskell quine this is a completely different approach, so feel free to post it yourself. \$\endgroup\$
    – Laikoni
    Commented Mar 17, 2018 at 16:05
  • \$\begingroup\$ OK, if you think so. \$\endgroup\$ Commented Mar 17, 2018 at 16:20
1
\$\begingroup\$

Wonder, 33 bytes

f\ @(-> ol) ["f\ ";f;";f1";#0];f1

An interesting variant on the normal quine that appends a 1 after each iteration.

Progression:

f\ @(-> ol) ["f\ ";f;";f1";#0];f1
f\ @(-> ol) ["f\ ";f;";f1";#0];f11
f\ @(-> ol) ["f\ ";f;";f1";#0];f111
...

Explanation

f\ @                               #. Sets f to a function that does the following:
    (-> ol) [                      #.   Output each:
             "f\ ";                #.     String for declaration of f
                   f;              #.     Formatted representation of f's function
                     ";f1";        #.     String for call of f
                           #0      #.     Argument passed into f
                             ];f1  #. Call f with 1 as the argument

One of the interesting parts of this quine is that Wonder can work with numbers of arbitrary precision, so the progression won't break after a certain amount of ones.

\$\endgroup\$
1
\$\begingroup\$

Stax, 20 18 bytes

"34s+cTZL"34s+cTZL

Run and debug it

Generates an extra space before the 2nd quotation mark every iteration.

Explanation

Uses the program "34s+cTZL "34s+cTZL to explain.

"34s+cTZL "34s+cTZL
"34s+cTZL "            String literal
           34s+        Prepend a double quote, Now the string is `"34s+cTZL `
               cT      Copy and trim trailing spaces
                 Z     Put a 0 under the top of stack
                       Stack now (from top to bottom): `["34s+cTZL,0,"34s+cTZL ]`
                  L    Collect all elements on stack, from bottom to top
                       Implicit output, 0 is converted to space.
\$\endgroup\$
1
\$\begingroup\$

Gol><>, 7 bytes

":r2ssH

Try it online! Try it online!! Try it online!!!

This is a simple variation of the regular Gol><> quine but adds a copy of H each time it's run. Since the H command halts the program, additional H at the end does not change the behavior.

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 89 bytes

var s="var s={0}{1}{0}+8;Write(s.Trim('8'),(char)34,s);"+8;Write(s.Trim('8'),(char)34,s);

Inspired by Kevin Cruijssen's answer.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 11 bytes

“⁾;⁶;ØV” ṘV

Try it online!

\$\endgroup\$
0
\$\begingroup\$

ColdFusion, 277 bytes

<cfset u=Chr(34)><cfset q="<cfset u=Chr(34)><cfset q=%s%s%s><cfoutput>%screateObject(%sjava%s,%sjava.lang.String%s).format(q,[u,q,u,Chr(35),u,u,u,u,Chr(35)])%s</cfoutput>
"><cfoutput>#createObject("java","java.lang.String").format(q,[u,q,u,Chr(35),u,u,u,u,Chr(35)])#</cfoutput>

This is a trivial modification of my ColdFusion quine which adds a newline each extra time it's called.

Tested locally on lucee-express-5.2.6.60

\$\endgroup\$
0
\$\begingroup\$

Windows Batch, 38 36 bytes

echo|set/p"=q">q&copy/b/y %0+q %0
::

This code creates a file called "q", containing the letter 'q', and then appends it to the original file. Note that "::" is an alias for "rem" that does not require an additional space.

Saved 2 bytes thanks to user3493001.

\$\endgroup\$
0
0
\$\begingroup\$

T-SQL, 175 bytes

DECLARE @ VARCHAR(MAX)='DECLARE @ VARCHAR(MAX)=*SET @=TRIM(REPLACE(@,0x2a,CHAR(39)+@+CHAR(32)+CHAR(39)))PRINT @'SET @=TRIM(REPLACE(@,0x2a,CHAR(39)+@+CHAR(32)+CHAR(39)))PRINT @

First I wrote an SQL quine, then I modified it to add an extra space (somewhat inspired by this answer).

\$\endgroup\$
0
\$\begingroup\$

Befunge-98 (PyFunge), 22 19 bytes

r:#,_ #;'D2/'<,,@;"

Try it online!

Adds a < before the last " every generation.

\$\endgroup\$
0
\$\begingroup\$

Jstx, 4 bytes

£↕☺V

Try it online!

Explanation

Verbose code:

meta.source    Push the source
load 1         Push 1
string.concat  Concatenate source with 1
               Implicit output
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 71 bytes

#define a(b)*s=#b;b
a(main(){printf("#define a(b)*s=#b;b\na(%s;)",s);})

Every iteration adds a semicolon after the closing brace of the main function.

Try it online!

\$\endgroup\$
0

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.