145
\$\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\$
14
  • 52
    \$\begingroup\$ I've got to start posting programs with a negative byte count to beat all these empty files! \$\endgroup\$
    – CJ Dennis
    Oct 3, 2015 at 4:32
  • 4
    \$\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, 2015 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\$ Oct 6, 2015 at 13:05
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Yes, file input is allowed. \$\endgroup\$
    – user41805
    Oct 6, 2015 at 16:47
  • \$\begingroup\$ Can you print "", an empty string? \$\endgroup\$
    – AAM111
    Mar 7, 2016 at 23:39

568 Answers 568

1
2 3 4 5
19
230
\$\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\$
5
  • 13
    \$\begingroup\$ Aw, I was gonna post this. :/ \$\endgroup\$
    – Deusovi
    Oct 2, 2015 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, 2015 at 20:02
  • 18
    \$\begingroup\$ @Justin That loophole only applies to certain types of challenges. \$\endgroup\$
    – Dennis
    Oct 2, 2015 at 20:32
  • 2
    \$\begingroup\$ You can use <pre>&nbsp;</pre> for an empty-looking code block. \$\endgroup\$
    – user253751
    Oct 3, 2015 at 6:17
  • \$\begingroup\$ First thing I looked for \$\endgroup\$ Oct 12, 2015 at 5:20
164
\$\begingroup\$

L00P, 0 bytes



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

\$\endgroup\$
4
  • 16
    \$\begingroup\$ LOL. +1 for choice of language. \$\endgroup\$ Oct 2, 2015 at 18:29
  • 82
    \$\begingroup\$ ... one of these days this lang is gonna loop all over you \$\endgroup\$
    – Pål GD
    Oct 3, 2015 at 11:05
  • 3
    \$\begingroup\$ +1 for the musical reference, and congrats on another gold badge! \$\endgroup\$
    – Luis Mendo
    Jul 1, 2016 at 23:35
  • 1
    \$\begingroup\$ The best reference I have seen \$\endgroup\$
    – user63187
    Jul 20, 2017 at 16:44
146
\$\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\$
11
  • 14
    \$\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\$ Oct 2, 2015 at 19:54
  • 60
    \$\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, 2015 at 20:40
  • 3
    \$\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, 2015 at 17:32
  • 2
    \$\begingroup\$ @Random832: Actually some of the later 6502 descendants did have an unconditional branch instruction (BRA, 0x80) - see for example this page (search for "BRA"). \$\endgroup\$
    – psmears
    Oct 5, 2015 at 20:15
  • 9
    \$\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\$ Oct 11, 2015 at 6:56
122
\$\begingroup\$

brainfuck, 3 bytes

+[]

Try it online!

Never decrement: never end.

\$\endgroup\$
6
  • 73
    \$\begingroup\$ It's funny when BF beats most other answers. \$\endgroup\$
    – Rohcana
    Oct 3, 2015 at 15:24
  • 1
    \$\begingroup\$ My first thought was BF! :D It ended shorter than I thought. \$\endgroup\$ Oct 3, 2015 at 19:16
  • 2
    \$\begingroup\$ Alternative, still same bytes: -[] \$\endgroup\$
    – Unihedron
    Jan 19, 2018 at 1:08
  • 5
    \$\begingroup\$ I bet there's an interpreter somewhere where ] would work \$\endgroup\$
    – 12Me21
    Mar 25, 2019 at 21:55
  • 2
    \$\begingroup\$ @12Me21 The most recent one I wrote, I just realized. \$\endgroup\$ Aug 7, 2019 at 17:52
116
\$\begingroup\$

///, 3 bytes

///

Try it online!

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

\$\endgroup\$
3
  • 77
    \$\begingroup\$ I'm honestly surprised that the link actually works, seeing as how it ends with /// ;) \$\endgroup\$ Oct 4, 2015 at 1:11
  • 1
    \$\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\$
    – hyper-neutrino
    May 1, 2016 at 22:33
  • 16
    \$\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, 2016 at 0:54
81
\$\begingroup\$

Java (JDK), 53 bytes

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

Try it online!

Yay full program requirement!

\$\endgroup\$
15
  • 88
    \$\begingroup\$ God is that verbose... \$\endgroup\$
    – user42643
    Oct 2, 2015 at 17:29
  • 5
    \$\begingroup\$ @minxomat gotta love that OOP \$\endgroup\$
    – Luke
    Oct 6, 2015 at 15:39
  • 72
    \$\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\$
    – geometrian
    Oct 11, 2015 at 5:18
  • 8
    \$\begingroup\$ @imallett Who says rocks aren't food? \$\endgroup\$
    – Geobits
    Nov 16, 2015 at 1:36
  • 10
    \$\begingroup\$ Replace class with interface and drop the public. Also, call main recursively instead of making a loop. \$\endgroup\$
    – Pavel
    Jan 8, 2017 at 10:23
60
\$\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, eg. swipl -g a a.pl with Swi-Prolog. 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.


Edit: I look at the rules again and for a full program and no command line arguments you need to add this, as user @radrow pointed out, for a total of 10 bytes.

a:-a.
:-a.

Alternatively, for 14 bytes you can have something more poetic:

:-repeat,fail.
\$\endgroup\$
7
  • 3
    \$\begingroup\$ Check if a is true by checking if a is true by checking if a is true by checking...yay recursion! \$\endgroup\$ Oct 8, 2015 at 20:20
  • \$\begingroup\$ You miss :-a. in your file, it won't magically execute itself \$\endgroup\$
    – radrow
    Mar 12, 2021 at 6:57
  • \$\begingroup\$ @radrow with swi-prolog for example you start the program with swipl -g a a.pl \$\endgroup\$
    – coredump
    Mar 12, 2021 at 8:41
  • \$\begingroup\$ Oh, didn't know that \$\endgroup\$
    – radrow
    Mar 12, 2021 at 8:43
  • \$\begingroup\$ @radrow I still edited the answer because using command line arguments might be not alright with the rule, I am not sure, thank you \$\endgroup\$
    – coredump
    Mar 12, 2021 at 8:50
52
\$\begingroup\$

Haskell, 9 bytes

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

main=main
\$\endgroup\$
8
  • 2
    \$\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, 2015 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, 2015 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, 2015 at 14:45
  • 1
    \$\begingroup\$ I think the shortest working haskell loop would be: main=main>>main \$\endgroup\$
    – lovasoa
    Nov 12, 2016 at 14:58
  • 1
    \$\begingroup\$ @nimi, what do you mean by "it doesn't loop"? It'll run forever with no output. \$\endgroup\$
    – dfeuer
    Mar 23, 2019 at 7:04
52
\$\begingroup\$

Python, 9 bytes

Works in both 2 and 3.

while 1:0

Shortened by @FryAmTheEggman

\$\endgroup\$
2
  • \$\begingroup\$ note, this does produce output when ran in the Python interpreter \$\endgroup\$ Aug 8, 2017 at 15:16
  • 6
    \$\begingroup\$ @Chris_Rands Only in the REPL. \$\endgroup\$
    – hyper-neutrino
    Oct 12, 2017 at 14:24
48
\$\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         [email protected]. ..

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\$
2
  • 9
    \$\begingroup\$ .com on DOS would be much shorter :) \$\endgroup\$
    – JimmyB
    Oct 5, 2015 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\$ Oct 5, 2015 at 19:58
42
\$\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\$
5
  • 13
    \$\begingroup\$ How dare you do a intercal program without comefrom \$\endgroup\$
    – flawr
    Oct 2, 2015 at 20:17
  • 1
    \$\begingroup\$ @flawr Fixed. ;) \$\endgroup\$ Oct 2, 2015 at 20:27
  • 4
    \$\begingroup\$ Np, I think we need more Intercal in golfing contests here=) \$\endgroup\$
    – flawr
    Oct 2, 2015 at 20:50
  • 20
    \$\begingroup\$ Did you say PLEASE? \$\endgroup\$
    – Daniel M.
    Oct 2, 2015 at 22:39
  • \$\begingroup\$ 15 bytes. Also works for CLC-INTERCAL. \$\endgroup\$
    – user100411
    Sep 15, 2021 at 3:41
37
\$\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\$
4
36
\$\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\$
8
  • 11
    \$\begingroup\$ Try ` ` for the code block. \$\endgroup\$ Oct 2, 2015 at 17:48
  • 2
    \$\begingroup\$ Why not just use >? \$\endgroup\$
    – mbomb007
    Oct 2, 2015 at 20:55
  • 5
    \$\begingroup\$ If this were Chem.SE, I would recommend putting the code in MathJax, too. \$\endgroup\$
    – hBy2Py
    Oct 5, 2015 at 1:56
  • \$\begingroup\$ Are we sure an empty program wouldn't be an infinite loop? \$\endgroup\$
    – Aaron
    Oct 5, 2015 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, 2015 at 17:40
36
\$\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\$
5
  • 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\$ Nov 4, 2015 at 18:02
  • \$\begingroup\$ Be warned that this shows up as 5 bytes on the scoreboard. \$\endgroup\$ Nov 18, 2015 at 10:31
  • \$\begingroup\$ C is Jump, but where to jump? \$\endgroup\$ Oct 4, 2016 at 4:34
  • 3
    \$\begingroup\$ Wow, you really like this language, huh? :P \$\endgroup\$
    – MD XF
    Jun 11, 2017 at 0:26
  • 1
    \$\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, 2017 at 17:29
35
\$\begingroup\$

LOLCODE, 24 bytes

IM IN YR X
IM OUTTA YR X
\$\endgroup\$
1
  • 4
    \$\begingroup\$ In many LOLCODE interpreters, such as the one on repl.it, HAI and KTHXBYE are unnecessary. \$\endgroup\$
    – Alex A.
    Oct 2, 2015 at 19:27
34
\$\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\$
8
  • 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\$ Oct 3, 2015 at 0:17
  • 24
    \$\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, 2015 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\$ Oct 5, 2015 at 21:01
  • 2
    \$\begingroup\$ For comparison, the shortest non-recursive proper loop implementation is 16B: main(){for(;;);}. \$\endgroup\$ Oct 6, 2015 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, 2015 at 12:37
31
\$\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\$
3
  • 1
    \$\begingroup\$ Your vimscript is a little long wh1|endw \$\endgroup\$
    – FDinoff
    Oct 6, 2015 at 4:48
  • \$\begingroup\$ Could you explain how this works to someone who's not familiar with Vim? \$\endgroup\$
    – iFreilicht
    Oct 16, 2015 at 11:37
  • 11
    \$\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\$ Dec 28, 2015 at 17:04
26
\$\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\$
26
\$\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
  • 13
    \$\begingroup\$ Why line number 10? Why not 9? \$\endgroup\$
    – recursive
    Oct 3, 2015 at 0:01
  • 25
    \$\begingroup\$ or 8 for 8 bytes?:) \$\endgroup\$
    – MickyT
    Oct 3, 2015 at 0:04
  • 4
    \$\begingroup\$ It's not going to win and it's funnier this way. \$\endgroup\$
    – CJ Dennis
    Oct 3, 2015 at 4:29
  • 4
    \$\begingroup\$ This could be shorted to: 1 RUN \$\endgroup\$
    – TOOGAM
    Oct 3, 2015 at 12:43
  • 3
    \$\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\$ Oct 11, 2015 at 7:06
26
\$\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\$
4
  • 1
    \$\begingroup\$ Wow, I just answered almost the exact thing in rs at the exact same time. \$\endgroup\$ Oct 2, 2015 at 19:12
  • 1
    \$\begingroup\$ @kirbyfan64sos They look very similar, but they actually work quite differently. :) \$\endgroup\$ Oct 2, 2015 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, 2015 at 21:10
  • \$\begingroup\$ @mbomb007 I think that would terminate with a 2 after a few iterations. \$\endgroup\$ Oct 3, 2015 at 5:34
25
\$\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\$
8
  • \$\begingroup\$ Wow-ee. That was amazing. \$\endgroup\$
    – adrian
    Oct 27, 2017 at 2:02
  • \$\begingroup\$ I always assumed JRO stood for Jump to Relative Offset. \$\endgroup\$ Jan 23, 2018 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\$ Jan 23, 2018 at 12:56
  • \$\begingroup\$ I wish I could +10 this answer. I love TIS-100 \$\endgroup\$
    – Shirkam
    Mar 2, 2018 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, 2018 at 21:36
24
\$\begingroup\$

Perl, 4 bytes

do$0

Executes itself repeatedly.

\$\endgroup\$
1
  • 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\$ Oct 6, 2015 at 16:52
23
\$\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\$
7
  • 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, 2015 at 17:44
  • \$\begingroup\$ @Geobits Ah, yes. How such simple things slip by. \$\endgroup\$
    – Namaskar
    Oct 2, 2015 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, 2015 at 4:40
  • \$\begingroup\$ @MichaelS Thanks! Worked in 2015 VS for me. \$\endgroup\$
    – Namaskar
    Oct 3, 2015 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, 2016 at 20:28
22
\$\begingroup\$

Foo, 3 bytes

(1)

Everyone's favorite programming language! :D

\$\endgroup\$
21
\$\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\$
8
  • 3
    \$\begingroup\$ Why don't you test it, :D \$\endgroup\$
    – user41805
    Oct 3, 2015 at 3:23
  • 8
    \$\begingroup\$ I can confirm that this is correct. :) \$\endgroup\$ Oct 3, 2015 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\$ Oct 3, 2015 at 19:07
  • 17
    \$\begingroup\$ @ETHproductions You don't? Huh. \$\endgroup\$ Oct 5, 2015 at 21:35
  • 3
    \$\begingroup\$ TIO shows that an empty program works. \$\endgroup\$ Mar 2, 2018 at 9:02
20
\$\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\$
20
  • 4
    \$\begingroup\$ With bash you can just do $0 in a shellscript and run it, it will invoke itself forever. \$\endgroup\$ Oct 2, 2015 at 18:57
  • 1
    \$\begingroup\$ Creative, but boring old while :;do :;done is only 17 bytes. \$\endgroup\$ Oct 2, 2015 at 19:09
  • 3
    \$\begingroup\$ ping>&- 0 works on Linux, where 0 is mapped to localhost. \$\endgroup\$
    – Dennis
    Oct 2, 2015 at 19:46
  • 2
    \$\begingroup\$ yes>- is just a slightly-obfuscated version of yes>a. It writes to a file called -. From my reading of the rules, this is acceptable, although Dennis has since commented on the question asking for clarification, and it does seem like kind of a loophole. \$\endgroup\$ Oct 2, 2015 at 20:00
  • 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, 2015 at 20:03
18
\$\begingroup\$

Pyth, 2 bytes

# 

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

\$\endgroup\$
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\$
2
  • \$\begingroup\$ Well the rules state that there needs to be an infinite loop to qualify. \$\endgroup\$
    – arodebaugh
    Apr 20, 2017 at 19:56
  • \$\begingroup\$ @arodebaugh There is. In other words, there is an infinite loop in every Gammaplex program. \$\endgroup\$
    – jimmy23013
    Apr 20, 2017 at 20:12
17
\$\begingroup\$

Common Lisp, 6 characters

(loop)
\$\endgroup\$
6
  • 52
    \$\begingroup\$ Is there something such as Rare Lisp? \$\endgroup\$
    – flawr
    Oct 2, 2015 at 20:15
  • 13
    \$\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\$ Oct 3, 2015 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\$ Oct 3, 2015 at 3:08
  • 2
    \$\begingroup\$ @JoshuaTaylor if you didn't count parens lisp would be the golfiest language of them all \$\endgroup\$
    – Cyoce
    Feb 16, 2017 at 0:06
  • 1
    \$\begingroup\$ @flawr Yeth, it'th when you can't properly pronounth the letter "eth" or "the." \$\endgroup\$
    – Makonede
    Jul 1, 2021 at 21:26
14
\$\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\$
1
2 3 4 5
19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.