143
\$\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
  • 138
    \$\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
  • 24
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$
    – Izkata
    Nov 10, 2013 at 1:39
  • 6
    \$\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
  • 10
    \$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ Jul 11, 2014 at 20:11
  • 9
    \$\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

323 Answers 323

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 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 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 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 at 1:46
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\$
0
\$\begingroup\$

q/KDB, 13 chars

while[1;0N!1]

Outputs

1 

forever.

For more information on q/KDB :

Another Solution:

{0N!1}/[0<;1]
\$\endgroup\$
1
  • 1
    \$\begingroup\$ {1}{0N!1}/1 for 11 \$\endgroup\$
    – tmartin
    Jan 30, 2014 at 20:54
0
\$\begingroup\$

Quomplex, 6 4

[*1]

Infinitely outputs 1.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Is there an interpreter or compiler for this language? \$\endgroup\$
    – Sylwester
    Jan 29, 2014 at 23:09
  • 2
    \$\begingroup\$ The link in the title gives a 404 error. \$\endgroup\$
    – Cyoce
    Jan 31, 2016 at 19:59
0
\$\begingroup\$

Bash - 13 bytes

cat /dev/zero

to see run cat -v /dev/zero

Documentation here: /dev/zero and here Purpose of /dev/zero?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Why not w&sh $0? \$\endgroup\$ Jun 17, 2020 at 17:07
0
\$\begingroup\$

Windows PowerShell (12)

for(;;){'-'}
\$\endgroup\$
0
\$\begingroup\$

Clojure

(repeat 1)

Returns a lazy seq of 1's '(1 1 1 1 1 1 1 1 1 1 ...)

\$\endgroup\$
1
  • 3
    \$\begingroup\$ it doesn't output anything \$\endgroup\$
    – cliffroot
    May 23, 2016 at 14:01
0
\$\begingroup\$

Scala REPL (19 characters)

while(true)print(1)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You could save one character by substituting true for 1>0. \$\endgroup\$ Jun 17, 2020 at 14:17
0
\$\begingroup\$

PureBasic - 17 chars

r:
Debug 0
Goto r

A slight variation on the classic BASIC infinite loop :-)

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

JavaScript, 23 characters

while(1)console.log(1);
\$\endgroup\$
3
  • \$\begingroup\$ while(1)alert(1); \$\endgroup\$
    – Timtech
    Feb 4, 2014 at 16:41
  • 1
    \$\begingroup\$ @Timtech for(;;)alert(1) is even shorter \$\endgroup\$ Nov 20, 2014 at 22:14
  • \$\begingroup\$ @user2428118 Yep, I've known that since then though. I must learn more all the time :) \$\endgroup\$
    – Timtech
    Nov 21, 2014 at 12:05
0
\$\begingroup\$

Processing, 26

for(int i=0;i<1;)print(1);

Prints 1 continuously.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might also be able to do for(;;) for an infinite loop and then you can add the print(1); \$\endgroup\$
    – user41805
    Dec 2, 2016 at 18:20

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.