146
\$\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
    Nov 9, 2013 at 20:00
  • 27
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$
    – Izkata
    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
    Nov 12, 2013 at 20:27
  • 12
    \$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ 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
    Apr 10, 2015 at 21:15

334 Answers 334

1
6 7
8
9 10
12
1
\$\begingroup\$

Spice, 26 bytes

Spice module that recursively calls itself. Quite quickly hits a stack overflow.

Given the code is saved to a module named 'x':

;return@OUT a;LOD .\x a a;

Un-golfed explanation

;a;return@    - define variables: "a" and the required "return"
OUT a;   - output a, implicitly "[0]"
LOD .\x a a;  - call this module, passing a, storing result in a
\$\endgroup\$
1
\$\begingroup\$

Roj, 19 bytes

while(1)do out""end

This produces output because of the [out]: buffer.

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

T-SQL, 15 bytes

a:PRINT 0GOTO a

6 years and 10 pages of answers, and no SQL versions yet.

Notes:

  • PRINT is 1 byte less than a SELECT.
  • A label with a goto is 1 byte shorter than the shortest WHILE statement: WHILE 1=1PRINT 1 (you can't just do WHILE 1 in SQL)
  • Depending on the version of SQL Management Studio, the "messages" pane (which displays the result of PRINT statements), might not immediately refresh; this is a client-side UI issue only.
\$\endgroup\$
2
  • \$\begingroup\$ Seriously? 0goto? yuck. \$\endgroup\$ Jun 17, 2020 at 0:08
  • 1
    \$\begingroup\$ @Sapphire_Brick You take the bytes where you can get them :) \$\endgroup\$
    – BradC
    Jun 17, 2020 at 13:29
1
\$\begingroup\$

Go, 59 characters

package main
import "fmt"
func main(){for{fmt.Print(0)}}
\$\endgroup\$
1
\$\begingroup\$

International Phonetic Esoteric Language, 6 bytes

10ɑeoɒ

Will print 0 with newlines forever.

Explanation:

10ɑeoɒ
10     (push the bounds for the loop: from 0 to 1)
  ɑ    (pop the bounds and start the loop)
   e   (push the current index)
    o  (print)
     ɒ (check if index < start. 0 < 1, so loop)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 11 9 characters

loop{p:p}

Old answer:

p 0 while 1
\$\endgroup\$
1
\$\begingroup\$

Rockstar, 13 bytes

while 1 say 1

Try it here (Code will need to be pasted in)

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

ARM Thumb, Linux syscalls only, 12 bytes

Raw machine code:

2704 2001 4669 2201 df00 e7f9

Assembly:

        .text
        .globl _start
        .thumb
        .thumb_func
_start:
        movs    r7, #4    @ write syscall
        movs    r0, #1    @ STDOUT_FILENO
        mov     r1, sp    @ sp is a valid pointer. May not be legible, but...
        movs    r2, #1    @ one byte is fine
        svc     #0        @ syscall: write(STDOUT_FILENO, sp, 1)
        b       _start    @ loop forever

ARM Thumb, with libc, 6 bytes

Raw machine code (f7ff fffe is an unlinked libc call):

f7ff fffe e7fc

Assembly:

        .text
        .globl main
        .thumb
        .thumb_func
@ Note that putchar returns the char it put, so we do this:
@ for (;;) putchar(argc);
main:
        bl      putchar @ putchar(argc)
        b       main    @ Loop forever

Fun fact: These programs do the exact same thing, printing argc % 256 in binary infinitely, since sp[0] at _start is also argc.

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

Befunge-93, 1 byte

'

Try it online!

'  # full program
'  # invalid command, NOP but raises warning
\$\endgroup\$
1
\$\begingroup\$

Duocentehexaquinquagesimal, 2 bytes

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Did you make this language? \$\endgroup\$ Apr 14, 2021 at 20:14
  • \$\begingroup\$ @Redwolf Yes I did. Read the linked Esolang page for more detail about how the language works. :) \$\endgroup\$
    – Makonede
    Apr 14, 2021 at 20:15
1
\$\begingroup\$

Pushy, 3 bytes

3[_

Explanation:

3 \ pushes 3 to the stack

[ \ starts an infinite loop

_ \ prints out the contents of the stack
\$\endgroup\$
0
1
\$\begingroup\$

Mascarpone, 6.875 bytes

['@.:!]v*:!

Try It Online!

Explanation:

[     ]     // push a string that will define an operation
 '@.        // push the symbol '@ and output it (popping it in the process)
    :!      // duplicate the top of the stack and execute it
       v*   // push a function/operation that does the above
         :! // duplicate the operation, and execute it.

The operation duplicates and executes itself tail-recursively, generating the infinite output.

5 bits are sufficient for the characters used by this program, thus the total size is 55 bits, or 6.875 bytes

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

INTERCAL, 26 bytes

DOCOMEFROM#1(1)DOREADOUT#0

Try it online!

This works for both C-INTERCAL and CLC-INTERCAL.

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

RETURN, 62 bytes

(())(()()()()()()()()()()()()()()()()())((()()()()()()()()()))

Try it online!

Explanation:

(()) Add 1
(()()()()()()()()()()()()()()()()()) While nonzero repeat what's in the next group of brackets
(
  (()()()()()()()()()) Put character
)
\$\endgroup\$
1
\$\begingroup\$

KonamiCode, 15 bytes

v(^)L(^)<<<B(^)

Explanation:

v(^) [Writes 1 to memory address 1.]
L(^) [A loop marker]
<<<  [Output the current memory address as a number]
B(^) [A reverse-jump instruction, ending the loop.]

Note: We can't use 0 for the first instruction becuase the comparison buffer is set to 0 by default. This means that the reverse-jump's condition will be fulfilled and the loop will immediately exit. However, we could also replace this instruction with S(^) and have the program run the same way.

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

APOL, 8 bytes

w(T p(0))

I'm not sure if this could reasonably be made smaller.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I like how, without knowing the lang, i can clearly tell what these commands stand for \$\endgroup\$ Feb 4, 2022 at 21:20
1
\$\begingroup\$

Headass, 2 bytes

PE

Try it here! copy and paste this: srun("PE")

Prints an infinite stream of 0s and newlines.

P   Print the value of the current register (initialized at 0) + a newline
 E  Go to code block determined by value at current register,
    Block 0 = return to start.

Headascii, 2 bytes

!E

Try it here! copy and paste this: erun("!E")

Similar to the last one, but instead of printing 0s and newlines, it just prints newlines.

!   Print the value of the string register (initially empty) + a newline
 E  Go to code block determined by value at current register,
    Block 0 = return to start.
\$\endgroup\$
1
  • \$\begingroup\$ you can, of course, infinite loop in 1 byte with E but this produces no output. submitted this because im bored and i think langs should compete in as many challenges as possible \$\endgroup\$ Feb 5, 2022 at 6:36
1
\$\begingroup\$

Python 3, 18 15 bytes

Thanks to DLosc for -3 bytes!

while 1:print()
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can save 3 bytes by using while 1 instead of while True. \$\endgroup\$
    – DLosc
    Feb 4, 2022 at 23:20
1
\$\begingroup\$

Appleseed, 26 bytes

(def start!(q(_(print!(0to

Try it online!

Explanation

(def start!                 ; Define the start! event handler as
           (q               ; A function
             (_             ; That takes any number of arguments
               (print!      ; And outputs
                      (0to  ; The infinite list of all nonnegative integers
\$\endgroup\$
1
\$\begingroup\$

rusty_deque, 17 bytes

1~{dup~ow~}~loop~

Forever print 1s to the screen.

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

BitCycle, 10 bytes

 !
 ~<
1A^

Outputs an infinite bit stream of alternating 1s and 0s, or, with the -u or -U flags, an infinite sequence of 1s.

Try it online! or with an online visualizer

\$\endgroup\$
1
  • \$\begingroup\$ 8 \$\endgroup\$
    – emanresu A
    Jun 10, 2022 at 1:46
1
\$\begingroup\$

Common Lisp, 15 bytes

(loop(princ'x))

Try it online!

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

A0A0, 43 39 bytes

A0A0
A0C3G1G1A0
A0O0A0
A0A1G-3G-3A0
G-3

Prints 0 forever. This uses a classic infinite loop which was created by the inventor of the language. Considering the only important thing about this code is that it runs infinitely, I'll use this post as an explanation of this looping construct, since it's the basis for all loops.

Loops are generally not really difficult in programming languages, even those which only have goto instructions and not proper looping constructs. A0A0 has goto instructions, so what's the problem? A0A0 is a self-modifying language which deletes the instruciton after it executes it. So if we were to use a goto to form a loop, we would simply end up at nothing and the program halts. This is not what we want.

Thankfully, the language contains instructions to modify its own code. There are two instructions that do this. A# and C# where # is a placeholder for an integer. A# appends the current line of instructions (excluding itself) to the line # positions below. C# clears the line # positions below this line. Since all integers are allowed, negative numbers can be used to go to lines above and 0 can be used to stay on the same line.

Let's take a look at the basis of the loop.

A0 A0
A0 C3 G1 G1 A0
A0 instructions here A0
A0 A1 G-3 G-3 A0
G-3

The first thing we do when entering the loop is go past four A0 instructions which append their own lines to themselves. Remember that it excludes itself, so this first A0 is not copied. This would be an issue on subsequent iterations, which is why at the very end of the loop, there is also an A0. This means that this A0 at the end is copied, which preserves the ability to copy the loop infinite amount of times.

You'll notice that the last line does not have any kind of append instruction. This is necessary, because we must at some point go back up and we can't go up if we have an A#. But if we never place a G-3 there again, we eventually will end up there and, again get stuck since there's no instruction there. This is what line 4 is for. It contains the following:

A1 G-3 G-3

It places two G-3 in line lower - at line 5. This preserves the ability for us to keep jumping back up forever. However, this line contains an A0 at the end. And during exeuciton, it may contain many more instructions we don't want. If we execute anything that is not G-3, we go to line 6 and left the loop. This is where line 2 comes into play. It contains a C3 instruction, which clears the line three lines lower. This is line 5. Because the instructions are aligned properly, this means that the line is cleared the moment we would hit an instruction that is not G-3. And since at the same point there will be another A1 G-3 G-3 on line 4, this now empty line will get two G-3 instructions again, just in time.

You may observe that, since there are also G-3 instructions on line 4, we may sometimes jump up to line 1 instead of line 2. And this is where line 1 comes into play. It only contains A0 A0. This will repeat itself infinitely on that line and acts as an infinite no-op. After executing a single instruction, the program counter automatically moves to the next line and we're back on line 2 again.

And all of this code gives us freedom on line 3. We can put whatever instructions we want in there, as long as these instructions don't modify the loop. We can even have multiple lines in the middle, as long as you change the G-3 to the appropriate amount.

Edit: Optimized by 4 bytes. It turns out that there is no minimum requirement of 3 instructions for the loop, allowing us to drop 2 instructions, totalling 4 bytes.

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

Regenerate -a, 1 byte

*

Attempt This Online!

Prints newlines forever :P it's finding and printing every possible match for at least 0 nothings, delimited with newlines. Surprised this isn't a syntax error.

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

BitCycle, 6 bytes

!~<
1^

Inspired by emanresu A's suggested golf to des54321's answer. Similarly to that answer, this outputs alternating 0s and 1s.

Try it here!

Explanation

The initial 1 bit is directed north into the dupneg (~). A negated copy (0) is sent west to the sink (!) and is output. A non-negated copy is sent east, whereupon it is immediately directed west again back into the dupneg. Now the original copy is sent north and leaves the playfield, while the negated copy is sent south, redirected north again, and the cycle continues forever (with the bit changing parity each time).

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

Fuzzy Octo Guacamole, 3 bytes

(1)

Infinitely outputs 1.

Due to weird implicit output and recursive loops (it executes the code inside the loop using recursion), 1 is outputted by the implicit output.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Since FOG is newer than this challenge, you should state that in your answer. \$\endgroup\$
    – Dennis
    Mar 21, 2016 at 3:39
  • \$\begingroup\$ @Dennis thanks for catching that. \$\endgroup\$
    – Riker
    Mar 21, 2016 at 16:06
1
\$\begingroup\$

Quetzalcoatl, 14 chars

while 1: ::' '

This is for an older version of Quetzalcoatl.

\$\endgroup\$
1
  • \$\begingroup\$ The latest revision of your answers makes it identical to this answer. \$\endgroup\$
    – Dennis
    May 6, 2016 at 15:41
1
\$\begingroup\$

Intcode, 9 bytes

4,0,5,0,1

Outputs "4" an infinite number of times.

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

Pip, 4 bytes

W1P1

Prints the number 1 with a trailing newline over and over. Attempt This Online!

This is a simple loop; in pseudocode,

while 1:
  print 1

Almost any expression can be printed instead of 1. For example, W1Pr prints random floats between 0 and 1 forever.

A more interesting option, also 4 bytes:

P*,u

This constructs an infinite range starting at 0 (,u) and then prints each element of it (P*). The result is an infinite loop that outputs the natural numbers one at a time. Attempt This Online!

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

Trilangle, unbounded memory, 3 bytes

'1!

Push a 1 to the stack, print it, repeat.

Trilangle, 4 bytes

'1,!

Push a 1 to the stack, print it, pop it off, repeat.

Trilangle -w, 1 + 2 = 3 bytes

,

When run with the -w flag or in the online interpreter, popping from an empty stack prints a warning.

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