150
\$\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
  • 144
    \$\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\$ Commented Nov 9, 2013 at 20:00
  • 29
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$ 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\$ Commented Nov 12, 2013 at 20:27
  • 13
    \$\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\$ Commented Apr 10, 2015 at 21:15

349 Answers 349

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

BASIC

10 PRINT "HELLO WORLD" 20 GOTO 10 
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Gotta love the classics. \$\endgroup\$ Commented Mar 21, 2016 at 4:07
  • 2
    \$\begingroup\$ Is it required to use 10 20 not 1 2? \$\endgroup\$ Commented Apr 2, 2018 at 2:49
  • \$\begingroup\$ need to print DURAN DURAN RULEZ!!1!! for proper 80s vibe :) \$\endgroup\$ Commented Jul 29, 2019 at 10:39
8
\$\begingroup\$

Postscript 9

{1 =}loop 

Output 1 in an infinite loop.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ {[=}loop outputs ---nostringval--- repeatedly in 8 bytes. \$\endgroup\$ Commented May 11, 2022 at 9:29
8
\$\begingroup\$

Python, 17 13

while 1:print 

Thanks to Ben and r.e.s.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can shorten it to 15 by not printing a string and removing the brackets in the while... while 1:print 1 \$\endgroup\$ Commented Nov 10, 2013 at 21:03
  • 6
    \$\begingroup\$ Or 13 characters: while 1:print produces "infinite output" composed of newlines. \$\endgroup\$ Commented Nov 10, 2013 at 21:40
  • \$\begingroup\$ How about while 1:1 ? \$\endgroup\$ Commented Apr 28, 2014 at 18:18
  • \$\begingroup\$ which @KaranGoel found below as well \$\endgroup\$ Commented Apr 28, 2014 at 18:23
  • \$\begingroup\$ @Willem Absolutely wrong. Literals are not allowed by themselves on executed programs. \$\endgroup\$ Commented Apr 12, 2016 at 13:10
8
\$\begingroup\$

C, 20 bytes (competing according to the challenge's creator)

Suppose the following code is contained in e.c:

int i; #include"e.c" 

In a machine with infinite system resources, GCC will create an infinitely big temporary file trying to resolve a recursive #inclusion.

\$\endgroup\$
8
\$\begingroup\$

C, 26 25 24 (no recursion)

main(){for(;;puts(""));} 

Prints endless '\n' characters. This is a byte longer than the shortest C answer, but doesn't rely on tail call optimization to avoid overflowing the stack.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You can shave off a character by using for(;;) in place of while(! \$\endgroup\$ Commented Nov 12, 2013 at 14:09
  • 1
    \$\begingroup\$ no ! as it keeps outputting \n \$\endgroup\$ Commented Mar 29, 2018 at 3:06
7
\$\begingroup\$

R, 13 characters

repeat cat(1) 
\$\endgroup\$
2
  • \$\begingroup\$ repeat cat(1) should work \$\endgroup\$ Commented Nov 9, 2013 at 19:40
  • \$\begingroup\$ @Dason you're right. I have no idea why i insist on putting them every time. \$\endgroup\$ Commented Nov 11, 2013 at 7:57
7
\$\begingroup\$

PowerShell 2.0: 17 11 8

My initial solution:

while(1 -eq 1){1}

Thanks to r.e.s.:

while(1){1}

I think Danko Durbić has the winner, here. I'm not even sure why this should work, but it does and I can't think of anything shorter so far.

for(){1} 
\$\endgroup\$
10
  • 1
    \$\begingroup\$ Try while(1){1}. \$\endgroup\$ Commented Nov 12, 2013 at 2:18
  • 6
    \$\begingroup\$ Try for(){1} . \$\endgroup\$ Commented Nov 14, 2013 at 13:22
  • 1
    \$\begingroup\$ I'm not sure. for(;;) works, and you can use carrige returns instead of semicolons, so if for(<CR><CR>) works, so should for(), if you collapse the white space. \$\endgroup\$ Commented Nov 14, 2013 at 15:48
  • 1
    \$\begingroup\$ @DankoDurbić For() does indeed work, but I'm trying to understand why. I don't even get why for(;;) should work. I would expect that, without any defined conditions, the loop should fail to run and/or output an error. \$\endgroup\$ Commented Nov 14, 2013 at 17:30
  • 1
    \$\begingroup\$ In C, C++, C#, and Java, all of the expressions that define a for statement are optional. I guess the Powershell creators chose to do the same. \$\endgroup\$ Commented Nov 15, 2013 at 9:23
7
\$\begingroup\$

C#, 57

Another C# entry, with a twist:

class M{static int Main(){for(;;)System.Console.Beep();}} 

This interpretation of "output" may be stretching, but still valid I think (and saves 4 characters! :)

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't work on 64 bit \$\endgroup\$ Commented Mar 4, 2014 at 2:15
  • \$\begingroup\$ @ColeJohnson Works fine for me: copy con test.cs, paste that string, ^Z and press Enter. csc /platform:x64 test.cs shows no errors. test.exe gives beep beep beep... \$\endgroup\$ Commented Mar 4, 2014 at 20:09
  • \$\begingroup\$ I meant that the internal speaker won't work. You'll hear the beeps, but they're "artificial" beeps from your desktop speakers. \$\endgroup\$ Commented Mar 7, 2014 at 0:17
  • \$\begingroup\$ @ColeJohnson Ah, that is true. Starting with Windows 7 64-bit the PC speaker driver (beep.sys) was changed to direct output to the sound card. \$\endgroup\$ Commented Mar 7, 2014 at 0:30
7
\$\begingroup\$

Xojo, 18 12 chars

Again, please don't actually run this (same reason as the JavaScript answer):

do Beep loop 

Never said the output couldn't be audio...

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

Windows Batch File, 1 character

Create a file called a.bat containing:

a 

When you execute this batch file, it executes itself. The Windows Command Prompt by default echos every command to the console, so the output resembles:

C:\>a C:\>a C:\>a C:\>a 
\$\endgroup\$
7
\$\begingroup\$

Malbolge (Original Interpreter), 164 46 44 bytes

Warning: This code uses non-printable characters, replace [2] by a byte containing 0x02, [8] by a byte containing 0x08, and [15] by a byte containing 0x0f

(=&`^?>=<}X98765[2][8][18][2][2][8][2][2][2][8][2][2][2][8][2][2][2][8][18]!~}|{zy[15] 

1505 bytes? Using a toolchain? Surely we can torture ourselves improve this code even more by making it non-portable. This program prints the letter T forever. The technique used here is the same one used by Lou Scheffer. We try to find a good enough pattern that we can exploit for our program, and we put the goto values we want in binary as padding (When not needed, I just used o instead). Luckily, we have a pattern of printing (<) at position 3 and a pattern of data pointer jumping (j) at position 9. In the previous version of the code, I absolutely wanted these two commands to be together, which was a mistake. Here's an explanation of the normalized version of the code minus the extra bytes:

jpj -> Put some printable value in the accumulator and jump to data section <i -> Sometimes prints and jumps to the data pointer resetting section oooo ji -> Sometimes rewinds the tape and jumps to the printing section [INSERT PADDING HERE] 

Malbolge and Malbolge Unshackled, 113 90 bytes

(=BA@?>=<;:381x543,1N/.-,+*)('&%$#"!~}|{zLxwvutWr1p0.--kjihgfedcF!`_^]\[ZYXWVUTSRQPONMLKig 

Try it online!

To be honest, I wasn't completely satisfied with my first attempt, since it was using a well-known bug from the original interpreter, so I tried to rewrite it so that it would work for a regular interpreter. This took more time than I expected... Well, if you disregard the fact that I'm programming in Malbolge. Here's a short explanation of the code:

j -> Move the data pointer forward pooooooooopopjooop -> Crazy operations in order to initialize the right values into the data section loop o i -> Starts loop oooooooooooooooooooo / -> Used for the first crazyop at the start of the program ooooo joio<i -> MAIN LOOP: Alternates between data pointer resetting and printing i< -> Some values for crazyop and goto ooooooooo ji -> Data pointer resetting loop oooooooooooooooooooooo <i -> Printing loop 
\$\endgroup\$
6
\$\begingroup\$

sed 7 5 chars (version 4.2)

sed -e ':;p;b' <<<'Hello world!' 

Unfortunely, this won't work anymore, from version 4.4 of GNU sed:

sed -e ':a;p;ba' <<<'Hello world!' 
\$\endgroup\$
1
  • \$\begingroup\$ sed (GNU sed) 4.4: -e expression #1, char 1: ":" lacks a label \$\endgroup\$ Commented Oct 14, 2017 at 12:24
6
\$\begingroup\$

Perl 6, 13 11 characters

Using some nice Perl6 syntax for infinite lazy lists:

.say for ^∞ 

The code will print all integers from 0 upto infinity or until RAM runs out to store single big integers, whichever comes first.

\$\endgroup\$
1
  • \$\begingroup\$ whichever comes first I think it's safe to say that RAM is infinite \$\endgroup\$ Commented Mar 17, 2019 at 22:27
6
\$\begingroup\$

Microsoft Word Math AutoCorrect, 4 bytes

  1. Add a new entry in the Math AutoCorrect table (not the regular one): $ -> $$. This makes a new production rule for us. (3 bytes)
  2. Insert an Equation (Alt + =), and type a single $. The AutoCorrect will replace it with a $$, but that's not you inputting any extra characters, so it doesn't add to the byte count. This initializes our memory tape. (1 byte)
  3. Press the "Professional" button under Equation Tools. This is where the program officially starts running.
  4. Watch the little right-moving Turing Machine freeze up Word.

Explanation

The Professional button makes the Autocorrect sweep from left to right, running the production rules. On initial state $$, it sees a $ in the first position and replaces it with $$.

  1. Then check the next position to the right.
  2. Replace $ with $$, otherwise halt.
  3. Repeat.

Note: the GUI doesn't show the $'s being generated as the GUI doesn't update until the operation halts, which it won't here. If you open up your Task Manager, you can see the memory usage going up to see that this is indeed what's happening. Sure, I could have made it produce more $s but the production rate would still just be O(1) per loop anyway, and I'd be spending precious bytes programming it. Oh, and I'm counting the $s as output.

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

Brachylog, 2 bytes

ẉ⊥ 

Try it online!

Not only does it print infinitely, this prints something much more interesting than most answers here: all integers.

Explanation

 Assign an integer value to the input ; since it is not constrained, it can take any value in (-∞, +∞), meaning there is an infinite number of choice points The way = is implemented makes it so that the order of choices it will try is: 0, 1, -1, 2, -2, 3, -3, … ẉ Write that integer to STDOUT, followed by a linebreak ⊥ False (i.e. go back and try the next integer choice point) 
\$\endgroup\$
1
  • \$\begingroup\$ @DLosc Thanks for the update, that answer was for old versions of Brachylog \$\endgroup\$ Commented Apr 19, 2022 at 11:57
5
\$\begingroup\$

tinylisp, 15 bytes

(d f(q(A(f g (f 

Try it online!

An infinite stream of errors. Just as DLosc intended, hopefully.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ah, cool. I had (d f(q(_(f(a for my function. \$\endgroup\$ Commented Feb 5, 2022 at 3:51
  • \$\begingroup\$ Improved \$\endgroup\$ Commented Jun 10, 2022 at 1:49
5
\$\begingroup\$

unsure, 20 bytes

um but uh okay wait 

Don't try it online, it freezes the browser. Listen to the code. Even it wants you to wait. Think this through. Don't be like me. Don't lose that unsaved tab.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ +1 for being (I think, not going to scroll through all 11 pages of answers to this question) the first answer here where the source code itself warns you about the inadvisability of infinite loops with no halting conditions \$\endgroup\$ Commented Apr 14, 2022 at 2:52
4
\$\begingroup\$

Haskell, 18 chars

a=1:a;main=print a 

N.B.: No new line at end!

\$\endgroup\$
1
  • \$\begingroup\$ 17 bytes \$\endgroup\$ Commented Apr 14, 2019 at 4:09
4
\$\begingroup\$

Perl, 10 chars

TIMTOWTDI:

$ perl -nE 'say;redo' <input> # 10 chars (1+8+1) $ perl -E 'say while+1' # 11 chars $ perl -E 'o:say;goto o' # 12 chars $ perl -E 'for(;;){say}' # 12 chars $ cat inf.pl warn;exec$^X,$0 # 15 chars 

If you have infinite RAM (maybe in the cloud ;-)

$ perl -E 'sub o{say;&o}o' # 14 chars $ perl -E '&{*1=sub{say;&1}}' # 17 chars, just kidding *gg* 
\$\endgroup\$
4
\$\begingroup\$

Matlab, 13

while 1 1 end 

This prints

ans = 1 

infinitely many times.

\$\endgroup\$
1
  • \$\begingroup\$ While this should work, note that MATLAB gets very angry if you try. I tested it in R2020b and it just froze up before it displayed anything. \$\endgroup\$ Commented Aug 16, 2021 at 0:00
4
\$\begingroup\$

Emotinomicon 18 bytes, 5 chars

😀⏪😀😨⏩ 

Explanation:

😀 - push 0 to the stack ⏪ - open loop 😀 - push 0 to the stack 😨 - pop top of the stack, and output it as a number ⏩ - end loop 

As long as there's something on the stack, it's going to loop indefinitely

\$\endgroup\$
1
  • \$\begingroup\$ Golfs two bytes, and saves precious RAM: ℹ⏪😨ℹ⏩. \$\endgroup\$ Commented Oct 8, 2016 at 18:27
4
\$\begingroup\$

PHP - 17 16 13 11 bytes

<?for(;;)echo O; 

Oh well, I guess PHP can't ever always win... Thank you, m.buettner

Edit:

<?for(;;)0/0; 

As it turns out, division by zero triggers a warning in php. So, even though the output it produces goes to STDERR, it's still infinite!

Edit 2:

for(;;)0/0; 

(Run with php -r)

\$\endgroup\$
7
  • \$\begingroup\$ Do you really need that 1 in there? \$\endgroup\$ Commented Jul 3, 2014 at 19:00
  • \$\begingroup\$ Huh. Apparently I don't. \$\endgroup\$ Commented Jul 3, 2014 at 19:51
  • \$\begingroup\$ You don´t need the tags either if you use -r. Apart from that, this can´t be done any shorter (yet). \$\endgroup\$ Commented Jan 8, 2017 at 16:54
  • \$\begingroup\$ @Titus Or can it :) Thanks for pointing out this answer to me :D \$\endgroup\$ Commented Jan 8, 2017 at 19:09
  • \$\begingroup\$ Wish I could upvote it again. :) Nice! You can still remove <?. Oh and if notices were in the default config ... _ or any letter instead of 0/0. Unfortunately they aren´t. \$\endgroup\$ Commented Jan 8, 2017 at 23:21
4
\$\begingroup\$

Wolfram Language ( Mathematica ) 14 bytes 12 characters

Do[Echo@1,∞] 
\$\endgroup\$
2
  • \$\begingroup\$ Do these 12 characters not need 14 bytes? \$\endgroup\$ Commented Sep 26, 2017 at 8:27
  • \$\begingroup\$ @JonathanFrech yes, thank you, I corrected it. \$\endgroup\$ Commented Sep 26, 2017 at 10:16
4
\$\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
4
\$\begingroup\$

tinylisp, 13 bytes

(d f(q(a(f (f 

Try it online!

Outputs an infinite stream of errors. -1 thanks to DLosc.

The tinylisp interpreter throws a warning because a shadows the builtin a, but continues evaluation.

\$\endgroup\$
1
  • \$\begingroup\$ @DLosc Idk how I didn't notice that, thanks \$\endgroup\$ Commented Sep 3, 2022 at 3:46
4
\$\begingroup\$

Bash, 3 bytes

top 

Haven't seen anyone do this yet. It's tied with the yes answer.

\$\endgroup\$
3
  • \$\begingroup\$ When I tried this, all the more output I got was bash: top: command not found. \$\endgroup\$ Commented Aug 16, 2021 at 0:03
  • \$\begingroup\$ @Bbrk24: top(1) (table of processes) is actually a standard utility. \$\endgroup\$ Commented Nov 5, 2022 at 13:28
  • \$\begingroup\$ @KaiBurghardt I'm using a bash install on Windows, not Linux. According to bash --version, I have "GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)". It's missing some things that people tend to expect a bash install to have -- most frustratingly man (though eventually I aliased man to npx tldr). \$\endgroup\$ Commented Nov 6, 2022 at 14:52
4
\$\begingroup\$

, 5 chars (11 UTF-8 bytes)

screenshot

➰1:☾ 

Prints an infinite stream of new lines, since calling (Python's print) with no arguments prints a singular new line. You could try it in the web UI, but there, it just hangs without output (it prints after halting).

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

APL, 7

{∇⎕←⍵}1 

Defines a function which output its argument and call itself with the same argument, then call it once with argument 1

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

Brainfuck, 8 bytes

+[[+.]+] 

Assumes wrapping.

I'm aware that a solution was already posted by @cardboard_box - allthough his doesn't print any printable characters.

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

Shell, 16 Chars

cat /dev/urandom 
\$\endgroup\$
1
  • 2
    \$\begingroup\$ also grep -r / . for 12 bytes \$\endgroup\$ Commented Jul 29, 2019 at 10:36
1
2
3 4 5
12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.