Shortest code to produce infinite output

  • 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>




    expression or whole program?

    All answers disqualified because at some point the Earth will be swallowed by the sun, and at some point the universe will die :P

    Does "infinite until your computer crashes" count? <_<

    @lzkata yes, it does.

    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.

    @DampeS8N In Piet, pixels count as characters. Sorry.

    I think the question should make it clear whether recursive solutions are allowed or not. Because they will crash at some point. I see some recursive solutions here.

    @microbian That would disciminate languages that does not have any other looping construct. Some implementations of a language can have TCO and I'd say it would be ok if that implementation is mentioned in the answer rather than disqualifying it.

    @tbodt but with a compression scheme optimized for very tiny images, it could end up taking less data than the text.

    @Izkata So any answer that crashes your computer is also allowed :D

    @Doorknob So really, the challenge is to produce infinite output in a **finite** amount of time. Sounds easy enough.

    Perhaps "theoretically infinite" is a better term.

    @dorukayhan I would say so.

    @AJMansfield If your Piet interpreter reads compressed images, then by all means your byte count can be shorter. I think standard Piet interpreters have to read the data as a matrix though, so really the number of bytes is the shortest (by bytes) representation that the interpreter/compiler understands.

    @Doorknob The consciousness of AC encompassed all of what had once been a Universe and brooded over what was now Chaos. Step by step, it must be done. And AC said, "LET THERE BE LIGHT!"

  • Befunge (1)



    .


    Outputs 0 forever. It works because in Befunge lines wrap around, and you get 0 if you pop from the empty stack.


    I think we have a winner.

    I *swear* I had a Java class around here somewhere that was shorter. +1

    This one dot has now gotten me more reputation than my next two answers combined.

    I just submitted a solution that uses 100% less characters than this solution AND prints an infinite amount of Dollars. That has to count for something? :)

    @BenjaminPodszun: if you code in Piet you have to count pixels instead of characters, sorry

    Aw... That's not fair :) Let's do it like this: I reduce the size of that image by a couple pixels and we display it very small.. So that it looks like a dot? And call it even? :)

    Why is this not the accepted answer?

    Aw, you beat me to it

    Hole in one! :)

  • x86 .COM Executable, 7



    in hex:



    b8 21 0e cd 10 eb fc





    The others need kilobytes or more of system libraries and runtime. Back to basics:



    $ echo -ne '\xb8!\xe\xcd\x10\xeb\xfc' > A.COM
    $ dosbox A.COM


    output



    You can change the 2nd byte (0x21, or !) to change the output.



    Uses a BIOS interrupt for output; doesn't need DOS, but I didn't have QEMU set up.






    Explanation



    The machine code corresponds with the following assembly:



            mov     ax, 0x0e21
    again: int 0x10
    jmp again


    The output is all in the int call -- per this reference, int 0x10 with 0x0e in AH will print the byte in AL to the screen.



    Knowing that register AX is a 16-bit word comprised of AH in the high byte and AL in the low byte, we can save an extra load (and thereby a byte in the machine code) by loading them together.


    Wow. That's really good

    low machine code length should be worth more points than high level languages.

    This is the shortest code in real sense.

    I've got it in 6 bytes in PDP-11 machine code, and requires no BIOS (writes directly to the UART tx register).

    6 bytes on an Apple ][ with 6502 as well.

    Change `21` to `41` and it outputs `AAAAAAAAAAAAAAAA`.

    Not only does it work, it works *faster* than all the other entries. :)

    @ThaneBrimhall Which is of course the most important criteria by *far* for an infinite loop... If my computer hangs up, I want it to do it in the most efficient way!

    Not sure to -1 because now my computer won't stop silently screaming at me. Thanks. ;)

    LOL! You talk of how other answers use kilobytes of libraries to support them, then you run your answer in DosBox. (Also, +1.) :-D

    Is it allowed to not give value to AL?

  • Windows Batch file, 2 chars



    %0


    Calls itself infinitely.


    Close second place, man. Well done. I've only ever seen one answer where Batch has won it.

    It's really great!!!

    This is awesome!

    How it works: `%0` doesn't have an `@`, so it would constantly output something like `...> %0`. This might just eat the whole RAM up sometime though, since it is recursive.

    @EriktheOutgolfer *might*? How will this *not* crash at some point due to infinite recursion? I don't see how this could go on for multiple hours

    Actually it will be able to continue indefinitely. Naming a batch file to run without using `call` replaces the prior batch execution with the new one. So this is a tail-call, not recursion.

  • Befunge 98 - 2


    ,"

    Outputs ",,,,,,,,,,," for eternity.


    Actually somewhat clever.

    Thank you. It's strange, the ", version outputs spaces (or ". outputs 32 32 32) using the most common b98 implementation. No idea where it gets it from.

    It's too bad it's buried under 82,395 really crappy answers.

    @tbodt Well, according to the spec, my little program has all of eternity to inch its way up. So... fingers crossed...

    @AndoDaan: ", creates a string of a comma and INT_MAX-2 spaces, and then prints the last space. So the stack will fill itself with a comma INT_MAX-3 spaces, not really efficient.

    @YoYoYonnY Thanks for taking the time to explain what was happening!

    @AndoDaan: Befunge-98 treats any string of consecutive spaces as one (contrary to what YoYoYonnY said), and treats the area "off the map" as spaces. So `",` pushes a comma and a space, then prints the space, repeatedly. In Befunge-93, all the spaces actually are pushed, but then the playfield's only 80 characters wide so it's less inefficient

    @JamesHolderness: Although it's implementation-dependent, I believe it will always either leave a comma or a space on top of the stack. (The interpretation where it prints commas is based on assuming that the lines wrap around as soon as you reach the end of the line, which doesn't seem to contradict the spec but which isn't orthogonal with the rest of the langugae.)

  • sh, 3 bytes



    yes


    outputs y continuously


    Though `yes` is an external binary, and not part of bash (strictly speaking).

    @Bob: That is true, but this requirement wasn't specified in the question.

    In bash, (almost) everything is an external binary.

    That would also mean that if I suppose I have an alias y=yes, then y in one char would work. After all, the above example also implies that "yes" is properly in the $PATH.

    Normally you don't have an alias `y=yes` and if your system isn't esoteric you can assume that `yes` is in your `$PATH` (see GNU Core Utilities). With your logic it is possible to assume weird constellations for every solution.

    Right, but this is not **only** a `bash` capability: I'ts a **POSIX shell** basic part of *core utils*. (Ie: please change title for *sh: 3 chars* ;-)

    It should be completely legitimate to use things like coreutils, wget...

    in busybox shells `yes` is a fully builtin "no forking" function that operates like a builtin ... see http://git.busybox.net/busybox/tree/include/applets.src.h

    As technosaurus says, in busybox it is a builtin, but in sh it isn't so you must say sh+coreutils instead.

  • Haskell: 8 Characters



    Old thread, but a fun one is



     fix show


    in Haskell, it prints



    \\\\\\\\\\\\\....


    forever since it's basically running



     let x = show x in x


    It knows that x is a string, so the first character is ", so this needs to be escaped with \", but know that \ needs to be escaped so \\\x, and so on and so on.


    ... wow. Now I'm wondering if `fix$show` would have been more aesthetic :-)

    Because this requires `import Data.Function`, and because it returns the string rather than printing it, I question the fairness of the character count. Also, the string is actually "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\…".

  • Windows Batch file, 8 characters



    :a
    goto a

    This does not produce infinite output, does it...?

    it does...without @echo off, it prints "goto a" until you close it

    we need mor BAT around here

    I count ten characters: `:a\r\ngoto a` because Windows uses CRLF

    @WChargin I just checked using a hex editor. `3a 61 0a 67 6f 74 6f 20` works on my windows 8... :P

    @YoYoYonnY are you missing a `61` at the end there?

    Probably, my hex editor isnt the greatest

    It's 10 bytes, literally. Also, if someone has turned off echo before executing this it will output nothing.

    Does `%0` eventually halt?

  • Java, 54 characters



    Best attempt in Java:



    class A{static{for(int i=0;i<1;)System.out.print(1);}}

    Functional, complies with the rules, golfed. Wondering why it was downvoted.

    Does java forbid the classic `for(;;)` pattern? What about `while(true)`?

    @CodesInChaos, Java doesn't normally disallow those patterns and normally I would consider them cleaner and more idiomatic. Here though we are trying to sneak an infinite loop past the compiler (those forms cause the compiler to complain about the static {} block not exiting).

    class A{static{while(0<1)System.out.print(1);}}

    @psgivens: `javac` 1.7.0_51 rejects that as non-terminating.

    @Mechanicalsnail Yes, static blocks can't execute alone since java 7.

    +1 for the idea; stupidly therea re no more ways to shave off bytes :(

    In Java 8 you can do `for(;;)`

    @OliverNi But you can't use the `static` block.

  • Brainfuck, 4



    +[.]


    Alternatively,



    -[.]

    Neither `\x01` nor `\xFF` are printable characters. I guess that wasn't a requirement, though :-/

    A way to make it output printable characters would be to replace the + with , It does mean it requires an input though

  • Bash, 4 bytes


    $0&$

    Outputs ./forever2.sh: line 1: $: command not found continuously.


    Because $0 is backgrounded, each parent dies after the invalid command $, and so stack/memory is not eaten up, and this should continue indefinitely.


    Strangely the output gets slower and slower over time. top reports that system CPU usage is close to 100%, but there are memory- or CPU-hog processes. Presumably some resource allocation in the kernel gets less and less efficient.


    maybe a behavior of stdout or similar...

    freeing resources is competing with forking processes/ To stop it you cannot kill the process as the PID changes only way to stop it is to rename the script so `$0` fails.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used