145
\$\begingroup\$

Write the shortest code you can that produces an infinite output.

That's all. You code will only be disqualified if it stops producing output at some point. As always in code golf, the shortest code wins.

Here's a list of answers that I think are really clever, so they can get credit:

Leaderboard

var QUESTION_ID=13152,OVERRIDE_USER=8611;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
16
  • 141
    \$\begingroup\$ All answers disqualified because at some point the Earth will be swallowed by the sun, and at some point the universe will die :P \$\endgroup\$
    – Doorknob
    Commented Nov 9, 2013 at 20:00
  • 27
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$
    – Izkata
    Commented Nov 10, 2013 at 1:39
  • 7
    \$\begingroup\$ If I write mine in Piet, can I count the pixels of the text the other programs used? I believe the smallest possible repeating Piet program would be 6 pixels. That beats Befunge if "off" pixels still count. \$\endgroup\$
    – DampeS8N
    Commented Nov 12, 2013 at 20:27
  • 12
    \$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ Commented Jul 11, 2014 at 20:11
  • 11
    \$\begingroup\$ @Doorknob So really, the challenge is to produce infinite output in a finite amount of time. Sounds easy enough. \$\endgroup\$
    – Sanchises
    Commented Apr 10, 2015 at 21:15

334 Answers 334

1 2
3
4 5
12
3
\$\begingroup\$

GNU sed, 5 bytes

:;l;b

Any sed script requires input to start executing. With echo|sed ':;l;b', a line with a single $ character on it is printed continuously.

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

Java (JDK 10), 32 bytes

a->{for(;;)System.out.print(1);}

Try it online!

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

Japt, 4 bytes

i@Oo

Try it online!

How it works

Ui@Oo

Ui     Call every 10 milliseconds...
  @Oo    the function that prints `undefined`.

Without input, U is initialized to zero, and 0 .i is the wrapper for setInterval JS function. According to its spec, the interval is low-capped at 10, so the target function is called every 10ms (approximately).

Oo prints its arguments to output, and with no arguments, it prints undefined.


The following is only correct with unbounded call stack (and therefore not a valid submission). When run on an actual browser, it overflows the stack in an instant.

3 bytes

ßOo

Try it online!

ß recursively calls the program. The whole source is translated to rp(O.o()) which first calls O.o() and then calls the program again.

\$\endgroup\$
5
  • \$\begingroup\$ Your second solution doesn't qualify. .a() doesn't create an endless loop, it simply runs for all integers up to 1e8. See the language source code. \$\endgroup\$
    – Etheryte
    Commented May 17, 2018 at 8:05
  • \$\begingroup\$ The 4 byte answer doesn't qualify either, it will reach the stack limits very quickly, according to this SO question in under 50k iterations on most browsers. \$\endgroup\$
    – Etheryte
    Commented May 17, 2018 at 9:58
  • \$\begingroup\$ The new 9 byte solution doesn't qualify either, once the array reaches the maximum size allowed by the spec, the runtime will throw and the execution will stop. In V8, for example, the error is Uncaught RangeError: Invalid array length. \$\endgroup\$
    – Etheryte
    Commented May 17, 2018 at 10:09
  • \$\begingroup\$ @Nit Do you mean any code that uses ß for infinite execution is invalid, including this and this? Or is it fine under "unbounded call stack" assumption? For the array limit, ECMA spec says "If len + argCount > 2**53‑1, throw a TypeError exception" for arr.push, so I agree that the 9-byte one isn't strictly correct. But then it only leaves the boring $while(1)$ though... \$\endgroup\$
    – Bubbler
    Commented May 17, 2018 at 23:59
  • \$\begingroup\$ Where did you find the "unbounded call stack assumption"? I don't see it anywhere in the question. Nice work on that 4 byte solution, by the way. \$\endgroup\$
    – Etheryte
    Commented May 18, 2018 at 7:17
3
\$\begingroup\$

TeX, 14 12 bytes

\def~{x

~}~

Less infinite than the other solutions, though, as TeX writes its output to a file.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ A blank line inside the definition would still make a \par token with 2 bytes less :-) \$\endgroup\$ Commented May 14, 2019 at 17:55
3
\$\begingroup\$

TeX, 11 bytes

The following results in an infinite amount of Undefined control sequence. errors (if run in batch mode no human interaction needed):

\def~{\[~}~

TeX, 14 bytes

The following creates a PDF with infinite lines each containing a dot (theoretically that is, since the process never terminates, the file isn't finalized so not really created). I've stopped the process after more than 1000000 pages were output. At some point memory wouldn't suffice anymore for this.

\def~{.\par~}~
\$\endgroup\$
3
\$\begingroup\$

Intercept 1.1, 37 bytes

Almost exact copy of my answer to a different question.

malware bm bitminer
software install 1

Prints newlines to the terminal. You'll probably have to leave this one overnight, as there is a delay of 5-15 minutes between each newline.

Will print infinitely as long as:
Your internet connection does not drop.
Nobody runs an antivirus program on your system.

Input is marked with a >>. The command prompt is the empty >>.

Assuming a system with only TZ_INFECT installed...


>> malware bm bitminer
creating bm (bitminer)
... wait a minute ...

finished creating bm (bitminer)
>> software install 1
Success
bm installed

...wait...
(newlines)








>>

Note that I'm using a custom client that prefixes [BROADCAST] to all broadcast events.

How???

TZ_INFECT is Intercept's malware generator. We can generate a bitminer by running malware <name> bitminer. This will create a new piece of software with the given name of type bitminer

We then install the bitminer: software install <index>, where index is the 0-based index of the software as given by software list. This is why I made sure the only piece of software on the system was the malware generator. This places our new bitminer at index 1.

There is a bug in the implementation of bitminer that results in an empty broadcast event to be sent to the client whenever bits are generated. Since bits are generated at random intervals, the broadcasts are sent at random intervals. Forever.

Note: the newlines are harder to detect using the official game client, but you can see them by running a command and watching the newlines slowly push the resulting output off the screen.

Intercept

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

7, 1 byte

I

Try it online!

Outputs IôŸ (the bytes 49 f4 9f) repeated forever.

Explanation

In binary, I is 01001001, which is broken into 3-bit chunks to get 010 010 01. This (including the implicit trailing bit 1) is converted to octal to get the instructions 223. These instructions are executed and push 223 on the frame to get ||223.

The last section of the frame (223) is copied to the command list, and executed. 2 duplicates the last section, and the second 2 duplicates the duplicate, leaving ||223|223|223 on the frame. 3 outputs the last section of the frame, 223 (which is automatically converted to 7223 because it contains anonymous commands) and then removes the last two sections. This leaves the frame as ||223.

This process (executing 223) is repeated forever, each time adding 7223 to the output.

The very first 7 specifies the output format as "the same way the source was", meaning that each future command in the output adds three bits. This means that output consists of the bits 010 010 011 111 (from 2237) repeated forever. This is 49f in hexadecimal, so repeating it leads to the bytes 49 f4 9f 49 f4 9f ... .

\$\endgroup\$
2
  • \$\begingroup\$ Why is this non-competing? I see no reason why it couldn't compete with other answers, as the "newer languages aren't allowed" rule has been rendered obsolete and no longer applicable. \$\endgroup\$
    – lyxal
    Commented Jun 16, 2020 at 23:22
  • 1
    \$\begingroup\$ Thanks! I didn't know that the rule had been changed, and assumed it still applied because I had seen other answers to this question marked as non-competing without checking when they had been posted. \$\endgroup\$ Commented Jun 17, 2020 at 0:34
3
\$\begingroup\$

Ruby, 9 bytes

loop{p 1}

p 1 is equivalent to puts 1.inspect

Try it online!

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

[BASIC] 10 9 bytes

1?:goto 1

Try it online!

Infinite carriage return/line feeds. (I saved a byte when I realized I could just output new lines without a 0 or other character!)

It's the first program every BASIC programmer learnt back in the day! Good old "GOTO" and the hatred it earned from "real" programmers...

Of course, a more "structured" version would use an infinite WHILE loop, but that's 6 bytes more:

while(1):?:wend

Try it online!

(note:TIO truncates the output eventually, but in a standard BASIC interpreter, the code will run forever.)

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

Zsh, 15 bytes

while;do 0;done

prints command not found: 0 infinitely

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Well, this obviously requires that there is no executable file named 0 in the PATH. \$\endgroup\$ Commented Nov 5, 2022 at 13:20
3
\$\begingroup\$

Jelly

2 bytes

Ȯß

Try it online!

How it works

Ȯß  Main link. Implicit argument: 0

Ȯ   Output/print the implicit argument.
 ß  Recursively call the main link.
    Thanks to tail call optimization, this results in an actual infinite loop.
\$\endgroup\$
3
\$\begingroup\$

Cascade, 5 bytes

}h
|"

Try it online!

-2 bytes thanks to Jo King.

\$\endgroup\$
1
  • \$\begingroup\$ 5 bytes, which i'm pretty sure is optimal \$\endgroup\$
    – Jo King
    Commented Aug 18, 2021 at 16:24
3
\$\begingroup\$

Grass, 11 bytes

Taken from original document (Japanese):

wWWwwwwWWww

Try it online!

How it works

In pseudo code:

function f(x){
   Out("w");
   return x(x);
}
f(f)
\$\endgroup\$
0
2
\$\begingroup\$

TCL (15)

while 1 puts\ x

(kind'a silly that we must write at least 30 characters per reply :)

\$\endgroup\$
4
  • \$\begingroup\$ Just improved it a bit. Ignore the "always brace your expr" and other rules in code golf ;) (I forgot that I could edit without approval) \$\endgroup\$ Commented Nov 10, 2013 at 22:49
  • 1
    \$\begingroup\$ And if you use the interactive command line (tclsh), you could do w 1 pu\ x \$\endgroup\$ Commented Nov 10, 2013 at 22:55
  • \$\begingroup\$ @JohannesKuhn Nice! Tks. \$\endgroup\$
    – user7795
    Commented Nov 11, 2013 at 0:36
  • \$\begingroup\$ Indeed, a character minimum on a code golf site is a bit odd even though it's standard across other SE sites. I usually pad with &nbsp; when necessary - though it rarely is for me, since I usually add an explanation of my scripts as well. \$\endgroup\$
    – Iszi
    Commented Nov 14, 2013 at 17:50
2
\$\begingroup\$

F# - 22

while 1=1 do printf"x"
\$\endgroup\$
2
\$\begingroup\$

PowerShell (8)

Save "a.ps1" file with following script code:

1
.\a.ps1

from command line run the script.

My initial solution was different, but I found that it has already been answered by Iszi.

I managed to write another solution with same byte length.

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

Zozotez LISP: 7

When started with the minimal interactive session dictated in the documentation:

(:'r p)

How it works is that it redefined read so the REPL prints what print returns rather than keyboard input. The never ending output is continious lines of: Zozotez-moi~>NILNIL

When started with the full interactive session it gets longer, but does the same (10 bytes):

(:'read p)

Without a bootstrap Zozotez does just (eval(read)) thus we need to implement a loop in one expression (16 bytes):

((:'z(\()(p)(z))))

The : is set to it binds z to the lambda expression (\()(p)(z)), when called without arguments calls print (p) with default argument NIL, then calls z (recurses).

: returns the value (the function) so wrapping it all in parenthesis makes the initial call.

Common Lisp: 15

(loop(print())) ; prints infinite lines of NIL

Racket: 21

(let z()(print'N)(z))

Works similar. The names let makes and calls the proecdure l that takes no arguments. The body prints N, then recurses. print without arguments displays data quoted so it prints 'N'N'N....

Scheme: 23

(let z()(display'N)(z))

Same as Racket version only that display is used which don't quote output. NNNN...

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

PDP-11 Machine Code: 2 bytes (or 6 if you actually want output)

mov -(pc),-(sp)    ; octal opcode = 014746

On many PDP-11's, this instruction will actually do what it says - back the PC up, push its own opcode onto the stack, and repeat until all 64K ram is used up and then just keep filling circularly (or until something like a segfault happens). Not really infinite output, but it's in the spirit.

In 6 bytes you could do this, sending characters directly to the console

mov #SerialPortTxRegAddr,r0  ; oct= 012700 177562 hex = 0x15C0  0xFF72
movb -(pc),(r0)              ; oct= 114710        hex = 0x99C8 

Which would send a series of 0xC8 characters (these would be probably be seen as 'H' by any terminal which would be attached to the PDP-11)

Yes, I'm actually that old :-)

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

Atari 8-bit Basic — 7 characters

1L.:G.1

This is what you can type. Internally, it expands to:

1 LIST : GOTO 1

In Atari 8-bit Basic you can use LIST in a program.

I seem to remember this shorter sequence:

1L.:R.

Which expands to:

1 LIST : RUN

This would work, but I'm not sure if R. corresponds to RUN.

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

Rebol, 18 chars

forever[print now]

Can shave off 1 perhaps even 2 more characters by choosing something different to now (for eg. pi).

However I like that it prints the time forever :)

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

Thue, 19

b::=~1
a::=ba
::=
a

The code prints an infinite stream of 1's.

The code is quite simple and easy to understand.

  • b can be expanded as "output 1" (then b is replaced with empty string)
  • a can be expanded as/replaced by ba
  • ::= on its own ends the list of rules
  • a on the last line represents the initial state.
\$\endgroup\$
2
\$\begingroup\$

Brainfuck - 6/4

Brainfuck does this easily:

-[.>-]

If we assume that the interpreter didn't zero the cells on startup but instead gave us already-initialised cells, the count goes down by two:

[.>]
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Why not just -[.]? \$\endgroup\$
    – Justin
    Commented Apr 23, 2014 at 18:51
  • \$\begingroup\$ Hm, I haven't thought of that really, good idea. \$\endgroup\$
    – Darkgamma
    Commented Apr 24, 2014 at 13:34
  • \$\begingroup\$ But that is actually a duplicate of another answer here. \$\endgroup\$
    – Justin
    Commented Apr 24, 2014 at 17:32
  • \$\begingroup\$ I haven't seen the others before now, really, though even just -[.] is shorter than the other answers given. \$\endgroup\$
    – Darkgamma
    Commented Apr 25, 2014 at 12:06
  • \$\begingroup\$ You should really go through other answers, especially of the same language, before posting. \$\endgroup\$
    – Justin
    Commented Apr 25, 2014 at 15:19
2
\$\begingroup\$

Bash, 9 characters

echo 1;$0

Because the $0 variable always holds the filename.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm not sure this will really have an infinite output. You'll run out of memory before reaching infinity. echo 1;exec $0 would be better (but also has more characters). \$\endgroup\$ Commented Apr 26, 2014 at 9:22
2
\$\begingroup\$

C# (16)

Directly executable in LinqPad:

for(;;)0.Dump();
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 11 bytes (invisible) or 17 bytes (visible)

Clearly not going to win here, but it works

For[,0<1,1]

If you actually want to see the characters streaming down the screen, use

For[,0<1,Print@1]

(20 bytes). I had thought I might do well with

Range@∞

or even

1~Table~{∞}

But to my surprise, the Kernel checks for infinite bounds and disallows them.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You'll definitely need the Print version. But that can be golfed further: 1>0 is shorter than True, Print@1 is shorter than Print[1] and you can leave out one of the two consecutive commas to use the 3-argument version of For. \$\endgroup\$ Commented Sep 2, 2014 at 14:41
  • 1
    \$\begingroup\$ Echo@1 is shorter than Print@1. \$\endgroup\$
    – Roman
    Commented Jul 23, 2019 at 13:01
2
\$\begingroup\$

Pyth, 2

#G

(or # followed by any normal variable, actually).

# is while True. Variables are printed implicitly, and since G is 'abcdefghijklmnopqrstuvwxyz', that gets printed infinitely.

\$\endgroup\$
1
  • \$\begingroup\$ This makes me want to learn Pyth myself. \$\endgroup\$ Commented Nov 13, 2015 at 8:33
2
\$\begingroup\$

QBasic, 6 (5?) characters

?1:RUN

? is short for PRINT, and RUN without arguments runs the current file from the top. This is the shortest (and most interesting) way to get an infinite loop in QBasic.

If, as in the accepted answer, infinite newlines count, then I present this 5-character version:

?:RUN
\$\endgroup\$
2
\$\begingroup\$

x86 machine code, 6 bytes

b40e cd10 ebfc

Assembly version of the code:

mov ah, 0Eh;    bios teletype output
code_golf:
int 10h;        print character(ascii 0)
jmp code_golf;  loop

This code constantly prints ASCII 0 (NULL).

This was run using DOSBOX

\$\endgroup\$
2
  • \$\begingroup\$ What OS is this for? (DOS? Linux?) Can you put that in your answer? \$\endgroup\$
    – tbodt
    Commented Mar 5, 2015 at 22:24
  • \$\begingroup\$ @tbodt I did specify: x86. But, I can see how some people might be confused so I included how I ran the code. \$\endgroup\$
    – SirPython
    Commented Mar 5, 2015 at 23:14
2
\$\begingroup\$

Piet (135 codels/pixels)

Prints an infinite sequence of $ signs (arbitrary, could've been anything. Loaded in the upper right corner of course).

The awesome program

Run with npiet -v11 the.gif

\$\endgroup\$
3
  • 2
    \$\begingroup\$ In Piet, pixels count as characters. \$\endgroup\$
    – tbodt
    Commented Feb 2, 2014 at 23:40
  • \$\begingroup\$ @tbodt - It's 135 pixels, but 112 bytes (if I'm not mistaken) -- byte-count is supposed to be the code-golf criterion. \$\endgroup\$
    – r.e.s.
    Commented Feb 3, 2014 at 0:24
  • 4
    \$\begingroup\$ what if you save it in a different image format? for piet, I count number of pixels. \$\endgroup\$
    – tbodt
    Commented Feb 3, 2014 at 0:26
2
\$\begingroup\$

ArnoldC, 81 bytes

IT'S SHOWTIME
STICK AROUND 1
TALK TO THE HAND 0
CHILL
YOU HAVE BEEN TERMINATED

Prints 0 forever.

Explanation

IT'S SHOWTIME            # start program
STICK AROUND 1           # infinite while loop (since 1!=0)
TALK TO THE HAND 0       # print 0
CHILL                    # end loop
YOU HAVE BEEN TERMINATED # end program
\$\endgroup\$
1 2
3
4 5
12

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.