134
\$\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
  • 133
    \$\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
    Nov 9 '13 at 20:00
  • 21
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$
    – Izkata
    Nov 10 '13 at 1:39
  • 5
    \$\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
    Nov 12 '13 at 20:27
  • 9
    \$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ Jul 11 '14 at 20:11
  • 8
    \$\begingroup\$ @Doorknob So really, the challenge is to produce infinite output in a finite amount of time. Sounds easy enough. \$\endgroup\$
    – Sanchises
    Apr 10 '15 at 21:15

308 Answers 308

1
2
3 4 5
11
7
\$\begingroup\$

R, 13 characters

repeat cat(1)
\$\endgroup\$
2
  • \$\begingroup\$ repeat cat(1) should work \$\endgroup\$
    – Dason
    Nov 9 '13 at 19:40
  • \$\begingroup\$ @Dason you're right. I have no idea why i insist on putting them every time. \$\endgroup\$
    – plannapus
    Nov 11 '13 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\$
    – r.e.s.
    Nov 12 '13 at 2:18
  • \$\begingroup\$ @r.e.s. I was going to trim it by two with while($true){1} but your solution definitely wins - I forgot that in PowerShell $true -eq 1. Post that as an answer, and I'll give you a vote. \$\endgroup\$
    – Iszi
    Nov 12 '13 at 2:21
  • \$\begingroup\$ Feel free to edit it into yours, if you like. \$\endgroup\$
    – r.e.s.
    Nov 12 '13 at 2:35
  • 6
    \$\begingroup\$ Try for(){1} . \$\endgroup\$ Nov 14 '13 at 13:22
  • \$\begingroup\$ @DankoDurbić Can you explain why that works? I don't quite get it. Seems it should error to me. \$\endgroup\$
    – Iszi
    Nov 14 '13 at 14:15
7
\$\begingroup\$

Postscript 9

{1 =}loop

Output 1 in an infinite loop.

\$\endgroup\$
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\$
    – Cole Tobin
    Mar 4 '14 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\$
    – Nick
    Mar 4 '14 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\$
    – Cole Tobin
    Mar 7 '14 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\$
    – Nick
    Mar 7 '14 at 0:30
7
\$\begingroup\$

BASIC

10 PRINT "HELLO WORLD"
20 GOTO 10
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Gotta love the classics. \$\endgroup\$ Mar 21 '16 at 4:07
  • 2
    \$\begingroup\$ Is it required to use 10 20 not 1 2? \$\endgroup\$
    – l4m2
    Apr 2 '18 at 2:49
  • \$\begingroup\$ need to print DURAN DURAN RULEZ!!1!! for proper 80s vibe :) \$\endgroup\$
    – roblogic
    Jul 29 '19 at 10:39
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\$

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\$
    – Ben
    Nov 10 '13 at 21:03
  • 6
    \$\begingroup\$ Or 13 characters: while 1:print produces "infinite output" composed of newlines. \$\endgroup\$
    – r.e.s.
    Nov 10 '13 at 21:40
  • \$\begingroup\$ How about while 1:1 ? \$\endgroup\$
    – Willem
    Apr 28 '14 at 18:18
  • \$\begingroup\$ which @KaranGoel found below as well \$\endgroup\$
    – Willem
    Apr 28 '14 at 18:23
  • \$\begingroup\$ @Willem Absolutely wrong. Literals are not allowed by themselves on executed programs. \$\endgroup\$ Apr 12 '16 at 13:10
7
\$\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\$
6
\$\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\$
6
\$\begingroup\$

Brachylog, 3 bytes

=w\

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, …
         
 w      Write that integer to STDOUT
  \     False (i.e. go back to = and try the next integer choice point)
\$\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\$ Oct 14 '17 at 12:24
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
5
\$\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\$
    – MilkyWay90
    Mar 17 '19 at 22:27
4
\$\begingroup\$

Haskell, 18 chars

a=1:a;main=print a

N.B.: No new line at end!

\$\endgroup\$
1
  • \$\begingroup\$ 17 bytes \$\endgroup\$
    – dfeuer
    Apr 14 '19 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\$
    – Bbrk24
    Aug 16 at 0:00
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\$ Jul 3 '14 at 19:00
  • \$\begingroup\$ Huh. Apparently I don't. \$\endgroup\$ Jul 3 '14 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\$
    – Titus
    Jan 8 '17 at 16:54
  • \$\begingroup\$ @Titus Or can it :) Thanks for pointing out this answer to me :D \$\endgroup\$ Jan 8 '17 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\$
    – Titus
    Jan 8 '17 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\$ Sep 26 '17 at 8:27
  • \$\begingroup\$ @JonathanFrech yes, thank you, I corrected it. \$\endgroup\$ Sep 26 '17 at 10:16
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
  • 1
    \$\begingroup\$ also grep -r / . for 12 bytes \$\endgroup\$
    – roblogic
    Jul 29 '19 at 10:36
3
\$\begingroup\$

Haskell, 9 characters

show[1..]

Prints an infinite list of integers starting from 1, exploiting the lazy evaluation of Haskell.

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

Piet, 3 codels

==> Piet_infinite_output_codel_size_1 <== here it is ;)

Just to make it visible, the same at codel size 10:

Piet_infinite_output_codel_size_10

This program bounces forward and backward, executing:

forward direction:

1
PSH (push on stack)
OUN (output number)

backward direction:

MUL (multiply)
POP (pop from stack)

The backward instructions are ignored because the stack is empty.

So, the program prints an infinite amount of 1’s in the console.

Edit: I just noticed that captncraig came up with the same answer before me. Sorry for that. Please upvote captncraig’s answer instead.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I suggest you make this CW and/or suggest that others upvote captncraig's answer if you think it's good. \$\endgroup\$
    – lirtosiast
    Jun 14 '15 at 1:49
3
\$\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\$ Oct 8 '16 at 18:27
3
\$\begingroup\$

Desmos, 1 byte

x

Graphs y=x forever.

\$\endgroup\$
1
  • \$\begingroup\$ Look, submitting Desmos answers is my job :P Good job anyway. \$\endgroup\$ Jun 1 '19 at 17:48
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
    May 17 '18 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
    May 17 '18 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
    May 17 '18 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
    May 17 '18 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
    May 18 '18 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\$ May 14 '19 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\$
1
2
3 4 5
11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.