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
…
8 9
10
11 12
0
\$\begingroup\$

π”Όπ•Šπ•„π•šπ•Ÿ, 4 chars / 7 bytes

↻ô1;

Try it here (Firefox only).

Translates to while(output(1)); in JS.

\$\endgroup\$
4
  • \$\begingroup\$ output isn't a JS function... \$\endgroup\$
    – mbomb007
    Commented Apr 26, 2018 at 14:07
  • \$\begingroup\$ There isn't really an equivalent to that function in JS, I guess I made it a placeholder name for the custom output function that ESMin uses. \$\endgroup\$ Commented Apr 30, 2018 at 14:11
  • \$\begingroup\$ console.log would work \$\endgroup\$
    – mbomb007
    Commented Apr 30, 2018 at 14:51
  • \$\begingroup\$ The function doesn't output by lines, but rather appends to output... Perhaps it's more like a process.stdout.write. \$\endgroup\$ Commented Apr 30, 2018 at 20:08
0
\$\begingroup\$

C, 25 bytes

main(){while(puts("1"));}
\$\endgroup\$
0
\$\begingroup\$

R, 15 bytes

Infinitely prints 1

while(T)cat(1)
\$\endgroup\$
0
\$\begingroup\$

ForceLang, 23 bytes

label l
io.write goto l

Looks weird, but it works. The key here is that goto does not interrupt the evaluation of the current line, and returns nil.

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

Pylons, 5

wp,1}

While 1, print the (empty) stack.

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

Come Here, 21 bytes

COME FROM1 1TELL1NEXT

In the reference implementation, this prints infinitely many smiley characters. Don't ask me why.

\$\endgroup\$
1
  • \$\begingroup\$ The smiley characters are printed because you are using Windows cmd. \$\endgroup\$ Commented Oct 6, 2016 at 18:58
0
\$\begingroup\$

Clojure, 14 bytes

(pr(cycle[1]))
\$\endgroup\$
0
\$\begingroup\$

Brainfuck, 26 bytes

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

Do 4*4*2+1 (33, !) and the final loop is a infinite loop that only print the current memory cell (!).

\$\endgroup\$
1
  • \$\begingroup\$ You could as well do +[.] (SOH), but then it would be a dupe. \$\endgroup\$ Commented Oct 8, 2016 at 18:12
0
\$\begingroup\$

Haystack, 6 bytes (non-competing, the language came 2 years after the challenge)

>"a"o<

This will print a [infinity] times.

If the above code gives a syntax error because of the lack of | (that character is the needle. The ultimate goal of all Haystack programs is to find the needle in the haystack), one can simply append it to the end:

>"a"o<|
\$\endgroup\$
0
\$\begingroup\$

Golfscript - 7

{1a}:a~

I'm still learing golfscript, but unless I'm mistaken, this puts 1 on the stack, then starts over?

But apparently, the stack only prints when the program terminates, so perhaps this is better:

{1.p}do

(thanks Dennis)

\$\endgroup\$
4
  • \$\begingroup\$ You need a space between 1 and a, otherwise it's a single variable. I suggest using a non-alnum as a variable name {1?}:?+. Also, this doesn't print anything. \$\endgroup\$ Commented Mar 4, 2014 at 5:31
  • 1
    \$\begingroup\$ This won't work, it will just print a stack level too deep error. Also, the stack is printed when the program finishes, so you have to print explicitly. Example: {1.p}do (7 bytes) @JanDvorak: [A-Za-z]+[0-9]+ is a single token, but [0-9]+[A-Za-z]+ is not. \$\endgroup\$
    – Dennis
    Commented Jun 21, 2014 at 18:46
  • \$\begingroup\$ I would downvote, but my vote is locked in. Help me out! \$\endgroup\$
    – tbodt
    Commented Sep 3, 2014 at 21:54
  • \$\begingroup\$ Edited as per @Dennis \$\endgroup\$
    – McKay
    Commented Sep 7, 2016 at 21:42
0
\$\begingroup\$

Kitanai, 24 bytes

#print"0"&2&1#print"1"&~

The "#" defines a flag, the first "print" displays 0, then the "&2" jumps to the second "#", it then prints "1", goes to the origin ("&~") which is just after "&2", then it jumps to the first "#" thanks to the "&1". And it does all that infinitely :)

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

FALSE, 8 bytes

Interpreter.

There are many programs that work, each 8 bytes. The one I find the most interesting is:

[1$][.]#

This is a while loop (#). [1$] is the condition, and it pushes two 1s. The last is popped for the condition (true), and then [.] is executed, which prints the number.

Here are some other programs (including the above):

[1$][.]#
[1][1.]#
[1$.][]#
[^^.][]#
[^.^][]#
[^][^.]#

A more interesting 9-byte (arbitrary 1-char repeated):

['c$,][]#

I feel that the following 7-byte programs should work on some FALSE interpreters, but I haven't found any such yet:

[1"][]#
[.1][]#
\$\endgroup\$
0
\$\begingroup\$

Haxe, 19 bytes

while(1>0)trace(1);

There are several things about Haxe that make this challenge more difficult than in similar languages:

  1. Its lack of a for(;;) loop
  2. Its unwillingness to consider anything other than true or false truthy or falsy
  3. Its unwillingness to print an empty line (with trace();)

If not for these things, this program might have been 15 bytes:

for(;;)trace();

Test it online here, though you may not want to, as it freezes your browser.

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

C++, 23 bytes

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

I was too lazy, but hey, It outputs a javascript script, which does absolutely nothing!

\$\endgroup\$
2
  • \$\begingroup\$ This submission is neither a full program nor a function and thus is invalid (since snippets are disallowed). \$\endgroup\$
    – user41805
    Commented Jan 23, 2017 at 16:36
  • 1
    \$\begingroup\$ Now with FULL program! \$\endgroup\$ Commented Jan 23, 2017 at 23:40
0
\$\begingroup\$

QC 6 bytes

Not competing because language is newer than the question

A00$00

A00 prints nothing(because memory is empty) and a new line
$00 jump to start(no recursion, sets instruction pointer to 00)
\$\endgroup\$
0
\$\begingroup\$

Commodore 64/VIC-20 assembly, 8 bytes (assembled)

033C LDA #$40
033E JSR $FFD2
0341 JMP $033E

When executed with SYS 828, this will continuously output the @ character to the screen using the Kernal routine to print. If you don't care what you're outputting, then you can remove 033C LDA #$40 and save two whole bytes (the last thing in the accumulator will be outputted).

\$\endgroup\$
1
  • \$\begingroup\$ If you remove the code at 033C then you will need to execute with SYS 830, unless you start the code at 033C and change JMP 033E accordingly. \$\endgroup\$ Commented Feb 16, 2017 at 12:16
0
\$\begingroup\$

Fith, 16 bytes

{ 1 . q } :: q q

{ 1 . q } pushes to the stack an anonymous function which prints 1 then calls a function named q. :: q binds this function to the word q, then the final q calls the new infinitely-recursing function.

Non-recursive version (19 bytes):

{ 1 } { 1 . } while

This pushes a condition function, which will always push 1, and a body function, which prints 1. The while word continues to run the body as long as the condition leaves a truthy value on top of the stack.

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

Whitespace, 16 bytes


  
   
	
 	
 


Try it online!

Saves 3 bytes on the previous Whitespace answer by abusing empty labels and declaring the value 0 as a 0-bit number (the spec unfortunately requires a sign bit). Posting as a new answer as the previous answer combines multiple unrelated entries.

Explanation

(s - space, t - tab, n - newline)

nssn # Declare label ''
sssn # Push +0 onto the stack
tnst # Pop and output the stack value as a number - outputs the character '0'
nsnn # Jump to label ''
\$\endgroup\$
1
  • \$\begingroup\$ I'm kinda pleased that this answer uses four 4-byte instructions \$\endgroup\$
    – Ephphatha
    Commented May 27, 2017 at 13:23
0
\$\begingroup\$

shortC, 5 bytes

AWP'1

Prints 1 forever. Explanation:

A     main function
 W    forever
  P   print char
   '1 char '1'

Try it online!

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

Fission 2, 2 bytes

R"

Try it online!

Same concept as the Befunge answer. An atom is spawned at the R, and moves right. It reaches the " and enters string mode, wraps to the beginning of the line and prints an R, then exits string mode and wraps again.

Outside of string mode, R simply changes the atom's direction to right, which is the direction it's already moving in.

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

Add++, 9 bytes

+1
W,+1,O

Prints 1 Followed by a newline for ever.

Try it online!

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

AHK, 11 bytes

Loop
Send,a

Pretty boring but couldn't find an AHK answer in here and thought it worthy of submission.

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

Bitwise, 19 bytes

OUT 0 &1
JMP &-2 &1

Prints a load of null bytes. Try it online!

OUT 0 &1 prints the 0th register when a literal 1 is truthy. JMP &-2 &1 jumps back two (one) lines when a literal 1 is truthy.

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

Evil, 4 bytes

Continuously prints the Start of Heading character:

amwb

If NUL is allowed you could do mwb.

An alternative, that goes through the ASCII characters, would be:

mawb

Quick explanation of commands:

  • m = marking character;
  • a = increment accumulator;
  • w = write accumulator to STDOUT;
  • b = go backwards to marking character and continue execution from there.
\$\endgroup\$
0
\$\begingroup\$

J-uby, 11 Bytes

-:p|:+&1!~0

Explanation

!~ is iterate until constant: x = f(x) until x == f(x)

-:p is the global print function, and :+&1 increments a number

So -:p|:+&1 prints a number and returns the next integer

We start at 0

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

VBA, 18 Bytes

Anonymous VBE immediate window that outputs an infinite quantity of newline characters vbCrLf to the VBE immediate window

Do:DoEvents:?:Loop
\$\endgroup\$
0
\$\begingroup\$

QBIC, 3 2 bytes

{?

Explanation:

{   Start a DO-loop 
 ?  PRINT nothing, followed by \n
    The DO-loop is implicitly closed by QBIC.
\$\endgroup\$
0
\$\begingroup\$

R, 15 chars (or 6)

repeat print(1)

or, you could cheat slightly with just

repeat

which will invisibly return NULL for each time through the loop. Thus it will return NULL infinite times, you just can't tell.

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

Jq 1.5, 9 bytes

repeat(1)

Try it online! (truncated at 128K)

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

cQuents 0, 1 byte

$

Try it online!

Outputs 1,2,3,4,5,6... up to Python's int cap (aka as long as you have memory).

Alternately, any number would work, printing that number separated by commas infinitely:

7

Try it online!

This may work at some point with an empty program once I finish this language - theoretically it should print 0,0,0,0,0,0... for an empty program but I haven't implemented empty nodes.

\$\endgroup\$
1
…
8 9
10
11 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.