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
3 4
5
6 7
12
2
\$\begingroup\$

Brainf*ck (4 bytes)

+[.] 

Infinitely produces 0x01 character.

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

PowerShell, 8 bytes

for(){1} 

Try it online!

Not the most clever, but it gets the job done.

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

Dis, 1 byte.

{ 

Outputs infinite amount of NULL characters; may be slow, depending on the implementation.

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

Arduino, 61 60 bytes

Managed to shave off one byte by changing what counts as "output".

void setup(){pinMode(13,1);digitalWrite(13,1);}void loop(){} 

Explanation:

void setup() { pinMode(13, OUTPUT); // OUTPUT is defined as 1 // Arduino boards have a built-in LED connected to pin 13, hence the choice of pin digitalWrite(13, HIGH); // HIGH is defined as 1 } void loop() {} 

This leaves the built-in LED on, which in a sense is output.


Original answer:

void setup(){Serial.begin(300);}void loop(){Serial.print(0);} 

Fills the Serial monitor with 0s.

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

Nim, 16 bytes

while 1>0:echo 0 
\$\endgroup\$
2
\$\begingroup\$

ℒight, 0 bytes

Prints "#" infinitely.

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

TRANSCRIPT, 33 bytes

You can see a f here. >TIE F TO F 

Try it online!

Generate endless warnings due to unclosed loop. At first it doesn't seem possible to loop infinitely in TRANSCRIPT because it only have for loops, but this works.

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

Seed, 4 bytes

1 87 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented Feb 17, 2022 at 3:34
2
\$\begingroup\$

Dyalog APL: 8 characters

Now you can choose what to infinitely print out! Yeeeay!!!

{⍞←⍵⋄∇⍵} 
\$\endgroup\$
2
\$\begingroup\$

Retina, 4 bytes

+:`0 

Try it online

Doesn't match, so output 0, then it does match so output 1, now it doesn't so output 0, etc...

\$\endgroup\$
1
  • 2
    \$\begingroup\$ A paradox in action :) \$\endgroup\$ Commented Nov 15, 2016 at 23:05
2
\$\begingroup\$

Oration, 21 bytes

to iterate, 1 listen, 

40 bytes:

to iterate, 1 listen, capture, that's it 

The first does print() (but it's python 2, so prints infinite ()), the second does print('') (the capture begins a string and the that's it ends it).

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

Sesos, 2 bytes

0000000: 2c06 ,. 

Prints an infinite amount of ones.

Try it online! Check Debug to see the generated binary code.

How it works

The binary file above has been generated by assembling the following SASM code.

set numout ; Switch to numeric output add 1 ; Set the current cell to 1. jmp ; Jump to the corresponding exit marker. put ; Print the integer in the current cell to STDOUT. ; (implicit jnz) ; If the integer in the current cell is non-zero, ; jump to the previous instruction. 
\$\endgroup\$
2
\$\begingroup\$

Assembly (MIPS, SPIM), 8 bytes

main:j$0 

Try it online!

This has both infinite stdout output and infinite stderr output, meaning that I have \$\infty * 2\$ output. Clearly that makes my answer twice as good as the others and I should win 😏.

Jumps to the zero register, which is an invalid address. Without macros:

main: # Jump to the address in the zero register, which is 0 jr $zero 

This causes the default exception handler to emit the following error message repeatedly while it advances the PC past the offending instruction to "solve" the problem.

 Exception 6 [Bad instruction address] occurred and ignored Exception 6 [Bad instruction address] occurred and ignored Exception 6 [Bad instruction address] occurred and ignored ... 

All while the interpreter also prints this error message to stderr:

Exception occurred at PC=0x00000000 Bad address in text read: 0x00000000 Exception occurred at PC=0x00000004 Bad address in text read: 0x00000004 Exception occurred at PC=0x00000008 Bad address in text read: 0x00000008 ... 

This continues until it eventually wraps to __start, jumps to main, then jumps back to 0x00000000.

\$\endgroup\$
3
  • \$\begingroup\$ J‑type instructions take just 4 Bytes. JR $zero is encoded as 000000 (indicating a special opcode), 00000 (indicating the $zero register), 0000000000 (a constant value), 00000 (optional hint), and 001000 (“jump register”). \$\endgroup\$ Commented Nov 8, 2022 at 0:08
  • \$\begingroup\$ This is for SPIM which is scored as text AFAIK. Normally I would score opcodes if it could interpret it directly \$\endgroup\$ Commented Nov 8, 2022 at 0:15
  • 1
    \$\begingroup\$ I see. It’s confusing if usually machine code instructions are counted. \$\endgroup\$ Commented Nov 8, 2022 at 0:26
2
\$\begingroup\$

(,), 10 Chars or \$10\times \log_{256}(3)\approx1.98\$ Bytes

(,,(),,()) 
\$\endgroup\$
2
\$\begingroup\$

GFortran, 21 18 bytes

Endless newlines. Try it Online!

1 print*;goto1 end 

Alternate solution (19B), hopefully less annoying for @Sapphire_Brick!

do;print*;enddo end 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is why I hate Fortran. No labels, just numbers. \$\endgroup\$ Commented Dec 25, 2020 at 20:29
1
\$\begingroup\$

C#, 60

class A{static void Main(){for(;;System.Console.Write(1));}} 
\$\endgroup\$
1
\$\begingroup\$

Golf-Basic 84, 2 characters

p` 

Causes upper-right line of 5 pixels to alternate continuously.

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

Assembly X86 - 44

Ok Im not a geek with assembly x86 but I wanted just to add this as a possible solution. COnsidering the number of dependencies needed by assembly code this code should be the shortest, a simple infinite loop that prints "x":

loop: movl $120, %rdi call putchar jmp loop 
\$\endgroup\$
1
\$\begingroup\$

awk, 15/19 chars

Canonical version, 19 characters:

BEGIN{for(;;)print} 

Version requiring user input (one newline will do), 14 chars (scores as 15 because of the required input):

{for(;;)print} 
\$\endgroup\$
1
\$\begingroup\$

~-~! - 17 bytes, with cheating 14 bytes

'=|@*:'&*|:'&|.|: 

~-~! has a very limited set of characters, and does not support explicit numbers, so yeah. Outputs infinite .s.

If I sway the rules a bit and have the user input the character to be output infinitely, I can have 15 bytes:

'=|@*:'&*|:'&^: 

Depending on the implementation, the last : for both of these can be left out, lowering the byte counts to 16 and 14.

\$\endgroup\$
2
  • \$\begingroup\$ Please link to a web page describing this language. Google turns up nothing, because the language name is just punctuation. \$\endgroup\$ Commented Mar 10, 2014 at 23:24
  • \$\begingroup\$ @tbodt It's over here; the language is unimplemented but it wasn't designed for this question and I didn't influence it's development at all. :P \$\endgroup\$ Commented Mar 11, 2014 at 8:19
1
\$\begingroup\$

J 11

echo@-^:_]1 

Outputs 0's because inversion (-) does not converge when repeated ad infinitum (^:_).

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

A couple of attempts for AVR, starting with one of the older MEGA/TINY models:

inc r1 out DIRC, r0 out DIRC, r1 rjmp .-2 

And for XMEGA:

inc r0 sts PORTC_DIR, r0 rjmp .-2 

Both of these rely on registers being cleared at reset. The output is toggling the direction of PORTC pin 0 (input/output). You need to connect a pull-up resistor to this pin to create a square wave.

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

Coffeescript 15

 alert 0 while 1 
\$\endgroup\$
1
  • \$\begingroup\$ Or: loop alert() \$\endgroup\$ Commented Apr 11, 2015 at 15:57
1
\$\begingroup\$

Bash - 13

cat /dev/zero 

OK, not the shortest, but I'm surprised this isn't here already.

\$\endgroup\$
1
  • \$\begingroup\$ Even shorter: cat /dev/zero. \$\endgroup\$ Commented Jun 22, 2014 at 22:05
1
\$\begingroup\$

Bash - 7 Characters

echo $0 

Prints endless newlines, at least until your computer crashes.

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

k4/q (11)

{x}{x""}/-1 

The second function, {x""}, sends the empty string to x. If x is an integer, it's interpreted as a file descriptor. If x is 1 or 2, this means to print to stdout or stderr. If x is -1 or -2, this means to additionally print a newline. The result of sending a string to a file descriptor is that file descriptor.

The first function {x} is an idempotent function. (k has an actual idempotent function, ::, but in this context, it would have to be written (::), so this is shorter.)

The construct g f/x, where g and f are functions, is a variation on functional "fold": f x is called, then g f x is tested as a boolean; if it is false, execution stops; if not, f is called on the result of the prior call. (The return value is the result of the last call to f.) Every integer but 0 is truthy, so the -1 returned by {x""} -1 allows the execution to continue.

As a bonus, and at no extra character cost, if you swap the / for a \, you'll waste infinite amounts of RAM too: g f\x does the same thing, but it saves all the intermediate results as it runs--its return value is the full list of intermediate returns of f.

Just for fun, here's another alternate version. It's the same length, but only valid in k (not q):

`{x}{x@$x}/1` 

This one prints "1"s forever, through more or less the same method, but a couple details are slightly different.

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

CJam, 6 bytes

1{1p}h 

or

{1p1}g 

which prints an infinite number of 1\n without risk of stack overflow (as there's no recursion).

I think CJam might be younger than the question, but this answer isn't a winner, so I don't see any harm.

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

LOOP, 1

. 

Outputs 000000000...

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

This Programming Language, 2

In the spirit of the Befunge answer,

i' 

Outputs 105 indefinitely.

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

Turing Machine Code (9)

(This is the dialect of turing machine code used by many online turing machine simulators)

 0 * 1 r * 

It will endlessly ouput ones while moving rightward along the tape.

\$\endgroup\$
1
3 4
5
6 7
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.