30
\$\begingroup\$

Your goal is to create a program that prints itself indefinitely, with a new line after each one. Thus, if your program is a one-liner, it would be repeated on every line of the output.

Example

Program:

A

Output:

A
A
A
...

Rules

  • It must be a complete program, not a snippet or function.
  • The program should loop forever without stack-overflow or recursion limit errors.
  • Output is to stdout or closest alternative.
  • No program input is accepted.
  • Standard loopholes are disallowed, such as opening the program's file or accessing an external resource. Empty programs are disallowed as a standard loophole.
  • If your program code ends with a trailing newline, this does not count as the necessary newline between quines, and you must print another.
  • - Shortest code wins!
\$\endgroup\$
3
  • \$\begingroup\$ The title looks like only one-line programs qualify? \$\endgroup\$ Sep 16, 2015 at 18:40
  • \$\begingroup\$ @PaŭloEbermann Then read the problem description. It's a witty title because it rhymes. Here's an example of a valid multi-line program: codegolf.stackexchange.com/a/57985/34718 . You can't expect to know everything about a challenge by only reading the title. \$\endgroup\$
    – mbomb007
    Sep 16, 2015 at 18:51
  • \$\begingroup\$ @PaŭloEbermann It's just while(1)println(your_code); \$\endgroup\$ Apr 21, 2017 at 23:41

30 Answers 30

19
\$\begingroup\$

Fission, 7 bytes

'!+!NR"

A rather simple modification of the shortest Fission quine I have found so far: I'm simply using the non-destructive ! instead of O and added an N for newline.

So all in all, here is how it works: control flow starts at the R with a right-going atom. " toggles string mode, which means everything until the next " is printed: in this case '!+!NR. That leaves the " and the newline to be printed. '! sets the atom's mass to 33, + increments it to 34 (the character code of ") and ! prints the quote. N prints a newline, and R is now a no-op in this case, so the loop starts over.

The following 7-byte solution also works:

"NR'!+!
\$\endgroup\$
9
\$\begingroup\$

><>, 16 bytes

'ard3*o50l2)?.~~

The traditional ><> quine uses too many os, so we use a loop for printing. Before each jump we push 5 and 0 (the coordinates of where to jump to), after which we either jump with . if there's still something to print, or pops the top two values with ~~.

(Reverted to the 16 version since I forgot about the stack overflow rule.)

\$\endgroup\$
2
  • \$\begingroup\$ An alternative is "ar00go50l2)?.[, right? \$\endgroup\$
    – mbomb007
    Sep 11, 2015 at 16:35
  • 3
    \$\begingroup\$ @mbomb007 I guess, but I prefer d3* since g reads your own source code \$\endgroup\$
    – Sp3000
    Sep 11, 2015 at 16:38
8
\$\begingroup\$

CJam, 13 bytes

{_o"_g
"o1}_g

The online interpreter doesn't print anything before the program terminates, so you'll have to test this in the Java interpreter.

Explanation

Finally a generalised CJam quine which doesn't end in _~.

{_o"_g
"o1}

This simply pushes a block. _g duplicates the block and executes it repeatedly while the top of the stack is truthy (discarding the condition).

Now inside the block, the other copy of the block is still on the stack. We duplicate and print it with _o and then we print _g followed by a newline (the required extra newline between quines) with "_g\n"o. Finally we push a 1 onto the stack for the loop to repeat, because unfortunately, blocks aren't truthy (or falsy) in CJam.

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

Python 2, 39

Not a very interesting task in Python as it's trivial to add the while loop to a normal quine.

c='while 2:print"c=%r;exec c"%c';exec c
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't this blow the stack? \$\endgroup\$ Sep 11, 2015 at 21:42
  • 1
    \$\begingroup\$ @JesanFafon No, if you tried it you would see that it doesn't. There is no recursion. \$\endgroup\$
    – feersum
    Sep 11, 2015 at 21:53
6
\$\begingroup\$

Perl 5.10+, 40 37 bytes

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

or (also 37 bytes)

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

Invoke with the -M5.010 or -E command line flag, e.g.

$ perl -E '$_=q{say"\$_=q{$_};eval"while 1};eval'
$_=q{say"\$_=q{$_};eval"while 1};eval
$_=q{say"\$_=q{$_};eval"while 1};eval
$_=q{say"\$_=q{$_};eval"while 1};eval
...

Thanks to Ilmari Karonen for shaving off 3 bytes from my original solution, which was:

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

This, as well as the shorter 37-byte solutions above, are all simple variations of the following quine, which I first saw in one of Ilmari's other posts:

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

Since all I added in my original solution was a while loop, he really deserves most of the credit. :-)

\$\endgroup\$
5
  • \$\begingroup\$ Nice, and thanks for the credit. :-) The original quine is my own design, although someone else may well have independently discovered it earlier. BTW, $_=q{say"\$_=q{$_};eval"while 1};eval or $_=q{{say"\$_=q{$_};eval";redo}};eval would be a few bytes shorter. ($_=q{say"\$_=q{$_};eval";eval};eval would be even shorter yet, but I suspect it will eventually run out of stack.) \$\endgroup\$ Sep 15, 2015 at 15:07
  • \$\begingroup\$ I was hoping you would show up! Thank you for the shorter solutions, I figured you would come up with something better; but I was so blown away by this when I saw it in the answer I linked to, I just had to try it myself. :) To be honest, I still haven't quite wrapped my head around quines, but the additional answers give me more to look at. I wasn't sure if you originated this because I saw you credited with the shortest non-cheating Perl quine ever(?!) on this page, but there's no attribution for this quine (using print instead of say). \$\endgroup\$ Sep 15, 2015 at 16:03
  • \$\begingroup\$ btw, the last one does indeed segfault after running for a while. Thanks again, @Ilmari! \$\endgroup\$ Sep 15, 2015 at 16:03
  • \$\begingroup\$ Actually, I take that somewhat back -- I'm definitely not the first to invent that quine, and since I'd definitely seen it before (I posted the one I do claim some credit for in the same thread), it's quite likely that I subconsciously remembered it from that thread, or from somewhere else. \$\endgroup\$ Sep 15, 2015 at 16:16
  • \$\begingroup\$ Alright, I've left the origin unspecified. Regardless, thank you for the 3 bytes and thank you for the original inspiration. \$\endgroup\$ Sep 15, 2015 at 16:39
5
\$\begingroup\$

Self-modifying Brainf*** (SMBF), 14 bytes

The trailing newline \n needs to be a literal, Unix, newline (ASCII code 10).

[<[<]>[.>]<.]\n

Explanation:

The code moves the pointer to the far left of its source code, then prints it all, including the newline (twice b/c of the rule). The loop continues.

\$\endgroup\$
6
  • \$\begingroup\$ You mean, it has to be \r? \$\endgroup\$ Sep 11, 2015 at 17:27
  • \$\begingroup\$ @IsmaelMiguel No. \n is a new line, and is ASCII code 10, so a hex-dump of the source code would have the value 0A for that byte. code.cside.com/3rdpage/us/newLine.html \$\endgroup\$
    – mbomb007
    Sep 11, 2015 at 18:19
  • 1
    \$\begingroup\$ @IsmaelMiguel Not true. When you type code, you actually hit the enter key for a newline. \n generally counts as 2 characters, and 2 bytes in your code. I typed it this way for readability, since the formatting doesn't work well for showing a blank line at the end of my source code. And so I had to specify that it was ONE byte, not two. \$\endgroup\$
    – mbomb007
    Sep 11, 2015 at 19:46
  • 2
    \$\begingroup\$ \n is always a single character, called a linefeed. A newline, however, is a platform-dependent sequence of bytes. \$\endgroup\$
    – Dennis
    Sep 11, 2015 at 22:32
  • 3
    \$\begingroup\$ @Dennis Maybe so, but I don't care. I don't call / a "Solidus", and I don't know anybody who does. "Newline" starts with an n, and that's the escape character, so that's what I call it. It's closer to "line feed" than it is to "carriage return". \$\endgroup\$
    – mbomb007
    Sep 12, 2015 at 1:02
3
\$\begingroup\$

PowerShell, 143 Bytes

$d='$d={0}{1}{0}{2}while(1){3}Write($d -f [char]39,$d,"`n",[char]123,[char]125){4}'
while(1){Write($d -f [char]39,$d,"`n",[char]123,[char]125)}

Based off the Rosetta Code PowerShell quine, I'm pretty confident this isn't the shortest possible. String replacement formatting in PowerShell is icky for this, because the same delimiters for where to put the replacements {} also delimit code blocks while{}, so we have to use [char] casting, which bloats the code a bunch.

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

Underload, 25 bytes

First time I've tried something like this and I'm not sure if follows all the rules as it is a couple of lines. The newline was a bit of a pain.

(::a~*S:a~*^
)::a~*S:a~*^
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @mbomb007 The output per iteration is the two lines of the program. I tested it here \$\endgroup\$
    – MickyT
    Sep 12, 2015 at 3:42
3
\$\begingroup\$

Befunge, 30 20 bytes

<,+55,*2+98_ #!,#:<"

A variation of a popular befunge quine that prints out a newline and pops -1 on the stack if it finishes the line.
Unfortunately, Befunge gets verbose when doing things in one line. I tried to remove all of the launchpads (#) that i could, but some had to be left in to skip certain commands (like ,).

Changes:

30-20 -> changed the base quine to a custom one I made that uses string input. This way, the branching is a lot easier.

Old:

:0g,:93*`>0-10 #,+$#: #5 _1+>

I don't think this is optimal, but it's acceptable for now.

\$\endgroup\$
2
  • \$\begingroup\$ You can use a get instruction to grab a space, and add 2 to it for one byte shorter than 98+2/: <,+55,+2g1_ #!,#:<" \$\endgroup\$ Apr 17, 2018 at 21:36
  • \$\begingroup\$ And push the quote and newline first and reverse the printer, -1g0:+5<>:#,_1" \$\endgroup\$
    – Jo King
    Apr 17, 2018 at 22:22
2
\$\begingroup\$

K, 30 bytes

{f:$_f;{x}{`0:f,"[]\n";1}/1}[]
\$\endgroup\$
2
\$\begingroup\$

R, 34 bytes

repeat{write(commandArgs()[5],'')}

to invoke from the command line as follows:

Rscript -e "repeat{write(commandArgs()[5],'')}"
\$\endgroup\$
2
\$\begingroup\$

><>, 31 29 bytes

A simple modification of the traditional ><> quine.

"ar00gc0.0+efooooooooooooooo|

To run, paste it here, click 'Submit', then 'Start' (running without animation doesn't work). Feel free to increase the execution speed.

\$\endgroup\$
4
  • \$\begingroup\$ "ar00gc0.0+feooooooooooooooo| is two bytes shorter. \$\endgroup\$
    – cole
    Sep 12, 2015 at 3:56
  • \$\begingroup\$ @Cole Thanks. I had thought about the mirror, but forgot to change it after getting side-tracked on a different challenge. \$\endgroup\$
    – mbomb007
    Sep 12, 2015 at 19:38
  • \$\begingroup\$ 20 bytes \$\endgroup\$
    – Jo King
    Apr 15, 2019 at 6:58
  • \$\begingroup\$ @JoKing There's already a shorter ><> solution. \$\endgroup\$
    – mbomb007
    Apr 15, 2019 at 14:30
2
\$\begingroup\$

Python 2, 48 bytes

c='c=\'%s\';while 1:print c%c';while 1:print c%c
\$\endgroup\$
2
\$\begingroup\$

GolfScript, 16 bytes

{.".do
":n\p}.do

This ended up looking a lot like Martin Büttner's CJam entry. An interesting feature is that, as it turns out, the shortest way to append ".do" to the block when it's printed is to assign it to the line terminator n. (Of course, we also need to include an actual newline in the string, to replace the one n normally contains.) The same string (being truthy in GolfScript) is also left on the stack for the do loop to pop off, ensuring that the loop runs forever.

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

Brachylog, 16 bytes

∋"∋~kgjẉ₁⊥"gjẉ₁⊥

You can't quite try it online, but I have verified that it works on my installation.

Brachylog, 18 bytes

∋"∋~kgjw₁⊥~n"gjw₁⊥

Try it online!

Adapted from the alternate version of this quine, with a backtracking hack I had initially devised for bogosort of all things. Spends two bytes on ~n because (the usual means for printing with a trailing newline) hits some odd issues with character encoding on TIO, and an actual newline inserted into the string literal gets printed through ~k as \n.

               w      Print
 "∋~kgjw₁⊥~n"         "∋~kgjw₁⊥~n"
∋                     which is an element of
                      the (unused and unconstrained) input
                ₁     formatted
          ~n          (so that the ~n is replaced with a newline)
             gj       with itself
   ~k                 (so that the ~k is replaced with the string in quotes),
                 ⊥    then try again.

Since w₁ takes input as a list [string to be formatted, formatting arguments], wrapping a string in a list with g then concatenating it to itself with j allows it to be formatted with itself. And since no input is given to the program, the input variable which is implicitly present at the beginning of the program can take on any value, so it can be constrained to one of the infinitely many lists which contain "∋~kgjw₁⊥~n" as an element, creating a single choice point to be backtracked to when after printing its source the program hits .

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

Gol><>, 10 bytes

"r2sslRoao

Try it online!

This can probably be golfed down more

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

Python 3.6, 48 43 bytes.

-5 bytes thanks to @Jo King

x='while 1:print("x=%r;exec(x)"%x)';exec(x)
\$\endgroup\$
2
  • \$\begingroup\$ You can use %r to save on escaping those 's. 43 bytes \$\endgroup\$
    – Jo King
    Apr 15, 2019 at 9:50
  • \$\begingroup\$ which gives 41 for python 3.8 :P \$\endgroup\$
    – ASCII-only
    Apr 16, 2019 at 12:48
1
\$\begingroup\$

BASH, 76 bytes

Just couldn't resist, especially with PowerShell here :)

while true;do
a ()
{
    echo while true\;do
    declare -f a
    echo a\;done
}
a;done

The whitespace is important for an EXACT copy.

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

Javascript (ES6), 64

for(;;(_=>console.log(`for(;;(_=>${arguments.callee})());`))());

Alternatively (also 64)

a="for(;;)console.log(`a=${JSON.stringify(a)};eval(a)`)";eval(a)
\$\endgroup\$
5
  • \$\begingroup\$ Wouldn't the alternative cause a stack overflow? \$\endgroup\$ Sep 13, 2015 at 13:23
  • \$\begingroup\$ No. There is no recursion so nothing should be adding up on the stack. \$\endgroup\$
    – DankMemes
    Sep 13, 2015 at 15:27
  • \$\begingroup\$ Okay, well I just tested both of them, you are correct about the alternative solution, but your first solution fails and I figured out why. According the MDN documentation on arrow functions, "Arrow functions do not expose an arguments object to their code" so your arguments.callee does not exist and throws an error. \$\endgroup\$ Sep 13, 2015 at 17:47
  • \$\begingroup\$ Interesting. For me in Firefox Developer Edition 42.0a2 (2015-09-13), both work. I guess once the standard is fully adopted the first one will stop working. \$\endgroup\$
    – DankMemes
    Sep 13, 2015 at 17:52
  • \$\begingroup\$ I tested them on latest chrome since I was trying to golf your first solution with ! instead of enclosing parentheses and putting the function execution after the for loop to avoid the need for the for loop semicolon body which would save (I think) 4 bytes \$\endgroup\$ Sep 13, 2015 at 17:54
1
\$\begingroup\$

Microscript, 22 bytes

"f1{CqxCanx"f1{CqxCanx

Based on the quine from the Esolangs article: "fCqxah"fCqxah. Exploits the fact that the language autoappends closing braces as needed, without which this would be two bytes longer.

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

Batch, 10 (+ 4 for filename length)

Not sure if this qualifies for two reasons:

  • Technically, there may or may not be textual side effects from the Windows command shell, as that depends on how it is configured.
  • This program invokes itself by name, and I am not sure whether that's prohibited by the rules (specifically the "no opening of program file" rule). It is not opening itself for the purposes of reading and printing out the text; it is simply re-running itself. Further, the file system structure is an integral part of old-school batch scripts (oftentimes even being used to store program state, etc). As such, I am not sure whether this violates the 5th rule or not.

The code (for a program named q.bat):

echo on&&q
\$\endgroup\$
9
  • 2
    \$\begingroup\$ I get I'll allow it since it's not the shortest, and it's creative and unique. And it wasn't reading the source code, but rather executing itself. \$\endgroup\$
    – mbomb007
    Sep 12, 2015 at 3:37
  • 1
    \$\begingroup\$ Since the file name is not arbitrary, at the very least you should add the file name to the byte count. \$\endgroup\$ Sep 12, 2015 at 5:40
  • 2
    \$\begingroup\$ I believe you could use echo on&&%0 although I'm afraid to try it. \$\endgroup\$
    – DankMemes
    Sep 12, 2015 at 19:12
  • 1
    \$\begingroup\$ Update: echo on&&%0 does not work. %0 is displayed as expanded in the output, and windows (7, 32 bit, running in a VM) terminates the entire thing pretty quickly. \$\endgroup\$
    – DankMemes
    Sep 12, 2015 at 19:50
  • 1
    \$\begingroup\$ I think you can omit the .bat part \$\endgroup\$ Sep 15, 2015 at 19:01
1
\$\begingroup\$

Ceylon, 210 208 bytes

Of course this won't win anything...

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

Original:

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(true){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(true){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

I modified my Quine from two days ago by adding the while(true){...} loop, so I come from the 185 bytes of the plain Quine to 210 (I don't need the trailing new line character anymore). But then I found that a while(1<2){...} loop is even two bytes shorter:

shared void u(){value q="\"\"\"";value t="""shared void u(){value q="\"\"\"";value t=""";value b="""while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}""";while(1<2){print(t+q+t+q+";value b="+q+b+q+";"+b);}}

(Ceylon has no for(;;) loop like Java, and the braces are also needed for this loop.)

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

PowerShell, 132 107 Bytes

$a='$a={0}{1}{0};for(){2}$a-f[char]39,$a,[char]123,[char]125{3}';for(){$a-f[char]39,$a,[char]123,[char]125}

Based off of the Rosetta Quine (Same as @AdmBorkBork) although doesn't use formatting for string replacement... maybe switching to a for loop and using formatting would be best?

I'm sure if AdmBorkBork came back they would beat this by a lot :P

EDIT Figured out the for loop and replacements, all thanks to my predecessor :)

Old attempt:

$a='$a=;for(){$a.substring(0,3)+[char]39+$a+[char]39+$a.substring(3)}';for(){$a.substring(0,3)+[char]39+$a+[char]39+$a.substring(3)}
\$\endgroup\$
1
\$\begingroup\$

Ruby, 41 39 bytes

A modification of one of the Ruby quines found here.

_="_=%p;loop{puts _%%_}";loop{puts _%_}
\$\endgroup\$
1
\$\begingroup\$

Java 10, 194 bytes

interface M{static void main(String[]a){for(var s="interface M{static void main(String[]a){for(var s=%c%s%1$c;;)System.out.println(s.format(s,34,s));}}";;)System.out.println(s.format(s,34,s));}}

Explanation:

Try it online (times out after 60 sec).

interface M{                    // Class
  static void main(String[]a){  //  Mandatory main-method
    for(var s="interface M{static void main(String[]a){for(var s=%c%s%1$c;;)System.out.println(s.format(s,34,s));}}";
                                //   Unformatted source code
        ;)                      //   Loop indefinitely
       System.out.println(      //    Print with trailing new-line:
         s.format(s,34,s));}}   //     The formatted source code

-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:

  • for(;;) is used to loop indefinitely.
  • System.out.println(...) is used to print with trailing new-line
\$\endgroup\$
1
\$\begingroup\$

Funky, 29 bytes

f=@print`f=[f]for)f()`for)f()

Try it online!

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

RProgN 2, 7 bytes

«]³]]p:

Try it online!

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

Jelly, 14 bytes

“;@ṾṄṛ¢”;@ṾṄṛ¢

Try it online!

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

Pyth, 11 bytes

#jN B"#jN B

Try it here!

\$\endgroup\$
2
  • \$\begingroup\$ It works well enough. Just pause execution shortly after running. \$\endgroup\$
    – mbomb007
    Apr 17, 2018 at 2:05
  • \$\begingroup\$ Huh, nice! I'll edit that link in. \$\endgroup\$
    – RK.
    Apr 17, 2018 at 13:59
1
\$\begingroup\$

Keg, 16 bytes

`{④⑩9⑨,}`{④⑩9⑨,}

Try it online!

Ties with most other golfing languages, so I'm happy with that. Pushes the string {④⑩9⑨,}, which is then infinitely printed rawly, then nicely followed by a newline infinitely.

\$\endgroup\$

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.