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
\$\begingroup\$

A0A0, 43 bytes

A0A0
A0C3G1G1A0
A0V0O0S1A0
A0A1G-3G-3A0
G-3

Prints 0, 1, 2, up to infinity forever. This wasn't required for the challenge, but the standard loop needs at least three instructions to work and doing this fits in three instructions (the three instructions are also as small as possible). This is a bit more interesting than printing the same number every time. (There are other patterns that would have worked in the same amount of bytes: 1, 2, 4, 8, ...; 0, -1, -2, -3, ...; 1, 3, 5, 7, ...; etc.)

V0O0S1
V0     ; operand, defaults to zero
  O0   ; outputs operand
    S1 ; increments operand

All other instructions are just there to form an infinite loop.

\$\endgroup\$
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\$
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 '14 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 '14 at 23:09
  • 2
    \$\begingroup\$ The link in the title gives a 404 error. \$\endgroup\$
    – Cyoce
    Jan 31 '16 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 '20 at 17:07
0
\$\begingroup\$

Windows PowerShell (12)

for(;;){'-'}
\$\endgroup\$
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 '20 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 '14 at 16:41
  • 1
    \$\begingroup\$ @Timtech for(;;)alert(1) is even shorter \$\endgroup\$ Nov 20 '14 at 22:14
  • \$\begingroup\$ @user2428118 Yep, I've known that since then though. I must learn more all the time :) \$\endgroup\$
    – Timtech
    Nov 21 '14 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 '16 at 18:20
0
\$\begingroup\$

Groovy, 14

for(;;)print 1

from the command line: groovy -e "for(;;)print 1"

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

JAVA - 72 chars:

counting everything:

class T{public static void main(String[]a){for(;;)System.out.print(0);}}

Obviously, Java was not built for code golfing.

\$\endgroup\$
11
  • \$\begingroup\$ Assuming you can import anything, import static java.lang.System.out and you can reduce your "actual code" by 7 characters. \$\endgroup\$
    – Colton
    Nov 29 '13 at 19:20
  • \$\begingroup\$ Also your "for some reason" comment is because print doesn't flush() \$\endgroup\$
    – Colton
    Nov 29 '13 at 19:20
  • \$\begingroup\$ @Sparkis Oh, thank you. Does that remove the characters from the output stream onto the screen? \$\endgroup\$
    – user10766
    Nov 29 '13 at 21:44
  • 2
    \$\begingroup\$ For java, I count everything, including imports, class declaration, and method declaration. \$\endgroup\$
    – tbodt
    Dec 13 '13 at 0:09
  • 2
    \$\begingroup\$ @user2509848 When the buffer gets full it will start to print eventually. It didn't take many seconds for me. \$\endgroup\$
    – Sylwester
    Jan 29 '14 at 22:28
0
\$\begingroup\$

Java, 74 characters

Since Java 7 and later versions seeks for main function at first instead of static block; I think this would be the best option to print infinite loops with small code. For your reference: static block won't execute in Java 7.

class I{public static void main(String[]a){for(;;)System.out.println(1);}}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ For Java 7 and later, this is indeed what you'd need to do. Therefore, I recommend that you add #Java 7+, characterCountOfYourProgram to the beginning of your post, then golf the rest some more. Also, instead of using `s around your code, just add four spaces before the line. Here are some golfing tricks: change the class's name to 1 char, perhaps I. Change the String args[] to String[]a. Change the System.out.println to System.out.print. \$\endgroup\$
    – Justin
    Jun 21 '14 at 19:21
  • \$\begingroup\$ Couldn't you also remove public, since methods are public by default? \$\endgroup\$
    – mbomb007
    Mar 5 '15 at 1:56
  • \$\begingroup\$ @mbomb007 public is needed to make the code executable. \$\endgroup\$
    – dorukayhan
    May 28 '16 at 15:50
0
\$\begingroup\$

C++ 29

main(){while(1)std::cout<<1;}
\$\endgroup\$
1
  • 3
    \$\begingroup\$ The shortest way to do an infinite loop in C/C++ is for(;;). If the condition is missing in a for loop, it becomes infinite. \$\endgroup\$
    – tbodt
    Jul 3 '14 at 23:27
0
\$\begingroup\$

SCALA 18

while(1>0)print(1)
\$\endgroup\$
0
\$\begingroup\$

AppleScript:

repeat until 1 < 0
    say "hi"
end repeat
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Because the question is a code-golf, could you please include the character count of your code? \$\endgroup\$
    – ProgramFOX
    Jul 11 '14 at 18:29
  • \$\begingroup\$ I'm sorry, I'm new to this :) does that include , returns , spaces and tabs ? \$\endgroup\$
    – mcgrailm
    Jul 11 '14 at 18:39
  • \$\begingroup\$ Yes, it does. If you have written this on Windows, then a word count application might count a newline as 2 characters (\r and \n), but here, newlines count just as one character. \$\endgroup\$
    – ProgramFOX
    Jul 20 '14 at 8:29
  • \$\begingroup\$ You can shorten this a lot - instead of using end repeat, you can use end. You can shorten say "hi" to say"hi", or, using a different method of output entirely towards the Messages tab, you can use log"". You do not need the tab before this. You can remove the until 1 < 0 statement, as repeat is infinite by default. After all these suggestions, you shave off 26 bytes of code. \$\endgroup\$ Oct 29 '15 at 11:14
0
\$\begingroup\$

Groovy - 15 chars

for(;;)print 1
\$\endgroup\$
0
\$\begingroup\$

Clojure - 18 15 chars

(while 1(pr 1))
\$\endgroup\$
0
\$\begingroup\$

CoffeeScript, 14

Pretty unoriginal and similar to JavaScript:

alert 6while 1
\$\endgroup\$
1
  • \$\begingroup\$ There is a loop keyword, this could be shortened down to loop alert 1 \$\endgroup\$
    – eosterberg
    Apr 11 '15 at 16:03
0
\$\begingroup\$

PHP: 22 chars

<?PHP while(true){print(0);} ?>

My first golf, hope I did it right :)
It will hang your browser if you do it on a PHP page, but from the command line it does the expected: prints something until you get bored and stop it.

\$\endgroup\$
2
  • \$\begingroup\$ Because you this is php, you can just put while(true){print(0);} the <?php ... ?> isn't actually php anyway \$\endgroup\$
    – addison
    Sep 1 '14 at 23:43
  • \$\begingroup\$ And the php portion of your answer, while(true){print(0);}, is actually 22 characters. \$\endgroup\$
    – addison
    Sep 1 '14 at 23:45
0
\$\begingroup\$

Chinese, 2 bytes

Prints ÿ continuously.

\$\endgroup\$
4
  • \$\begingroup\$ Isn't that two bytes? \$\endgroup\$ Jan 29 '14 at 15:54
  • \$\begingroup\$ @JanDvorak He didn't specify chars or bytes, and chars is default. \$\endgroup\$
    – Timtech
    Jan 29 '14 at 16:08
  • 5
    \$\begingroup\$ @Timtech According to the code-golf tag: If you use Unicode, byte count should use UTF-8. \$\endgroup\$
    – Sylwester
    Jan 29 '14 at 22:14
  • \$\begingroup\$ @Sylwester Okay \$\endgroup\$
    – Timtech
    Jan 29 '14 at 23:07
0
\$\begingroup\$

I haven't seen Thue used here much, so here we go:

Thue (22 19 bytes)

a::=ab
b::=~1
::=
a
\$\endgroup\$
0
\$\begingroup\$

Cardinal, 3 characters

.%$

Explanation:

.    print active value
 %   start pointers here
  $  jump to x,y = active, inactive value


.%$

Pointers spread out in all cardinal directions from every %
All pointers carry two values, initialized to 0. The upper value is
called the active value, and it can be accessed directly. The lower
value is called inactive value. Inactive values can only be accessed
by flipping both values, switching the active to inactive, and the
inactive to active, or by using instructions like $ that grab the
inactive value directly to e.g. execute a jump.
The < and > below the values are the positional and directional
markers of the pointers. In programs, they instruct pointers to change
the movement direction.

0 0     At this step, the first active value (from the left pointer)
0 0     gets printed in the console, the right pointer is going to
<%>     jump to x,y=0,0

0      The jumped (former left) pointer prints out the next 0
0
>%$

 0
 0     ... travels on ...
.>$

  0
  0
.%>    ... reaches the jump instruction again, going to jump to 0,0 again

0
0
>%$    ... print out the next 0... and so on, to infinity.

http://esolangs.org/wiki/Cardinal

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

Bat, 2 chars

%0

The output is (starting from a blank line, I've added nbsp to make it visible bellow)

 
d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"

d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"

d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"

d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"

d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"

d:\Temp\Supertemp>"D:\Temp\Supertemp\inf.bat"
\$\endgroup\$
2
  • \$\begingroup\$ This is dependent to where you have stored the file, but it will always output. This is an example output. \$\endgroup\$ Oct 6 '16 at 19:02
  • \$\begingroup\$ @EriktheGolfer, yes. \$\endgroup\$
    – Qwertiy
    Oct 6 '16 at 21:34
0
\$\begingroup\$

><> Fish - 2 characters, I'm counting the shortest one.

Thijs beat me to it, but oh well. Here are a couple more ><> answers.

1n
-Infinite 1's, 2 bytes

   >"drlow ,olleH"l  
                  ?  
                  !  
   ^              <  
                  o  

-Infinite "Hello, world", 84 bytes.

Edit: Whoops. It's character count, not bytecount. I also fixed the first answer. Edit 2: Thanks mbomb007 for shaving off 1 character.

\$\endgroup\$
3
  • \$\begingroup\$ Why not just use 1n? \$\endgroup\$
    – mbomb007
    Oct 15 '15 at 15:46
  • \$\begingroup\$ @mbomb007 Whoops, forgot that I could do that. \$\endgroup\$ Oct 15 '15 at 15:49
  • \$\begingroup\$ "n also works. \$\endgroup\$
    – mbomb007
    Oct 15 '15 at 16:01
0
\$\begingroup\$

GoLunar, 4 bytes

This is the equivalent of 5543 zeros in a row in Unary, which is equivalent to +[.] in BrainF***.

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

BotEngine, 1x3=3

Noncompeting as BotEngine is much too recent a language. Anyway, here we go:

>CT

It starts with a right-moving bot on the >. When it reaches the C, it creates a left-moving bot (which starts on the C.) On the next step, the left-moving bot reaches the > and is turned around to move right and the right-moving bot reaches the T, destroying itself and printing TRUE. The step after that, the other bot gets to the C starting the process over again.

>CF also works but prints FALSE instead of TRUE. Using P in this case would print empty lines instead.

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

Javascript ES6, 28 bytes

setInterval`console.log()`

I didn't want to code an infinite for loop like everyone else...

Try it here using anything BUT Firefox.

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

Mouse-2002, 3 bytes

(0!)

prints 0 forever.


Alternatively, for a more interesting and (theoretically) infinite error with no loop, the Ackermann Function in 99 bytes in this language:

"M>"?a:"N>"?b:#Y,a.b.;$Y1%n:2%m:m.0=k:n.0=j:j.k.>[n.1+!|j.k.<[#Y,m.1-,1;!|#Y,m.1-,#Y,n.,n.1-;;!]]]@

Expanded:

"M>" ? a:
"N>" ? b:

1 a:
1 b:

#Y, a. b.;!

$Y 1% n: 2% m: ~ ack
  m. 0 = k: ~ store whether m == 0 in k
  n. 0 = j:
  j. k. > [ ~ if y>x
    n.1+ ~ print it
  | j. k. < [ ~ if x>y
    #Y, m. 1-, 1;
  | #Y, m. 1-, #Y, m.,n. 1-;;!
    ]
   ]
  ]
@
$

Errors recursively, forever, as long as the input was a positive nonzero integer. (result of a bug in the interpreter: the code is correct!)

\$\endgroup\$
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
    Apr 26 '18 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\$ Apr 30 '18 at 14:11
  • \$\begingroup\$ console.log would work \$\endgroup\$
    – mbomb007
    Apr 30 '18 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\$ Apr 30 '18 at 20:08

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.