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

Motorola MC14500B Machine Code, 1.5 bytes

1BC 

Explanation

1 # store data in register R B # output R C # JMP to the start of the program 
\$\endgroup\$
1
\$\begingroup\$

!@#$%^&*()_+, 4 bytes

^(?) 

Try it online!

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

Zsh, 3 bytes

yes 

Try it online!

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

2let, 7 bytes

aa ag 1 

prints "Hello World!" infinitely

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

How dare you fuck the brain, 7 4 3 bytes

ING 

Try it online

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

AWK, 14 bytes

{for(;;)print} 

Try it online!

The default output for AWK is your input, so pass this any string and it prints it (on a new line) forever.

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

BASIC, 20 13 bytes

1PRINT1:GOTO1 

This has probably been done before

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

jbasher2, 29 bytes

while 1 < 2 output 1 endwhile 
\$\endgroup\$
1
\$\begingroup\$

Bespoke, 38 bytes

PROGRAM INQUIRY tiny:Y proper:Y good:N 

Outputs a series of 1s forever.

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

SAKO, 23 bytes

1)LINIA SKOCZDO1 KONIEC 

Basically the same as this infinite loop solution, but additionally prints newlines.

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

'Python' is not recognised, 8 bytes

j11|j1 

Try it online

\$\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\$ Commented 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\$ Commented Jan 29, 2014 at 23:09
  • 2
    \$\begingroup\$ The link in the title gives a 404 error. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Feb 4, 2014 at 16:41
  • 1
    \$\begingroup\$ @Timtech for(;;)alert(1) is even shorter \$\endgroup\$ Commented Nov 20, 2014 at 22:14
  • \$\begingroup\$ @user2428118 Yep, I've known that since then though. I must learn more all the time :) \$\endgroup\$ Commented 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\$ Commented Dec 2, 2016 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\$ Commented Nov 29, 2013 at 19:20
  • \$\begingroup\$ Also your "for some reason" comment is because print doesn't flush() \$\endgroup\$ Commented Nov 29, 2013 at 19:20
  • \$\begingroup\$ @Sparkis Oh, thank you. Does that remove the characters from the output stream onto the screen? \$\endgroup\$ Commented Nov 29, 2013 at 21:44
  • 2
    \$\begingroup\$ For java, I count everything, including imports, class declaration, and method declaration. \$\endgroup\$ Commented Dec 13, 2013 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\$ Commented Jan 29, 2014 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\$ Commented Jun 21, 2014 at 19:21
  • \$\begingroup\$ Couldn't you also remove public, since methods are public by default? \$\endgroup\$ Commented Mar 5, 2015 at 1:56
  • \$\begingroup\$ @mbomb007 public is needed to make the code executable. \$\endgroup\$ Commented May 28, 2016 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\$ Commented Jul 3, 2014 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\$ Commented Jul 11, 2014 at 18:29
  • \$\begingroup\$ I'm sorry, I'm new to this :) does that include , returns , spaces and tabs ? \$\endgroup\$ Commented Jul 11, 2014 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\$ Commented Jul 20, 2014 at 8:29
  • 1
    \$\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\$ Commented Oct 29, 2015 at 11:14
0
\$\begingroup\$

Groovy - 15 chars

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

Clojure - 18 15 chars

(while 1(pr 1)) 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ (while 1(prn)) prints infinite newlines for 14 bytes. \$\endgroup\$ Commented Mar 31, 2022 at 4:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.