119
\$\begingroup\$

Write the shortest code you can that produces an infinite output.

That's all. You code will only be disqualified if it stops producing output at some point. As always in code golf, the shortest code wins.

Here's a list of answers that I think are really clever, so they can get credit:

Leaderboard

var QUESTION_ID=13152,OVERRIDE_USER=8611;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 117
    \$\begingroup\$ All answers disqualified because at some point the Earth will be swallowed by the sun, and at some point the universe will die :P \$\endgroup\$ – Doorknob Nov 9 '13 at 20:00
  • 16
    \$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$ – Izkata Nov 10 '13 at 1:39
  • 5
    \$\begingroup\$ If I write mine in Piet, can I count the pixels of the text the other programs used? I believe the smallest possible repeating Piet program would be 6 pixels. That beats Befunge if "off" pixels still count. \$\endgroup\$ – DampeS8N Nov 12 '13 at 20:27
  • 9
    \$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ – ɐɔıʇǝɥʇuʎs Jul 11 '14 at 20:11
  • 7
    \$\begingroup\$ @Doorknob So really, the challenge is to produce infinite output in a finite amount of time. Sounds easy enough. \$\endgroup\$ – Sanchises Apr 10 '15 at 21:15

264 Answers 264

210
\$\begingroup\$

Befunge (1)

.

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

\$\endgroup\$
  • 6
    \$\begingroup\$ I think we have a winner. \$\endgroup\$ – primo Nov 9 '13 at 3:34
  • 95
    \$\begingroup\$ I swear I had a Java class around here somewhere that was shorter. +1 \$\endgroup\$ – l0b0 Nov 9 '13 at 9:22
  • 55
    \$\begingroup\$ This one dot has now gotten me more reputation than my next two answers combined. \$\endgroup\$ – marinus Nov 11 '13 at 14:32
  • 3
    \$\begingroup\$ 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? :) \$\endgroup\$ – Benjamin Podszun Jan 31 '14 at 7:47
  • 4
    \$\begingroup\$ Why is this not the accepted answer? \$\endgroup\$ – Claudiu Sep 3 '14 at 22:30
101
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ Wow. That's really good \$\endgroup\$ – tbodt Jan 16 '14 at 1:43
  • 46
    \$\begingroup\$ low machine code length should be worth more points than high level languages. \$\endgroup\$ – pwned Jan 23 '14 at 11:33
  • 12
    \$\begingroup\$ This is the shortest code in real sense. \$\endgroup\$ – microbian Jan 29 '14 at 4:51
  • 4
    \$\begingroup\$ Not only does it work, it works faster than all the other entries. :) \$\endgroup\$ – Thane Brimhall Apr 29 '14 at 19:46
  • 14
    \$\begingroup\$ @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! \$\endgroup\$ – kratenko Apr 30 '14 at 10:17
90
\$\begingroup\$

Windows Batch file, 2 chars

%0

Calls itself infinitely.

\$\endgroup\$
  • 11
    \$\begingroup\$ Close second place, man. Well done. I've only ever seen one answer where Batch has won it. \$\endgroup\$ – unclemeat Feb 13 '14 at 2:21
  • \$\begingroup\$ It's really great!!! \$\endgroup\$ – Qwertiy Nov 20 '14 at 19:57
  • \$\begingroup\$ This is awesome! \$\endgroup\$ – Patrick Purcell Dec 13 '15 at 1:16
  • \$\begingroup\$ 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. \$\endgroup\$ – Erik the Outgolfer Oct 6 '16 at 10:37
  • 2
    \$\begingroup\$ @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 \$\endgroup\$ – Blauhirn Jun 3 '17 at 4:04
67
\$\begingroup\$

Befunge 98 - 2

,"

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

\$\endgroup\$
  • 2
    \$\begingroup\$ Actually somewhat clever. \$\endgroup\$ – tbodt Sep 2 '14 at 20:16
  • \$\begingroup\$ 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. \$\endgroup\$ – AndoDaan Sep 2 '14 at 20:20
  • 1
    \$\begingroup\$ It's too bad it's buried under 82,395 really crappy answers. \$\endgroup\$ – tbodt Sep 3 '14 at 2:57
  • 7
    \$\begingroup\$ @tbodt Well, according to the spec, my little program has all of eternity to inch its way up. So... fingers crossed... \$\endgroup\$ – AndoDaan Sep 3 '14 at 3:21
  • \$\begingroup\$ @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. \$\endgroup\$ – YoYoYonnY Jul 15 '15 at 9:29
61
\$\begingroup\$

sh, 3 bytes

yes

outputs y continuously

\$\endgroup\$
  • 10
    \$\begingroup\$ Though yes is an external binary, and not part of bash (strictly speaking). \$\endgroup\$ – Bob Nov 9 '13 at 12:35
  • 54
    \$\begingroup\$ In bash, (almost) everything is an external binary. \$\endgroup\$ – Johannes Kuhn Nov 10 '13 at 22:42
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ – klingt.net Nov 28 '13 at 9:21
  • 5
    \$\begingroup\$ 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 ;-) \$\endgroup\$ – F. Hauri Jan 29 '14 at 6:34
  • 2
    \$\begingroup\$ As technosaurus says, in busybox it is a builtin, but in sh it isn't so you must say sh+coreutils instead. \$\endgroup\$ – Erik the Outgolfer Apr 12 '16 at 12:53
41
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ ... wow. Now I'm wondering if fix$show would have been more aesthetic :-) \$\endgroup\$ – John Dvorak Mar 4 '14 at 5:26
  • 7
    \$\begingroup\$ 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 "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\…". \$\endgroup\$ – Anders Kaseorg Feb 20 '16 at 10:18
37
\$\begingroup\$

Windows Batch file, 8 characters

:a
goto a
\$\endgroup\$
  • 1
    \$\begingroup\$ This does not produce infinite output, does it...? \$\endgroup\$ – Doorknob Nov 9 '13 at 2:28
  • 37
    \$\begingroup\$ it does...without @echo off, it prints "goto a" until you close it \$\endgroup\$ – Patrick Purcell Nov 9 '13 at 2:30
  • 11
    \$\begingroup\$ we need mor BAT around here \$\endgroup\$ – Einacio Jan 29 '14 at 20:44
  • 35
    \$\begingroup\$ I count ten characters: :a\r\ngoto a because Windows uses CRLF \$\endgroup\$ – wchargin Feb 3 '14 at 23:31
  • 1
    \$\begingroup\$ @WChargin I just checked using a hex editor. 3a 61 0a 67 6f 74 6f 20 works on my windows 8... :P \$\endgroup\$ – YoYoYonnY Jul 1 '15 at 22:44
31
\$\begingroup\$

Brainfuck, 4

+[.]

Alternatively,

-[.]
\$\endgroup\$
  • 8
    \$\begingroup\$ Neither \x01 nor \xFF are printable characters. I guess that wasn't a requirement, though :-/ \$\endgroup\$ – John Dvorak Nov 9 '13 at 5:37
  • \$\begingroup\$ A way to make it output printable characters would be to replace the + with , It does mean it requires an input though \$\endgroup\$ – Prismo Oct 3 '18 at 23:01
31
\$\begingroup\$

Java, 54 characters

Best attempt in Java:

class A{static{for(int i=0;i<1;)System.out.print(1);}}
\$\endgroup\$
  • 7
    \$\begingroup\$ Functional, complies with the rules, golfed. Wondering why it was downvoted. \$\endgroup\$ – manatwork Nov 11 '13 at 15:03
  • 14
    \$\begingroup\$ Does java forbid the classic for(;;) pattern? What about while(true)? \$\endgroup\$ – CodesInChaos Nov 21 '13 at 13:38
  • 17
    \$\begingroup\$ @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). \$\endgroup\$ – Rich Smith Nov 21 '13 at 17:56
  • 2
    \$\begingroup\$ @Mechanicalsnail Yes, static blocks can't execute alone since java 7. \$\endgroup\$ – Fabinout Feb 4 '14 at 14:51
  • 1
    \$\begingroup\$ @OliverNi But you can't use the static block. \$\endgroup\$ – Okx Sep 1 '17 at 15:17
30
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ maybe a behavior of stdout or similar... \$\endgroup\$ – masterX244 Jun 24 '14 at 19:08
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Emmanuel Nov 25 '16 at 14:44
29
\$\begingroup\$

LOLCODE (36)

HAI IZ 1<2? VISIBLE "0" KTHX KTHXBYE 

Thought I'd give LOLCODE a shot, has a surprisingly large amount of functionality.

\$\endgroup\$
  • 2
    \$\begingroup\$ Don't need to ask about stdio. \$\endgroup\$ – cat Jul 18 '16 at 4:13
  • \$\begingroup\$ In the repl.it version of LOLCODE, you don't need HAI and KTHXBYE \$\endgroup\$ – MilkyWay90 May 13 at 2:58
24
\$\begingroup\$

JavaScript: 14

WARNING: you really don't want to run this in your browser

for(;;)alert()
\$\endgroup\$
  • 31
    \$\begingroup\$ Just check "Prevent this site from opening more dialogs". \$\endgroup\$ – Johannes Kuhn Nov 11 '13 at 8:42
  • 1
    \$\begingroup\$ @JohannesKuhn don't check this and you need a reboot \$\endgroup\$ – Ilya Gazman Nov 11 '13 at 11:48
  • 8
    \$\begingroup\$ Or just do nothing and it don't display any further output. \$\endgroup\$ – Johannes Kuhn Nov 11 '13 at 12:02
  • \$\begingroup\$ just running alert() gives me this error Error: Not enough arguments [nsIDOMWindow.alert] \$\endgroup\$ – Clyde Lobo Mar 10 '14 at 9:21
  • 3
    \$\begingroup\$ Is it really infinite output, when the process is blocked and execution halted until humanoid interaction? \$\endgroup\$ – Morten Bergfall May 27 '14 at 15:25
24
\$\begingroup\$

Turing Machine - 6 Characters :

#s->1Rs

where # is blank symbol (on the tape by default), s describes the only existing (start) state, 1 is printing a digit, R means shifting to the right, s at the end is staying in the same state.

\$\endgroup\$
  • 3
    \$\begingroup\$ I count 7 chars!? \$\endgroup\$ – F. Hauri Nov 10 '13 at 23:24
  • 9
    \$\begingroup\$ In maybe the most-common 5-tuple notation, this would be written simply as the 5 characters s#1Rs (current-state, current-symbol, symbol-to-write, direction-to-shift, next-state). \$\endgroup\$ – r.e.s. Nov 21 '13 at 4:17
19
\$\begingroup\$

Haskell, 10

print[1..]

I think this is the shortest code to

  1. Print something out.
  2. Create an infinite Show:able datastructure.

For those interested, it prints:

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,...
\$\endgroup\$
  • 4
    \$\begingroup\$ Is there any reason you chose the magic number 32 to stop at? \$\endgroup\$ – ike Nov 10 '13 at 16:21
  • 12
    \$\begingroup\$ It seemed nice and round, at it also filled the code-block space as well as possible without the need for a next line. \$\endgroup\$ – shiona Nov 12 '13 at 1:22
  • 2
    \$\begingroup\$ If the machine has a finite amount of memory, it might terminate when the integer grows too huge to hold in memory? \$\endgroup\$ – Jeppe Stig Nielsen Nov 29 '13 at 8:40
  • 3
    \$\begingroup\$ @JeppeStigNielsen Yes. However an integer that would fill even one megabyte of memory would be 2^(2^20), or around 6.7 * 10^315652. Up that to a gigabyte of memory and the number would become 2^(2^30), a monster with almost half a billion digits. I'm quite sure that if we tried printing the series the heat death of the universe would hit us before the memory ran out. And HDD failure way before that. \$\endgroup\$ – shiona Nov 29 '13 at 10:05
  • 5
    \$\begingroup\$ In the comments below the OP it is mentioned that 'infinite untill your computer crashes' counts as well. \$\endgroup\$ – 11684 Nov 29 '13 at 18:22
19
\$\begingroup\$

Marbelous 4

24MB

Marbelous does surprisingy well here. This will post an infinite amount of dollar signs $, though it will hit the stack limit rather quickly.

How it works.

24 is a language literal, which will fall off the board and be printed to STDOUT as its corresponding ascii character. MB is the name implicitly given to the main board, since the main board has no input, it will fire every tick. And since cells are evaluated from left to right, the literal will always be printed before the next recursive call.

So this is rougly equivalent to this pseudocode:

MB() {
    print('$');
    while(true) MB();
}

A solution without infinite recursion 11

24@0
@0/\..

This one works by looping the literal between two portals @0, whenever the 24 hits the lower @0 it is transported to the cell underneath the upper @0. It the lands on the /\, which is a clone operator, it puts one copy of the marble (literal) on it's left (back on the portal) and another one to its right. This coopy then falls off the board (since .. is an empty cell) and gets printed to STDOUT. In pseudocode, this would translate to:

MB() {
    while(true) print '$'
}

A shorter solution without infinite recursion 9

24
\\/\..

This one constantly tosses the marble around between a cloner and a deflector, putting one copy on the rightmost cell, to be dropped off the board. In pseudocode that would look something like:

MB() {
    List charlist = ['$'];
    while(true) {
        charlist.add(charlist[0];
        charlist.add(charlist[0];
        charlist.pop();
        print(charlist.pop());
        // Wait, what?
    }
}

note

The .. cells are necessary on the two last boards since the marbles would land off the board (and be discarded) otherwise. For some extra fun, you replace the 24 marble by FF and the empty .. cell by a ??, which turns any marble into a marble between 0 and it's current value before dropping it down. Guess what that would look like on STDOUT.

\$\endgroup\$
  • \$\begingroup\$ make your "/\/\" "\\/\" to avoid producing an extra marble off the left edge? purely aesthetic improvement. \$\endgroup\$ – Sparr Mar 5 '15 at 17:07
  • \$\begingroup\$ @Sparr, point taken, I've edited the post. \$\endgroup\$ – overactor Mar 5 '15 at 21:30
  • 5
    \$\begingroup\$ Shouldn't this solution be 25165824 bytes (24MB)? ;P \$\endgroup\$ – HyperNeutrino Oct 12 '17 at 14:19
18
\$\begingroup\$

C, 23 chars

Slightly shorter than the best C/C++ answer so far. Prints empty lines infinitely (but if compiled without optimizations, overflows the stack).

main(){main(puts(""));}
\$\endgroup\$
  • 1
    \$\begingroup\$ 22 chars: putc(0) (or 1, or 2, ..., or 9). Would that work? \$\endgroup\$ – CompuChip Mar 10 '14 at 22:59
  • \$\begingroup\$ @CompuChip, putc requires 2 parameters. \$\endgroup\$ – ugoren Mar 11 '14 at 7:26
  • 10
    \$\begingroup\$ @tdbot, Just notice that this is the accepted answer. I appreciate the compliment, but can't understand why. There's an obviously shorter answer \$\endgroup\$ – ugoren Mar 11 '14 at 7:28
  • \$\begingroup\$ @tbodt, not tdbot. ftfy \$\endgroup\$ – nyuszika7h Jul 5 '14 at 12:35
  • 5
    \$\begingroup\$ @nyuszika7h, This way I could show how modest I am, while holding on to the 15 rep. \$\endgroup\$ – ugoren Jul 6 '14 at 11:20
14
\$\begingroup\$

Python 3: 15, 17, or 18 characters

mdeitrick's answer is longer in Python 3, which replaces the print statement with a function call (15 chars):

while 1:print()

This remains the shortest I've found in Python 3. However, there are some more-interesting ways of printing in an infinite loop that are only a few characters longer.

  • print() returns None, which != 9, making it an infinite loop; the 8 is a no-op that substitutes for pass (18 chars):

    while print()!=9:8
    
  • iter(print, 9) defines an iterable that returns the output of print() until it equals 9 (which never happens). any consumes the input iterable looking for a true value, which never arrives since print() always returns None. (You could also use set to the same effect.)

    any(iter(print,9))
    
  • Or, we can consumer the iterable by testing whether it contains 8 (17 chars):

    8in iter(print,9)
    
  • Or, unpack it using the splat operator:

    *_,=iter(print,9)
    
  • The weirdest way I thought of is to use splat destructuring inside a function call, function(*iterable). It seems that Python tries to consume the entire iterable before even attempting the function call—even if the function call is bogus. This means that we don't even need a real function, because the type error will only be thrown after the iterable is exhausted (i.e. never):

    8(*iter(print,9))
    
\$\endgroup\$
  • \$\begingroup\$ *iter(print,1), works and has only 15 chars, but does consume quite a bit of memory. \$\endgroup\$ – ivzem Sep 25 '18 at 19:54
14
\$\begingroup\$

piet - 3 codels

http://www.pietfiddle.net/img/auQXtFXATg.png?cs=15

Outputs an infinite number of 1's

\$\endgroup\$
14
\$\begingroup\$

x86 .COM Executable, 5 bytes

endless ASCII char set output

in hex:

40 CD 29 FF E6

in asm:

inc ax
int 0x29
jmp si

Explanation:

inc ax increments the register AX by one. int 0x29 is the "fast put char" routine of MSDOS, which simply outputs the value in AL (the low part of AX) and advances the cursor by one. jmp si is just a weird way to jump back to the top, since the register SI is 0x100 on almost every DOS-like operating system, which is also where a .com program starts ;) It's also possible to do a short jump instead, which also uses 2 bytes.

Sources:

MSDOS Start Values

Sizecoding WIKI

\$\endgroup\$
12
\$\begingroup\$

Summary: Ruby - 9, Golfscript - 6, ><> - 2, Whitespace - 19, Perl - 2

One language I know, and two I've never, ever, used before :D
EDIT: Perl one didn't work when I installed Perl to try it :(

Ruby, 9

loop{p 1}

Simply prints 1 on separate lines continually.

Also, a bunch of alternatives:

loop{p''} # prints [ "" ]  on separate lines continually
loop{p p} # prints [ nil ] on separate lines continually

10-char solution:

p 1while 1

I was actually surprised that I could remove the space between the first 1 and the while, but apparently it works

Golfscript, 6

{1p}do

My first Golfscript program! :P

><> (Fish), 2

1n

Whitespace, 19

lssslssstltlsslslsl

Where s represents a space, t represents a tab, and l a linefeed.

\$\endgroup\$
  • 10
    \$\begingroup\$ Every time someone types the words, "I don't have Perl," God kills a kitten. \$\endgroup\$ – primo Nov 9 '13 at 3:37
  • 2
    \$\begingroup\$ @primo :P Well at least now I do. \$\endgroup\$ – Doorknob Nov 9 '13 at 4:48
  • 2
    \$\begingroup\$ This is code golf. Please create one answer per submission. \$\endgroup\$ – J B Jan 15 '14 at 8:35
  • 21
    \$\begingroup\$ @primo Good, I hate cats. And I don't have Perl. \$\endgroup\$ – SQB Jan 15 '14 at 9:12
  • 5
    \$\begingroup\$ The GS program {1p}do only prints 1 and quits, because the do is an exec-pop-test sequence that continues only if it tests true. (Since there's no input, the stack is initially "", which, after exec of 1p, gets popped and tests false.) Just adding a dup will work though, i.e. {1.p}do (at 7 bytes). \$\endgroup\$ – r.e.s. Jan 26 '14 at 17:22
12
\$\begingroup\$

C, 25 24

main(){main(puts("1"));}
\$\endgroup\$
  • 3
    \$\begingroup\$ s/while/main/ \$\endgroup\$ – ugoren Nov 10 '13 at 14:05
  • 2
    \$\begingroup\$ I am not sure that there are stackoverflow exception in C, but something bad will happen when you run out of memory. \$\endgroup\$ – Ilya Gazman Nov 11 '13 at 11:46
  • 9
    \$\begingroup\$ Tailcall optimisation ftw. \$\endgroup\$ – Johannes Kuhn Nov 28 '13 at 8:38
  • 4
    \$\begingroup\$ @JohannesKuhn: that's not a tail call. If it'd been return main(puts("1")) then it'd been a tail call. \$\endgroup\$ – marinus Dec 30 '13 at 13:39
  • 4
    \$\begingroup\$ @psgivens. In c a function without any parameters can take any amount of parameters. Only a signature of methodName(void) accepts exactly zero parameters. \$\endgroup\$ – James Webster Jan 27 '14 at 16:59
12
\$\begingroup\$

><> (Fish), 2

A creative way to use the infinite codebox of fish:

"o

Because the instruction pointer returns back to the beginning of the line after reaching the end, this code can essentially be read as

"o"o

which means 'read the string "o" and then output it'.

You can test the code here

\$\endgroup\$
12
\$\begingroup\$

Bitxtreme, 0.25 bytes

Binary representation:

00

From the documentation:

The first bit of each pair is a pointer to the memory position which holds the value to subtract from the accumulator. The result is stored in that same memory position pointed to by the operand. If the result is negative (the bits are in two's complement representation) then the second bit of the pair will be added to the current PC, modulo 2.

The program counter and accumulator are initialized to zero; then, the contents of memory location 0 are subtracted from the accumulator. This happens to be 0, leaving the accumulator at zero. Since there was no carry, the second bit is not added to the program counter.

The program counter is then incremented by 2 modulo 2, sending it back to the start, and causing an infinite loop. At each step, the special memory location 0 is modified, causing its contents (a 0) to be written to output.

It can be argued that this program should be scored as 1 byte, because the official interpreter in Python requires zero-padding. However, I don't think the zero-padding is really code.

\$\endgroup\$
11
\$\begingroup\$

perl, 10 chars

Here's another 10 char perl solution with some different tradeoffs. Specifically, it doesn't require the -n flag or user input to start. However, it does keep eating memory ad infinitum.

warn;do$0

save to a file, execute that file and you get eg:

Warning: something's wrong at /tmp/foo.pl line 1.
Warning: something's wrong at /tmp/foo.pl line 1.
Warning: something's wrong at /tmp/foo.pl line 1.
\$\endgroup\$
  • \$\begingroup\$ Couldn't you make this shorter using say (which always prints a newline even if it can't find an argument from anywhere) rather than warn? (This requires the selection of a modern Perl variant using -M5.010, but that doesn't count against your character count.) \$\endgroup\$ – user62131 Nov 26 '16 at 21:54
10
\$\begingroup\$

dc, 7

[pdx]dx

prints 'pdx\n' infinitely many times.

\$\endgroup\$
10
\$\begingroup\$

VBA: 12

Audio is output, right?

do:beep:loop

Put that in your 'favorite' coworker's favorite macro-enabled MS office file for some 'fun'!

Private Sub Workbook_Open()
    Do:Beep:Loop
End Sub

Bonus points if they're using headphones.

\$\endgroup\$
  • \$\begingroup\$ Dang, I was looking through the answers to see if anyone had thought about printing the bell char yet. +1 \$\endgroup\$ – mbomb007 Apr 10 '15 at 20:55
9
\$\begingroup\$

Seed, 4 bytes

99 5

Try it online! Outputs 11 infinitely

Generates the following Befunge-98 Program:

[glzgx"Lz^v*M7TW!4:wi/l-[, s44~s;|Sa3zb|u<B/-&<Y a@=nN>Nc%}"gq!kCW $1{2hyzABRj*glr#z(@Zx@xT=>1'.b
/

Try it online!


The relevant part is just this:

[>1'.b
/

b pushes 11 to the stack and . prints it. 1 and 49 are also pushed to the stack, but never actually printed.

Animation of the code running:

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Won't this eventually run out of memory if values are continually pushed to the stack without being popped? \$\endgroup\$ – caird coinheringaahing Apr 4 '18 at 18:54
8
\$\begingroup\$

C, 26 25 24 (no recursion)

main(){for(;;puts(""));}

Prints endless '\n' characters. This is a byte longer than the shortest C answer, but doesn't rely on tail call optimization to avoid overflowing the stack.

\$\endgroup\$
  • 2
    \$\begingroup\$ You can shave off a character by using for(;;) in place of while(! \$\endgroup\$ – Alex Gittemeier Nov 12 '13 at 14:09
  • 1
    \$\begingroup\$ no ! as it keeps outputting \n \$\endgroup\$ – l4m2 Mar 29 '18 at 3:06
7
\$\begingroup\$

R, 13 characters

repeat cat(1)
\$\endgroup\$
  • \$\begingroup\$ repeat cat(1) should work \$\endgroup\$ – Dason Nov 9 '13 at 19:40
  • \$\begingroup\$ @Dason you're right. I have no idea why i insist on putting them every time. \$\endgroup\$ – plannapus Nov 11 '13 at 7:57
7
\$\begingroup\$

Postscript 9

{1 =}loop

Output 1 in an infinite loop.

\$\endgroup\$

protected by Community Jun 5 '18 at 8:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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