118
\$\begingroup\$

Your task is to create the shortest infinite loop!

The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code might be shorter if no output is given.

Rules

  • Each submission must be a full program.
  • You must create the shortest infinite loop.
  • Even if your program runs out of memory eventually, it is still accepted as long as it is running the whole time from the start to when it runs out of memory. Also when it runs out of memory, it should still not print anything to STDERR.
  • The program must take no input (however, reading from a file is allowed), and should not print anything to STDOUT. Output to a file is also forbidden.
  • The program must not write anything to STDERR.
  • Feel free to use a language (or language version) even if it's newer than this challenge. -Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. :D
  • Submissions are scored in bytes, in an appropriate (pre-existing) encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score - if in doubt, please ask on Meta.
  • This is not about finding the language with the shortest infinite loop program. This is about finding the shortest infinite loop program in every language. Therefore, I will not accept an answer.
  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainf**k-derivatives like Alphuck), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.
  • There should be a website such as Wikipedia, Esolangs, or GitHub for the language. For example, if the language is CJam, then one could link to the site in the header like #[CJam](http://sourceforge.net/p/cjam/wiki/Home/), X bytes.
  • Standard loopholes are not allowed.

(I have taken some of these rules from Martin Büttner's "Hello World" challenge)


Please feel free to post in the comments to tell me how this challenge could be improved.

Catalogue

This is a Stack Snippet which generates both an alphabetical catalogue of the used languages, and an overall leaderboard. To make sure your answer shows up, please start it with this Markdown header:

# Language name, X bytes

Obviously replacing Language name and X bytes with the proper items. If you want to link to the languages' website, use this template, as posted above:

#[Language name](http://link.to/the/language), X bytes

Now, finally, here's the snippet: (Try pressing "Full page" for a better view.)

var QUESTION_ID=59347;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var OVERRIDE_USER=41805;var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=true,comment_page;function answersUrl(index){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(index,answers){return"//api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=false;comment_page=1;getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,});else console.log(body)});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<a>'+lang+'</a>').text();languages[lang]=languages[lang]||{lang:a.language,lang_raw:lang,user:a.user,size:a.size,link:a.link}});var langs=[];for(var lang in languages)if(languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function(a,b){if(a.lang_raw.toLowerCase()>b.lang_raw.toLowerCase())return 1;if(a.lang_raw.toLowerCase()<b.lang_raw.toLowerCase())return-1;return 0});for(var i=0;i<langs.length;++i){var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;width:500px;float:left}#language-list{padding:10px;padding-right:40px;width:500px;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="language-list"> <h2>Shortest Solution 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> <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> <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\$
  • 41
    \$\begingroup\$ I've got to start posting programs with a negative byte count to beat all these empty files! \$\endgroup\$ – CJ Dennis Oct 3 '15 at 4:32
  • 3
    \$\begingroup\$ This challenge is interesting because it brings out lots of 0 byte languages (some of which are NOT esolangs). FWIW, most declarative languages have an implicit infinite loop because declarative languages don't have loops in their syntax (they assume they're running in an infinite loop). Ladder diagrams are perhaps among the oldest such languages. Then you have the Instruction Language (IL), a sort of assembly for PLCs that also assume an infinite loop. ILs, like assembly are different between manufacturers \$\endgroup\$ – slebetman Oct 5 '15 at 9:36
  • \$\begingroup\$ Are programs that read and execute their own source code allowed, or does file I/O break the "must take no input" rule? \$\endgroup\$ – ThisSuitIsBlackNot Oct 6 '15 at 13:05
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Yes, file input is allowed. \$\endgroup\$ – Cows quack Oct 6 '15 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$ – OldBunny2800 Mar 7 '16 at 23:39

450 Answers 450

208
\$\begingroup\$

Befunge, 0 bytes



Yup. A Befunge program exists in a two-dimensional playfield with fixed size which wraps around the edges. With nothing in that space to interfere, the program counter runs in an infinite loop by default :)

\$\endgroup\$
  • 10
    \$\begingroup\$ Aw, I was gonna post this. :/ \$\endgroup\$ – Deusovi Oct 2 '15 at 18:00
  • 5
    \$\begingroup\$ @daniero It may have some relation to this standard loophole.... I personally don't agree with the standard loophole. \$\endgroup\$ – Justin Oct 2 '15 at 20:02
  • 17
    \$\begingroup\$ @Justin That loophole only applies to certain types of challenges. \$\endgroup\$ – Dennis Oct 2 '15 at 20:32
  • 1
    \$\begingroup\$ You can use <pre>&nbsp;</pre> for an empty-looking code block. \$\endgroup\$ – immibis Oct 3 '15 at 6:17
  • \$\begingroup\$ First thing I looked for \$\endgroup\$ – Dylan Madisetti Oct 12 '15 at 5:20
144
\$\begingroup\$

L00P, 0 bytes



This lang was made for looping, and that's just what it'll do...

\$\endgroup\$
  • 14
    \$\begingroup\$ LOL. +1 for choice of language. \$\endgroup\$ – ETHproductions Oct 2 '15 at 18:29
  • 75
    \$\begingroup\$ ... one of these days this lang is gonna loop all over you \$\endgroup\$ – Pål GD Oct 3 '15 at 11:05
  • 3
    \$\begingroup\$ +1 for the musical reference, and congrats on another gold badge! \$\endgroup\$ – Luis Mendo Jul 1 '16 at 23:35
  • \$\begingroup\$ The best reference I have seen \$\endgroup\$ – Christopher Jul 20 '17 at 16:44
129
\$\begingroup\$

C64 Machine Code, 2 Bytes

D0 FE

Branches to itself if the zero flag is not set.

Branches are single-byte offsets from the next instruction location, and 254 is -2 in two's complement... the BNE instruction (D0) takes one byte of memory, and the offset takes a second byte, so branching two bytes back branches back to itself. The zero flag is always cleared when code is loaded into memory.

Note that this is not a recursive subroutine call, so you will never run out of memory. Also note that there is no header, compiler, or executable overhead... it is truly a two-byte program :)

\$\endgroup\$
  • 13
    \$\begingroup\$ Shouldn't this work on any machine with a 6502 / 6510 family processor, not just a C64? Also, what you've written is machine code. The assembly would be BNE -2 \$\endgroup\$ – Level River St Oct 2 '15 at 19:54
  • 51
    \$\begingroup\$ +1 for making an actual program that is small, instead of trying to find the most obscure language that just so happens to have the least characters to represent the structure. \$\endgroup\$ – Knetic Oct 2 '15 at 20:40
  • 1
    \$\begingroup\$ @user45891 EB FE is x86. 6502/6510 doesn't have an unconditional short jump instruction. \$\endgroup\$ – Random832 Oct 2 '15 at 22:21
  • 2
    \$\begingroup\$ Steveverrill, touché, it is machine code, indeed. And yes, I thought more people would recognize the Commodore 64 than the 65xx family in general :) The VIC-20 used the 6502 and would have been able to run this. So, technically, would my 1541 floppy drive... I vaguely recall being able to reprogram the controller on that. Ah, I still miss my C64 :) \$\endgroup\$ – James King Oct 3 '15 at 17:32
  • 7
    \$\begingroup\$ Back in the day, a particularly nasty "trick" (imho vandalism) that customers would play on unsuspecting computer store owners was to place two bytes akin to this but for the X86, at the beginning of the bootloader, using DOS' debug. This would effectively brick the machine, and most shop staff lacked the nous to know it wasn't just a dead drive. \$\endgroup\$ – Dewi Morgan Oct 11 '15 at 6:56
102
\$\begingroup\$

Brainfuck, 3 bytes

+[]

Never decrement: never end.

\$\endgroup\$
  • 59
    \$\begingroup\$ It's funny when BF beats most other answers. \$\endgroup\$ – Rohcana Oct 3 '15 at 15:24
  • \$\begingroup\$ I had to think of this when I saw the question on SE's start page. \$\endgroup\$ – s3lph Oct 3 '15 at 16:03
  • 1
    \$\begingroup\$ My first thought was BF! :D It ended shorter than I thought. \$\endgroup\$ – André Christoffer Andersen Oct 3 '15 at 19:16
  • 1
    \$\begingroup\$ Alternative, still same bytes: -[] \$\endgroup\$ – Unihedron Jan 19 '18 at 1:08
  • 1
    \$\begingroup\$ I bet there's an interpreter somewhere where ] would work \$\endgroup\$ – 12Me21 Mar 25 at 21:55
100
\$\begingroup\$

///, 3 bytes

///

Any bonus points for using the language's name as source code?

\$\endgroup\$
  • 71
    \$\begingroup\$ I'm honestly surprised that the link actually works, seeing as how it ends with /// ;) \$\endgroup\$ – ETHproductions Oct 4 '15 at 1:11
  • \$\begingroup\$ @ETHproductions That is rather surprising, but if one thinks about it a bit more, it makes some sense; it's just the URL esolangs.org wiki _ _ _ _ \$\endgroup\$ – HyperNeutrino May 1 '16 at 22:33
  • 11
    \$\begingroup\$ @AlexL. Not entirely. The web server sees a GET request to /wiki////. While that intended to be a path, the server can do with that information whatever it wants. \$\endgroup\$ – Dennis May 2 '16 at 0:54
72
\$\begingroup\$

Java, 53 bytes

class A{public static void main(String[]a){for(;;);}}

Yay full program requirement!

\$\endgroup\$
  • 78
    \$\begingroup\$ God is that verbose... \$\endgroup\$ – mınxomaτ Oct 2 '15 at 17:29
  • 4
    \$\begingroup\$ @minxomat Yep, 45 bytes for an empty main :( \$\endgroup\$ – Geobits Oct 2 '15 at 17:30
  • 3
    \$\begingroup\$ @minxomat gotta love that OOP \$\endgroup\$ – Luke Oct 6 '15 at 15:39
  • 52
    \$\begingroup\$ That ain't OO. Putting code in a class doesn't make it OO any more than putting a rock in the microwave makes it food. \$\endgroup\$ – imallett Oct 11 '15 at 5:18
  • 5
    \$\begingroup\$ @imallett Who says rocks aren't food? \$\endgroup\$ – Geobits Nov 16 '15 at 1:36
55
\$\begingroup\$

Prolog, 5 bytes

a:-a.

In order to know if predicate a is true, you only need to check if predicate a is true.

You need to load the file and execute a, both with a command-line arguments. Note that the recursion is likely to be optimized as an infinite loop and shouldn't blow the stack.

Also, this looks like a smiley, but I am not sure how to call it. The dot looks like saliva, so maybe "vegetative state", or "Infiurated programmer with curly hair". Suggestions are welcome.

\$\endgroup\$
  • 3
    \$\begingroup\$ Check if a is true by checking if a is true by checking if a is true by checking...yay recursion! \$\endgroup\$ – kirbyfan64sos Oct 8 '15 at 20:20
48
\$\begingroup\$

Haskell, 9 bytes

Infinite recursion of the main function. Should get compiled to a loop due to tail recursion optimization.

main=main
\$\endgroup\$
  • 1
    \$\begingroup\$ It compiles, but if run the runtime system detects the loop and throws the <<loop>> exception - at least with ghc. Maybe some other compiler behaves differently. \$\endgroup\$ – nimi Oct 3 '15 at 10:23
  • 4
    \$\begingroup\$ Doing runhaskell Loop.hs happily executed it for several minutes on my machine. So, it's at least runnable by an interpreter. I think the <<loop>> runtime exception that ghc throws is purely an implementation detail of the runtime and not part of the Haskell language as specified in any of the Haskell Reports. \$\endgroup\$ – GrantS Oct 3 '15 at 17:29
  • \$\begingroup\$ It's an edge case. runhaskell happily accepts the code but it doesn't loop. It does nothing. However, the challange only requires to create the loop, not to execute it, so I guess it's fine. Have a +1. \$\endgroup\$ – nimi Oct 4 '15 at 14:45
  • \$\begingroup\$ @GrantS: the Haskell language has no such notion as a “loop”; definitions like main = main are semantically in the same bucket as undefined or error "<<loop>>": bottom values ⟂. \$\endgroup\$ – ceased to turn counterclockwis Oct 9 '15 at 18:52
  • 1
    \$\begingroup\$ I think the shortest working haskell loop would be: main=main>>main \$\endgroup\$ – lovasoa Nov 12 '16 at 14:58
40
\$\begingroup\$

Python, 9 bytes

Works in both 2 and 3.

while 1:0

Shortened by @FryAmTheEggman

\$\endgroup\$
  • \$\begingroup\$ note, this does produce output when ran in the Python interpreter \$\endgroup\$ – Chris_Rands Aug 8 '17 at 15:16
  • 2
    \$\begingroup\$ @Chris_Rands Only in the REPL. \$\endgroup\$ – HyperNeutrino Oct 12 '17 at 14:24
37
\$\begingroup\$

x86 ELF executable, 45 bytes

Unlike the vast majority of these answers, this is a truly complete program, as in a free-standing executable program.

00000000: 7f45 4c46 0100 0000 0000 0000 0000 0100  .ELF............
00000010: 0200 0300 2000 0100 2000 0100 0400 0000  .... ... .......
00000020: ebfe 31c0 40cd 8000 3400 2000 01         ..1.@...4. ..

The guts of the program are at byte 0x20 ebfe, which is featured in another answer as the smallest NASM program. If you assemble that with NASM however, you get an executable with thousands of un-needed bytes. We can get rid of most of them using the technique outlined here. You may note that this program isn't even as big as the ELF header! This bit of executable golfing malforms the ELF header and program header so they can occupy the same bytes in the file and inserts our program into some unused bytes within the header. Linux will still happily read the header and start execution at offset 0x20 where it spins forever.

\$\endgroup\$
  • 9
    \$\begingroup\$ .com on DOS would be much shorter :) \$\endgroup\$ – JimmyB Oct 5 '15 at 11:38
  • 1
    \$\begingroup\$ You only get "thousands" of bytes if you let gcc include the startup files. yasm && ld makes executables only about 1k. \$\endgroup\$ – Peter Cordes Oct 5 '15 at 19:58
34
\$\begingroup\$

Perl, 6 bytes

perl -e '{redo}'

From perldoc -f redo:

The redo command restarts the loop block without evaluating the conditional again...Note that a block by itself is semantically identical to a loop that executes once. Thus redo inside such a block will effectively turn it into a looping construct.

I don't see redo too often in production code, but it's great for golf! Compare the above to the shortest equivalents with for, while, and goto:

for(;;){} # 9 bytes
1while 1  # 8 bytes
X:goto X  # 8 bytes
\$\endgroup\$
34
\$\begingroup\$

INTERCAL, 42 18 bytes

(1)DO COME FROM(1)

Idea taken from @flawr's comment.

EDIT: Holy crap, INTERCAL is actually shorter than C#. I don't know if that's ever happened before...

42-byte version

DO(1)NEXT
(1)DO FORGET #1
PLEASE DO(1)NEXT
\$\endgroup\$
  • 12
    \$\begingroup\$ How dare you do a intercal program without comefrom \$\endgroup\$ – flawr Oct 2 '15 at 20:17
  • 1
    \$\begingroup\$ @flawr Fixed. ;) \$\endgroup\$ – kirbyfan64sos Oct 2 '15 at 20:27
  • 4
    \$\begingroup\$ Np, I think we need more Intercal in golfing contests here=) \$\endgroup\$ – flawr Oct 2 '15 at 20:50
  • 16
    \$\begingroup\$ Did you say PLEASE? \$\endgroup\$ – Daniel M. Oct 2 '15 at 22:39
31
\$\begingroup\$

><>, 1 byte

 

A single space will cause ><> to go into an infinite loop of NOPs

Other valid single character programs (with no memory requirements) are as follows:

>^v</\|_#x!"'{}r

In addition, the rules state that your program can run out of memory in which case we can add the following characters to the list of valid 1-byte programs:

01234567890abcdefli
\$\endgroup\$
  • 11
    \$\begingroup\$ Try ` ` for the code block. \$\endgroup\$ – TheNumberOne Oct 2 '15 at 17:48
  • 2
    \$\begingroup\$ Why not just use >? \$\endgroup\$ – mbomb007 Oct 2 '15 at 20:55
  • 4
    \$\begingroup\$ If this were Chem.SE, I would recommend putting the code in MathJax, too. \$\endgroup\$ – hBy2Py Oct 5 '15 at 1:56
  • \$\begingroup\$ Are we sure an empty program wouldn't be an infinite loop? \$\endgroup\$ – Aaron Oct 5 '15 at 9:12
  • 1
    \$\begingroup\$ Get a runtime error trying to execute the empty program: Traceback (most recent call last): File "fish.py", line 493, in <module> instr = interpreter.move() File "fish.py", line 149, in move if self._position[1] > max(self._codebox.keys()): ValueError: max() arg is an empty sequence \$\endgroup\$ – Fongoid Oct 6 '15 at 17:40
29
\$\begingroup\$

C, 15 bytes

main(){main();}

Yes, it's possible to call main() recursively. If you've got a compiler that does tail-call optimization (say, gcc with the -O2 option), it doesn't even segfault: the compiler is smart enough to turn the function call into a goto.

\$\endgroup\$
  • 4
    \$\begingroup\$ Unless you can give me a compiler that does this by default, you would have to add 3 bytes for the -O2 option. \$\endgroup\$ – LegionMammal978 Oct 3 '15 at 0:17
  • 18
    \$\begingroup\$ @LegionMammal978, according to the rules in the challenge, running out of memory is acceptable. That makes the -O2 optimization a "nice-to-have" rather than a requirement. \$\endgroup\$ – Mark Oct 3 '15 at 4:22
  • 1
    \$\begingroup\$ If options to the compiler is allowed (and is not counted for the solution count), so why not change the code to l(){l();} and compile it with the options -Dl=main -O2 \$\endgroup\$ – wendelbsilva Oct 5 '15 at 21:01
  • 1
    \$\begingroup\$ For comparison, the shortest non-recursive proper loop implementation is 16B: main(){for(;;);}. \$\endgroup\$ – Peter Cordes Oct 6 '15 at 21:30
  • 3
    \$\begingroup\$ @wendelbsilva you may nail it even more by changing the code to X (just one byte) and compiling with -DX=main(){main();} \$\endgroup\$ – LeFauve Oct 7 '15 at 12:37
29
\$\begingroup\$

Motorola MC14500B Machine Code, 0.5 0 bytes



Explanation

According to the manual, the system is configured to have a looping control structure. The program counter counts up to its highest value, wraps back around to zero, and counts up again.

\$\endgroup\$
  • 3
    \$\begingroup\$ I believe you have found the ultimate golfing language, at least for small tasks...not even Pyth can get this short. +1 \$\endgroup\$ – ETHproductions Nov 4 '15 at 18:02
  • \$\begingroup\$ Be warned that this shows up as 5 bytes on the scoreboard. \$\endgroup\$ – Addison Crump Nov 18 '15 at 10:31
  • \$\begingroup\$ C is Jump, but where to jump? \$\endgroup\$ – Kishan Kumar Oct 4 '16 at 4:34
  • 3
    \$\begingroup\$ Wow, you really like this language, huh? :P \$\endgroup\$ – MD XF Jun 11 '17 at 0:26
  • \$\begingroup\$ This will work in any machine language where 0 is a NOP, the instruction pointer wraps around, and the program memory doesn't hold any extra data. \$\endgroup\$ – 12Me21 Oct 24 '17 at 17:29
28
\$\begingroup\$

LOLCODE, 24 bytes

IM IN YR X
IM OUTTA YR X
\$\endgroup\$
  • 3
    \$\begingroup\$ In many LOLCODE interpreters, such as the one on repl.it, HAI and KTHXBYE are unnecessary. \$\endgroup\$ – Alex A. Oct 2 '15 at 19:27
  • 1
    \$\begingroup\$ @AlexA. oh, didn't know that. Thanks. \$\endgroup\$ – daniero Oct 2 '15 at 19:29
25
\$\begingroup\$

Labyrinth, 1 byte

"

A labrinth program executes the same instruction over and over again if there are no neighbors. They also won't end until they execute the @ instruction.

\$\endgroup\$
25
\$\begingroup\$

Vim, 7 keystrokes

Open the editor, preferably without any loaded scripts, for instance like like this from the command line: vim -u NONE

qq@qq@q

Vimscript, 15 8 bytes

Add it in a script, or run it directly by punching the colon (:) key first while you're in normal mode

wh1|endw
\$\endgroup\$
  • 1
    \$\begingroup\$ Your vimscript is a little long wh1|endw \$\endgroup\$ – FDinoff Oct 6 '15 at 4:48
  • \$\begingroup\$ Could you explain how this works to someone who's not familiar with Vim? \$\endgroup\$ – iFreilicht Oct 16 '15 at 11:37
  • 7
    \$\begingroup\$ @iFreilicht qq starts recording into the q buffer. @q replays what's in the q buffer (at this point, nothing). q stops recording, and then @q replays whats in the q buffer, which at this point is the keystrokes @q. So @q replays @q replays @q.... also, writing this many q's makes my brain hurt. \$\endgroup\$ – Wayne Werner Dec 28 '15 at 17:04
25
\$\begingroup\$

Retina, 3 bytes

+`0

If given a single file, Retina uses a Count stage, replacing the input with the number of matches found for the given regex. Here, the regex is 0. Now + loops the stage for as long as the result changes from the previous iteration. So what exactly is happening?

  • 0 is matched against the empty input, giving zero matches, so the result is 0. This is different from the input, so we run this again.
  • 0 is matched against the previous output 0, which now gives one match... so the result is 1.
  • 0 is matched against the previous output 1, which fails... so the result is 0.
  • ... you get the idea.

The result of the loop iteration alternates between 0 and 1, which a) ensures that the loop never terminates and b) ensures that we're not running out of memory because the string doesn't grow.

By default, Retina only outputs once the program terminates, so this doesn't print anything (you can change this behaviour by adding a > after the +, which will then print the alternating zeroes and ones).

As of 1.0, Retina actually also has more traditional while-loops, which you could use with a simpler stage (which doesn't change the string all the time), but they actually require more bytes. One option would be:

//+`
\$\endgroup\$
  • 1
    \$\begingroup\$ Wow, I just answered almost the exact thing in rs at the exact same time. \$\endgroup\$ – kirbyfan64sos Oct 2 '15 at 19:12
  • 1
    \$\begingroup\$ @kirbyfan64sos They look very similar, but they actually work quite differently. :) \$\endgroup\$ – Martin Ender Oct 2 '15 at 19:13
  • \$\begingroup\$ Nice job. I was going to try doing the shortest Retina program in Replace mode. I'm curious, can +` match empty input repeatedly? \$\endgroup\$ – mbomb007 Oct 2 '15 at 21:10
  • \$\begingroup\$ @mbomb007 I think that would terminate with a 2 after a few iterations. \$\endgroup\$ – Martin Ender Oct 3 '15 at 5:34
24
\$\begingroup\$

BASIC (QBasic 4.5), 10 5 3 bytes

In the BASIC programming language, RUN is used to start program execution from direct mode, or to start a overlay program from a loader program. - Wikipedia

Edit: This works without a line number in QBasic 4.5, according to @steenbergh

RUN

Here's the first version I posted. Infinite GOTO loop. Also, it's 10 bytes, which is a nice coincidence!

10 GOTO 10
\$\endgroup\$
  • 13
    \$\begingroup\$ Why line number 10? Why not 9? \$\endgroup\$ – recursive Oct 3 '15 at 0:01
  • 25
    \$\begingroup\$ or 8 for 8 bytes?:) \$\endgroup\$ – MickyT Oct 3 '15 at 0:04
  • 3
    \$\begingroup\$ It's not going to win and it's funnier this way. \$\endgroup\$ – CJ Dennis Oct 3 '15 at 4:29
  • 3
    \$\begingroup\$ This could be shorted to: 1 RUN \$\endgroup\$ – TOOGAM Oct 3 '15 at 12:43
  • 2
    \$\begingroup\$ @davidjwest Shortest I can do in Sinclair BASIC is: 1 GOTO SIGNUM PI - since constant ints are stored in memory twice, as shorts, so 1 would be 4 bytes, but SIGNUM and PI both use only one byte each. Ah, the old coding tricks we used to use :D Not sure how much memory line numbers take up, if any, so all I can say is it's 3+(line number storage) bytes. \$\endgroup\$ – Dewi Morgan Oct 11 '15 at 7:06
22
\$\begingroup\$

C#, 38 37 36 bytes

class B{static int Main(){for(;;);}}

For loop with no stopping condition.

The return of main should be an int, but since it'll never reach the end this should compile. (Tested in VS 2015 and 2013, also works in Ideone). Thanks Geobits and MichaelS.

A shorter version, 35 bytes, can be achieved, but prints Process is terminated due to StackOverflowException which I believe violates the third point of not printing anything to stderr. Credit to MichaelB

class B{static int Main()=>Main();}
\$\endgroup\$
  • 1
    \$\begingroup\$ Can you save one by using ; instead of {}? I'd test, but online services don't like infinite loops much. \$\endgroup\$ – Geobits Oct 2 '15 at 17:44
  • \$\begingroup\$ @Geobits Ah, yes. How such simple things slip by. \$\endgroup\$ – Sven Writes Code Oct 2 '15 at 17:52
  • 1
    \$\begingroup\$ It may depend on compiler, but I saved another character by declaring it as int main and it ran without a return statement. I'm using Microsoft Visual C# 2013 Community Version 12.0.31101.00 Update 4. "class A{static int Main(){for(;;);}}" \$\endgroup\$ – MichaelS Oct 3 '15 at 4:40
  • \$\begingroup\$ @MichaelS Thanks! Worked in 2015 VS for me. \$\endgroup\$ – Sven Writes Code Oct 3 '15 at 16:35
  • 1
    \$\begingroup\$ In vs 15, this might be shorter (barely). 35 class B{static int Main()=>Main();} \$\endgroup\$ – Michael B Jan 25 '16 at 20:28
21
\$\begingroup\$

Foo, 3 bytes

(1)

Everyone's favorite programming language! :D

\$\endgroup\$
21
\$\begingroup\$

TIS Node Type T21 Architecture, 6 bytes

A single node with NOP written in it

Tessellated Intelligence System nodes are classified as "processing" or "storage" nodes. Storage nodes simply store and retrieve information, and are irrelevant in this case. Remaining are the processing nodes. Node Type T21, or Basic Execution Node, is the most common and simple (as the name would suggest).

Technically, each node can be thought of as an independent computer. In the case of the T21, it is a computer that has two storage registers (one addressable, one not) and an instruction set of 15 commands. It has enough memory to be programmed with up to 15 instructions. All TIS nodes have four ports connecting them to the topologically adjacent nodes. Reading from a port causes that node to hang until the node on the other end writes to it, and writing to a port hangs until that node reads it.

You might be able to tell by now that TIS nodes were never meant to do much on their own. Together, though, they can be quite powerful... well, for their time. Because of these limitations, it's very rare to see someone use only a single node. In fact, a program that takes input and provides output based on it must use at least three nodes, as TIS systems feed input into the UP port of a node on the top row and take output from the DOWN port of a node on the bottom row. There are three rows, so data must pass through at least three nodes to get to the bottom.

Because of these limitations, TIS nodes are intended to generally be used somewhat like this:

  1. Get input
  2. Do something to it
  3. Pass it on
  4. Return to step 1

Because of this, the limited space for instructions and the fact that nodes simply wait quietly and don't cause trouble when trying to read input that isn't there, a decision was made in their design that makes them very good for this challenge. I'll quote from the TIS-100's reference manual:

After executing the last instruction of the program, execution automatically continues to the first instruction.

Perfect! Infinite loops are default for TIS nodes.

I very nearly answered this question with a 0 byte answer, claiming that an empty node was an infinite loop. However, I researched further. First, the quote above states that the loop occurs after executing the last instruction. Additionally, I tested the implementation. Each node reports a "mode" at all times. It isn't accessible programmatically but it's intended to make debugging easier. Here are the possible modes:

 RUN‌ - I am executing an instruction.
READ - I am reading from a port, waiting for it to be written to.
WRTE - I am writing to a port, waiting for it to be read from.
IDLE - I am doing nothing.

It turns out that, since each node is an individual computer, they are capable of determining whether or not they have instructions to execute. If not, they remain in the IDLE state (likely to save power). As such, I couldn't in good conscience claim that it was "looping"; rather, each node sat quietly, assuming the others were doing something important.

This program that I've submitted is truly an infinite loop, as executing it sets the state of the node to RUN. It is as simple as you would expect, NOP performs No OPeration. Once it's done doing nothing, execution returns to the top of the code: NOP.

If you find it unsatisfying that I'm abusing the T21 architecture to create a loop, I offer an alternate solution at the cost of 2 bytes: JRO 0. JRO means Jump Relative uncOnditionally. Or something, I guess. There's no agreed-upon expanded form of the instructions. Anyway, JRO takes a numeric argument and jumps execution by that amount relative to the current position. For example, JRO 2 skips the instruction that follows it (useful if that instruction is jumped to from somewhere else). JRO 1 jumps forward one instruction, making it a NOP. JRO -1 jumps back one instruction, effectively performing the previous instruction once every two cycles until the program is halted. And, of course, JRO 0 jumps to itself, executing itself forever.

At this point you may be thinking:

Sure, monorail, this all makes sense, but your answer is simply NOP. Why is its score 6 bytes?

Good question, thanks for asking. One may naively think that TIS programs should be counted the same way we count programs in multiple files: the number of bytes in all nodes, plus 1 byte for each additional node after the first. However, the TIS golfing community decided this would be unfair for the simple reason that it ignores some of the information required to recreate solutions. A node's neighbours are very important, and that scoring method gives you positional information for free. Instead, we've adopted the format used by the most common TIS emulator, the confusingly-named TIS-100. (Side note: Please don't name emulators after the system they emulate. It's not clever, it's just annoying and makes everyone have to constantly clarify what they're talking about.) It's very simple: The 12 nodes of a TIS-100 device are numbered, left to right and top to bottom, skipping any storage nodes the emulated system has installed. A node numbered N containing # CODE\n# CODE\n CODE is saved like so:

@N
# CODE
# CODE
# CODE

And so, a node numbered 0 containing NOP is scored according to its representation in this format:

@0
NOP

Six bytes.

As I often include in my answers in visually-interesting languages, you can watch the TIS-100 emulator execute this program on YouTube. Though, considering what this challenge is, I don't know what you expect to see...

\$\endgroup\$
  • \$\begingroup\$ Wow-ee. That was amazing. \$\endgroup\$ – Adrian Zhang Oct 27 '17 at 2:02
  • \$\begingroup\$ I always assumed JRO stood for Jump to Relative Offset. \$\endgroup\$ – BHustus Jan 23 '18 at 12:52
  • \$\begingroup\$ @BHustus i think you're right but i wrote this before i had experience with any other assembly language and i didn't know the lingo \$\endgroup\$ – undergroundmonorail Jan 23 '18 at 12:56
  • \$\begingroup\$ I wish I could +10 this answer. I love TIS-100 \$\endgroup\$ – Shirkam Mar 2 '18 at 12:49
  • \$\begingroup\$ I wrote a TIS emulator of my own, so you can now Try It Online. Don't expect much other than a timeout, though. \$\endgroup\$ – Phlarx Mar 29 '18 at 21:36
20
\$\begingroup\$

Perl, 4 bytes

do$0

Executes itself repeatedly.

\$\endgroup\$
  • 1
    \$\begingroup\$ Very cool. At first I thought this violated the "must take no input" rule since do EXPR reads from a file, which would technically be input, but the OP clarified that file I/O is acceptable. The really cool thing is that unlike the equivalent in Bash, say, this doesn't fork any new processes, so it will continue indefinitely without hitting any memory or process limits. \$\endgroup\$ – ThisSuitIsBlackNot Oct 6 '15 at 16:52
18
\$\begingroup\$

Hexagony, 1 byte

.

I don't know much about this awesome language created by @MartinBüttner, but from what I've seen, this should loop infinitely, as there is no @ to halt the program. . is simply a no-op.

\$\endgroup\$
  • 3
    \$\begingroup\$ Why don't you test it, :D \$\endgroup\$ – Cows quack Oct 3 '15 at 3:23
  • 7
    \$\begingroup\$ I can confirm that this is correct. :) \$\endgroup\$ – Martin Ender Oct 3 '15 at 19:01
  • \$\begingroup\$ @KritixiLithos Because I couldn't find an online interpreter, and I don't prefer to download the interpreter for every language I ever want to test. ;) \$\endgroup\$ – ETHproductions Oct 3 '15 at 19:07
  • 16
    \$\begingroup\$ @ETHproductions You don't? Huh. \$\endgroup\$ – Martin Ender Oct 5 '15 at 21:35
  • 1
    \$\begingroup\$ TIO shows that an empty program works. \$\endgroup\$ – Weijun Zhou Mar 2 '18 at 9:02
18
\$\begingroup\$

Gammaplex, 0 bytes

In Gammaplex, it isn't possible to write a program that isn't an infinite loop. So I just write a program that doesn't use input/output.

\$\endgroup\$
  • \$\begingroup\$ Well the rules state that there needs to be an infinite loop to qualify. \$\endgroup\$ – arodebaugh Apr 20 '17 at 19:56
  • \$\begingroup\$ @arodebaugh There is. In other words, there is an infinite loop in every Gammaplex program. \$\endgroup\$ – jimmy23013 Apr 20 '17 at 20:12
16
\$\begingroup\$

bash + BSD coreutils, 23 22 14 6 5 6 bytes

yes>&-

yes outputs "y" forever; >&- closes STDOUT.

Thanks @Dennis and @ThisSuitIsBlackNot for help getting the size down!

\$\endgroup\$
  • 4
    \$\begingroup\$ With bash you can just do $0 in a shellscript and run it, it will invoke itself forever. \$\endgroup\$ – TessellatingHeckler Oct 2 '15 at 18:57
  • 1
    \$\begingroup\$ Creative, but boring old while :;do :;done is only 17 bytes. \$\endgroup\$ – ThisSuitIsBlackNot Oct 2 '15 at 19:09
  • 3
    \$\begingroup\$ ping>&- 0 works on Linux, where 0 is mapped to localhost. \$\endgroup\$ – Dennis Oct 2 '15 at 19:46
  • 1
    \$\begingroup\$ yes! (I thought about using yes but it didn't occur to me to discard the output.) You can save one byte with yes>/dev/null \$\endgroup\$ – ThisSuitIsBlackNot Oct 2 '15 at 19:50
  • 3
    \$\begingroup\$ I wasn't aware of that. If it works with BSD yes, great, but I don't think writing to a file complies with producing no output. \$\endgroup\$ – Dennis Oct 2 '15 at 20:03
15
\$\begingroup\$

Pyth, 2 bytes

# 

Pyth expects tokens after the forever operator. (That's a space.)

\$\endgroup\$
15
\$\begingroup\$

Common Lisp, 6 characters

(loop)
\$\endgroup\$
  • 42
    \$\begingroup\$ Is there something such as Rare Lisp? \$\endgroup\$ – flawr Oct 2 '15 at 20:15
  • 8
    \$\begingroup\$ @flawr I'll assume it was just a humorous comment, but Common Lisp is the result of a standardization process that unified a bunch of mostly-but-not-quite compatible languages in the Lisp family. Languages in the Lisp-family that aren't Common Lisp include Scheme, Racket, Emacs Lisp, and Clojure. \$\endgroup\$ – Joshua Taylor Oct 3 '15 at 3:07
  • \$\begingroup\$ Also (a bit longer, but if we didn't have to count parens, it'd be just two bytes): (do()(())). \$\endgroup\$ – Joshua Taylor Oct 3 '15 at 3:08
  • \$\begingroup\$ @JoshuaTaylor I thought about do too, and about making the reader goes on infinite loop but I don't see how to do it shortly. It seems that nothing beats (loop). \$\endgroup\$ – coredump Oct 3 '15 at 12:50
  • \$\begingroup\$ @JoshuaTaylor if you didn't count parens lisp would be the golfiest language of them all \$\endgroup\$ – Cyoce Feb 16 '17 at 0:06
13
\$\begingroup\$

Fission, 1 byte

There are exactly 4 one-byte solutions:

R
L
U
D

These four letters indicate that an atom starts there (in the corresponding direction). One of them is required because without an atom the program terminates immediately. Since the source code is only one character in size, the atom will wrap around immediately, and execute the same cell again. However, after the beginning of the program UDLR simply act to deflect an incoming atom in the corresponding direction, so they become no-ops in this case.

\$\endgroup\$

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.