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:
- The comma is both code and data
- Infinite errors (that counts)
- Infinite warnings (that also counts)
- What's Marbelous?
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>
-
144\$\begingroup\$ All answers disqualified because at some point the Earth will be swallowed by the sun, and at some point the universe will die :P \$\endgroup\$Doorknob– Doorknob2013年11月09日 20:00:11 +00:00Commented Nov 9, 2013 at 20:00
-
29\$\begingroup\$ Does "infinite until your computer crashes" count? <_< \$\endgroup\$Izkata– Izkata2013年11月10日 01:39:15 +00:00Commented Nov 10, 2013 at 1:39
-
7\$\begingroup\$ If I write mine in Piet, can I count the pixels of the text the other programs used? I believe the smallest possible repeating Piet program would be 6 pixels. That beats Befunge if "off" pixels still count. \$\endgroup\$DampeS8N– DampeS8N2013年11月12日 20:27:26 +00:00Commented Nov 12, 2013 at 20:27
-
13\$\begingroup\$ @Izkata So any answer that crashes your computer is also allowed :D \$\endgroup\$ɐɔıʇǝɥʇuʎs– ɐɔıʇǝɥʇuʎs2014年07月11日 20:11:35 +00:00Commented Jul 11, 2014 at 20:11
-
11\$\begingroup\$ @Doorknob So really, the challenge is to produce infinite output in a finite amount of time. Sounds easy enough. \$\endgroup\$Sanchises– Sanchises2015年04月10日 21:15:31 +00:00Commented Apr 10, 2015 at 21:15
349 Answers 349
Befunge (1)
.
Outputs 0 forever. It works because in Befunge lines wrap around, and you get 0 if you pop from the empty stack.
-
10\$\begingroup\$ I think we have a winner. \$\endgroup\$primo– primo2013年11月09日 03:34:51 +00:00Commented Nov 9, 2013 at 3:34
-
114\$\begingroup\$ I swear I had a Java class around here somewhere that was shorter. +1 \$\endgroup\$l0b0– l0b02013年11月09日 09:22:40 +00:00Commented Nov 9, 2013 at 9:22
-
66\$\begingroup\$ This one dot has now gotten me more reputation than my next two answers combined. \$\endgroup\$marinus– marinus2013年11月11日 14:32:21 +00:00Commented Nov 11, 2013 at 14:32
-
5\$\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– Benjamin Podszun2014年01月31日 07:47:55 +00:00Commented Jan 31, 2014 at 7:47
-
7\$\begingroup\$ Why is this not the accepted answer? \$\endgroup\$Claudiu– Claudiu2014年09月03日 22:30:07 +00:00Commented Sep 3, 2014 at 22:30
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.
-
2\$\begingroup\$ Wow. That's really good \$\endgroup\$tbodt– tbodt2014年01月16日 01:43:21 +00:00Commented Jan 16, 2014 at 1:43
-
51\$\begingroup\$ low machine code length should be worth more points than high level languages. \$\endgroup\$pwned– pwned2014年01月23日 11:33:13 +00:00Commented Jan 23, 2014 at 11:33
-
14\$\begingroup\$ This is the shortest code in real sense. \$\endgroup\$microbian– microbian2014年01月29日 04:51:41 +00:00Commented Jan 29, 2014 at 4:51
-
7\$\begingroup\$ Not only does it work, it works faster than all the other entries. :) \$\endgroup\$Thane Brimhall– Thane Brimhall2014年04月29日 19:46:46 +00:00Commented Apr 29, 2014 at 19:46
-
20\$\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– kratenko2014年04月30日 10:17:02 +00:00Commented Apr 30, 2014 at 10:17
Windows Batch file, 2 chars
%0
Calls itself infinitely.
-
18\$\begingroup\$ Close second place, man. Well done. I've only ever seen one answer where Batch has won it. \$\endgroup\$unclemeat– unclemeat2014年02月13日 02:21:31 +00:00Commented Feb 13, 2014 at 2:21
-
\$\begingroup\$ It's really great!!! \$\endgroup\$Qwertiy– Qwertiy2014年11月20日 19:57:56 +00:00Commented Nov 20, 2014 at 19:57
-
\$\begingroup\$ This is awesome! \$\endgroup\$Patrick Purcell– Patrick Purcell2015年12月13日 01:16:14 +00:00Commented Dec 13, 2015 at 1:16
-
3\$\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\$phil294– phil2942017年06月03日 04:04:32 +00:00Commented Jun 3, 2017 at 4:04
-
2\$\begingroup\$ Actually it will be able to continue indefinitely. Naming a batch file to run without using
callreplaces the prior batch execution with the new one. So this is a tail-call, not recursion. \$\endgroup\$Miral– Miral2018年02月22日 07:03:02 +00:00Commented Feb 22, 2018 at 7:03
Befunge 98 - 2
,"
Outputs ",,,,,,,,,,," for eternity.
-
2\$\begingroup\$ Actually somewhat clever. \$\endgroup\$tbodt– tbodt2014年09月02日 20:16:22 +00:00Commented Sep 2, 2014 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– AndoDaan2014年09月02日 20:20:53 +00:00Commented Sep 2, 2014 at 20:20
-
1\$\begingroup\$ It's too bad it's buried under 82,395 really crappy answers. \$\endgroup\$tbodt– tbodt2014年09月03日 02:57:27 +00:00Commented Sep 3, 2014 at 2:57
-
8\$\begingroup\$ @tbodt Well, according to the spec, my little program has all of eternity to inch its way up. So... fingers crossed... \$\endgroup\$AndoDaan– AndoDaan2014年09月03日 03:21:50 +00:00Commented Sep 3, 2014 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\$yyny– yyny2015年07月15日 09:29:28 +00:00Commented Jul 15, 2015 at 9:29
sh, 3 bytes
yes
outputs y continuously
-
11\$\begingroup\$ Though
yesis an external binary, and not part of bash (strictly speaking). \$\endgroup\$Bob– Bob2013年11月09日 12:35:21 +00:00Commented Nov 9, 2013 at 12:35 -
64\$\begingroup\$ In bash, (almost) everything is an external binary. \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年11月10日 22:42:28 +00:00Commented Nov 10, 2013 at 22:42
-
5\$\begingroup\$ Normally you don't have an alias
y=yesand if your system isn't esoteric you can assume thatyesis in your$PATH(see GNU Core Utilities). With your logic it is possible to assume weird constellations for every solution. \$\endgroup\$klingt.net– klingt.net2013年11月28日 09:21:25 +00:00Commented Nov 28, 2013 at 9:21 -
6\$\begingroup\$ Right, but this is not only a
bashcapability: I'ts a POSIX shell basic part of core utils. (Ie: please change title for sh: 3 chars ;-) \$\endgroup\$F. Hauri - Give Up GitHub– F. Hauri - Give Up GitHub2014年01月29日 06:34:02 +00:00Commented Jan 29, 2014 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– Erik the Outgolfer2016年04月12日 12:53:48 +00:00Commented Apr 12, 2016 at 12:53
GHCi + Data.Function, 8 bytes
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.
-
1\$\begingroup\$ ... wow. Now I'm wondering if
fix$showwould have been more aesthetic :-) \$\endgroup\$John Dvorak– John Dvorak2014年03月04日 05:26:14 +00:00Commented Mar 4, 2014 at 5:26 -
15\$\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– Anders Kaseorg2016年02月20日 10:18:07 +00:00Commented Feb 20, 2016 at 10:18
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.
-
2\$\begingroup\$ maybe a behavior of stdout or similar... \$\endgroup\$masterX244– masterX2442014年06月24日 19:08:58 +00:00Commented Jun 24, 2014 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– Emmanuel2016年11月25日 14:44:14 +00:00Commented Nov 25, 2016 at 14:44
Windows Batch file, 9 characters
:a
goto a
-
2\$\begingroup\$ This does not produce infinite output, does it...? \$\endgroup\$Doorknob– Doorknob2013年11月09日 02:28:24 +00:00Commented Nov 9, 2013 at 2:28
-
44\$\begingroup\$ it does...without @echo off, it prints "goto a" until you close it \$\endgroup\$Patrick Purcell– Patrick Purcell2013年11月09日 02:30:55 +00:00Commented Nov 9, 2013 at 2:30
-
12\$\begingroup\$ we need mor BAT around here \$\endgroup\$Einacio– Einacio2014年01月29日 20:44:50 +00:00Commented Jan 29, 2014 at 20:44
-
41\$\begingroup\$ I count ten characters:
:a\r\ngoto abecause Windows uses CRLF \$\endgroup\$wchargin– wchargin2014年02月03日 23:31:11 +00:00Commented Feb 3, 2014 at 23:31 -
1\$\begingroup\$ @WChargin I just checked using a hex editor.
3a 61 0a 67 6f 74 6f 20works on my windows 8... :P \$\endgroup\$yyny– yyny2015年07月01日 22:44:00 +00:00Commented Jul 1, 2015 at 22:44
Java, 54 characters
Best attempt in Java:
class A{static{for(int i=0;i<1;)System.out.print(1);}}
-
7\$\begingroup\$ Functional, complies with the rules, golfed. Wondering why it was downvoted. \$\endgroup\$manatwork– manatwork2013年11月11日 15:03:20 +00:00Commented Nov 11, 2013 at 15:03
-
14\$\begingroup\$ Does java forbid the classic
for(;;)pattern? What aboutwhile(true)? \$\endgroup\$CodesInChaos– CodesInChaos2013年11月21日 13:38:44 +00:00Commented Nov 21, 2013 at 13:38 -
21\$\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– Rich Smith2013年11月21日 17:56:12 +00:00Commented Nov 21, 2013 at 17:56
-
2\$\begingroup\$ @Mechanicalsnail Yes, static blocks can't execute alone since java 7. \$\endgroup\$Fabinout– Fabinout2014年02月04日 14:51:33 +00:00Commented Feb 4, 2014 at 14:51
-
1\$\begingroup\$ @OliverNi But you can't use the
staticblock. \$\endgroup\$Okx– Okx2017年09月01日 15:17:42 +00:00Commented Sep 1, 2017 at 15:17
Brainfuck, 4
+[.]
Alternatively,
-[.]
-
13\$\begingroup\$ Neither
\x01nor\xFFare printable characters. I guess that wasn't a requirement, though :-/ \$\endgroup\$John Dvorak– John Dvorak2013年11月09日 05:37:37 +00:00Commented Nov 9, 2013 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– Prismo2018年10月03日 23:01:49 +00:00Commented Oct 3, 2018 at 23:01
LOLCODE (36)
HAI IZ 1<2? VISIBLE "0" KTHX KTHXBYE
Thought I'd give LOLCODE a shot, has a surprisingly large amount of functionality.
-
3\$\begingroup\$ Don't need to ask about stdio. \$\endgroup\$cat– cat2016年07月18日 04:13:23 +00:00Commented Jul 18, 2016 at 4:13
-
1\$\begingroup\$ In the repl.it version of LOLCODE, you don't need
HAIandKTHXBYE\$\endgroup\$MilkyWay90– MilkyWay902019年05月13日 02:58:46 +00:00Commented May 13, 2019 at 2:58
JavaScript: 14
WARNING: you really don't want to run this in your browser
for(;;)alert()
-
36\$\begingroup\$ Just check "Prevent this site from opening more dialogs". \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年11月11日 08:42:15 +00:00Commented Nov 11, 2013 at 8:42
-
1\$\begingroup\$ @JohannesKuhn don't check this and you need a reboot \$\endgroup\$Ilya Gazman– Ilya Gazman2013年11月11日 11:48:33 +00:00Commented Nov 11, 2013 at 11:48
-
9\$\begingroup\$ Or just do nothing and it don't display any further output. \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年11月11日 12:02:39 +00:00Commented Nov 11, 2013 at 12:02
-
\$\begingroup\$ just running
alert()gives me this errorError: Not enough arguments [nsIDOMWindow.alert]\$\endgroup\$Clyde Lobo– Clyde Lobo2014年03月10日 09:21:30 +00:00Commented Mar 10, 2014 at 9:21 -
4\$\begingroup\$ Is it really infinite output, when the process is blocked and execution halted until humanoid interaction? \$\endgroup\$Morten Bergfall– Morten Bergfall2014年05月27日 15:25:39 +00:00Commented May 27, 2014 at 15:25
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.
-
3\$\begingroup\$ I count 7 chars!? \$\endgroup\$F. Hauri - Give Up GitHub– F. Hauri - Give Up GitHub2013年11月10日 23:24:31 +00:00Commented Nov 10, 2013 at 23:24
-
11\$\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.– r.e.s.2013年11月21日 04:17:58 +00:00Commented Nov 21, 2013 at 4:17
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(""));}
-
2\$\begingroup\$ 22 chars:
putc(0)(or 1, or 2, ..., or 9). Would that work? \$\endgroup\$CompuChip– CompuChip2014年03月10日 22:59:28 +00:00Commented Mar 10, 2014 at 22:59 -
1\$\begingroup\$ @CompuChip,
putcrequires 2 parameters. \$\endgroup\$ugoren– ugoren2014年03月11日 07:26:01 +00:00Commented Mar 11, 2014 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– ugoren2014年03月11日 07:28:10 +00:00Commented Mar 11, 2014 at 7:28
-
\$\begingroup\$ @tbodt, not tdbot. ftfy \$\endgroup\$user344– user3442014年07月05日 12:35:52 +00:00Commented Jul 5, 2014 at 12:35
-
7\$\begingroup\$ @nyuszika7h, This way I could show how modest I am, while holding on to the 15 rep. \$\endgroup\$ugoren– ugoren2014年07月06日 11:20:39 +00:00Commented Jul 6, 2014 at 11:20
x86 .COM Executable, 5 bytes
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:
Haskell, 10
print[1..]
I think this is the shortest code to
- Print something out.
- 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,...
-
5\$\begingroup\$ Is there any reason you chose the magic number 32 to stop at? \$\endgroup\$ike– ike2013年11月10日 16:21:37 +00:00Commented Nov 10, 2013 at 16:21
-
15\$\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– shiona2013年11月12日 01:22:01 +00:00Commented Nov 12, 2013 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– Jeppe Stig Nielsen2013年11月29日 08:40:18 +00:00Commented Nov 29, 2013 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– shiona2013年11月29日 10:05:38 +00:00Commented Nov 29, 2013 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– 116842013年11月29日 18:22:32 +00:00Commented Nov 29, 2013 at 18:22
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.
-
\$\begingroup\$ make your "/\/\" "\\/\" to avoid producing an extra marble off the left edge? purely aesthetic improvement. \$\endgroup\$Sparr– Sparr2015年03月05日 17:07:56 +00:00Commented Mar 5, 2015 at 17:07
-
\$\begingroup\$ @Sparr, point taken, I've edited the post. \$\endgroup\$overactor– overactor2015年03月05日 21:30:15 +00:00Commented Mar 5, 2015 at 21:30
-
7\$\begingroup\$ Shouldn't this solution be 25165824 bytes (24MB)? ;P \$\endgroup\$2017年10月12日 14:19:16 +00:00Commented Oct 12, 2017 at 14:19
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()returnsNone, which != 9, making it an infinite loop; the8is a no-op that substitutes forpass(18 chars):while print()!=9:8iter(print, 9)defines an iterable that returns the output ofprint()until it equals9(which never happens).anyconsumes the input iterable looking for a true value, which never arrives sinceprint()always returnsNone. (You could also usesetto 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))
-
\$\begingroup\$
*iter(print,1),works and has only 15 chars, but does consume quite a bit of memory. \$\endgroup\$ivzem– ivzem2018年09月25日 19:54:26 +00:00Commented Sep 25, 2018 at 19:54 -
\$\begingroup\$
while(1):1has 10 chars, basically an infinite loop \$\endgroup\$Saphereye– Saphereye2021年11月30日 17:49:42 +00:00Commented Nov 30, 2021 at 17:49 -
\$\begingroup\$ Using 3.5+,
8(*iter(print,9))can shorten to*iter(print,9),(dropping 17 down to 15). That trailing comma is mandatory (it makes it atupleliteral using the unpacking generalizations). Matches thewhile 1:print()for length, though the unpacking will rapidly exhaust all your memory and hopefully crash before it brings your computer to a standstill. \$\endgroup\$ShadowRanger– ShadowRanger2023年02月17日 18:59:35 +00:00Commented Feb 17, 2023 at 18:59
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.
-
\$\begingroup\$ Dang, I was looking through the answers to see if anyone had thought about printing the bell char yet. +1 \$\endgroup\$mbomb007– mbomb0072015年04月10日 20:55:30 +00:00Commented Apr 10, 2015 at 20:55
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;do0ドル
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.
-
1\$\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 thanwarn? (This requires the selection of a modern Perl variant using-M5.010, but that doesn't count against your character count.) \$\endgroup\$user62131– user621312016年11月26日 21:54:50 +00:00Commented Nov 26, 2016 at 21:54
><> (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'.
piet - 3 codels
http://www.pietfiddle.net/img/auQXtFXATg.png?cs=15
Outputs an infinite number of 1's
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.
Z80Golf, 0 bytes
Surprisingly, this is an empty machine code program that produces infinite output.
How it works
Z80Golf machine initializes all the memory and registers to zero, and then loads the code to address 0. But there's nothing to load, so the whole memory becomes a no-op program.
When the PC hits the address 8000ドル ($ indicates hex in Z80 assembly), putchar is simulated. It consists of printing the register a to stdout and a ret instruction. Initially the stack pointer sp is zero, and ret does pc <- [sp]; sp <- sp + 2. The whole memory is zeroed, so the ret always returns to the start of the program regardless of sp.
The conclusion is that an empty Z80Golf program is a program that infinitely prints null bytes.
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.
-
12\$\begingroup\$ Every time someone types the words, "I don't have Perl," God kills a kitten. \$\endgroup\$primo– primo2013年11月09日 03:37:24 +00:00Commented Nov 9, 2013 at 3:37
-
2\$\begingroup\$ @primo :P Well at least now I do. \$\endgroup\$Doorknob– Doorknob2013年11月09日 04:48:26 +00:00Commented Nov 9, 2013 at 4:48
-
2\$\begingroup\$ This is code golf. Please create one answer per submission. \$\endgroup\$J B– J B2014年01月15日 08:35:27 +00:00Commented Jan 15, 2014 at 8:35
-
24\$\begingroup\$ @primo Good, I hate cats. And I don't have Perl. \$\endgroup\$SQB– SQB2014年01月15日 09:12:59 +00:00Commented Jan 15, 2014 at 9:12
-
5\$\begingroup\$ The GS program
{1p}doonly prints1and 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 of1p, gets popped and tests false.) Just adding a dup will work though, i.e.{1.p}do(at 7 bytes). \$\endgroup\$r.e.s.– r.e.s.2014年01月26日 17:22:17 +00:00Commented Jan 26, 2014 at 17:22
C, (削除) 25 (削除ここまで) 24
main(){main(puts("1"));}
-
4\$\begingroup\$
s/while/main/\$\endgroup\$ugoren– ugoren2013年11月10日 14:05:52 +00:00Commented Nov 10, 2013 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– Ilya Gazman2013年11月11日 11:46:54 +00:00Commented Nov 11, 2013 at 11:46
-
9\$\begingroup\$ Tailcall optimisation ftw. \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年11月28日 08:38:42 +00:00Commented Nov 28, 2013 at 8:38
-
5\$\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– marinus2013年12月30日 13:39:15 +00:00Commented Dec 30, 2013 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– James Webster2014年01月27日 16:59:30 +00:00Commented Jan 27, 2014 at 16:59
Minecraft> 1.9 2 + 5 = 7 Bytes
Note that this version of this "language" was created after the question.
Surprisingly good for MineCraft. o-o
This is using this definition of MineCraft scoring.
The command say 1 put inside of a permanently active repeating command block. It will permanently output [@] 1 to the chat.
-
2\$\begingroup\$ I believe this is technically invalid, because it uses a version of the language from after the challenge :P \$\endgroup\$lirtosiast– lirtosiast2015年11月12日 22:40:26 +00:00Commented Nov 12, 2015 at 22:40
-
\$\begingroup\$ Never mind. I'll mark it as invalid. ;D \$\endgroup\$Addison Crump– Addison Crump2015年11月12日 22:43:14 +00:00Commented Nov 12, 2015 at 22:43
-
\$\begingroup\$ It's valid now; we had a rule change. \$\endgroup\$wizzwizz4– wizzwizz42019年07月31日 12:46:47 +00:00Commented Jul 31, 2019 at 12:46
-
3\$\begingroup\$ Cookie. 🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪 \$\endgroup\$2022年04月23日 02:54:28 +00:00Commented Apr 23, 2022 at 2:54
-
2\$\begingroup\$ vyxal genie giveth \$\endgroup\$Razetime– Razetime2022年04月23日 14:19:19 +00:00Commented Apr 23, 2022 at 14:19
-
1\$\begingroup\$ yay! Cookie gooo \$\endgroup\$math scat– math scat2022年04月25日 11:13:15 +00:00Commented Apr 25, 2022 at 11:13
-
\$\begingroup\$ So a 🍪 takes 4 "bytes" to eat? :3 \$\endgroup\$DialFrost– DialFrost2022年10月28日 07:13:52 +00:00Commented Oct 28, 2022 at 7:13
-
\$\begingroup\$ vyxal.pythonanywhere.com/#WyIiLCIiLCJ7LCIsIiIsIiJd 2 bytes, not as cool as a cookie \$\endgroup\$pacman256– pacman2562022年10月31日 03:51:33 +00:00Commented Oct 31, 2022 at 3:51
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
/
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:
-
1\$\begingroup\$ Won't this eventually run out of memory if values are continually pushed to the stack without being popped? \$\endgroup\$2018年04月04日 18:54:02 +00:00Commented Apr 4, 2018 at 18:54
-
1\$\begingroup\$ @cairdcoinheringaahing You'd be hard pressed to find an answer here that won't run out of memory \$\endgroup\$2020年06月21日 02:46:10 +00:00Commented Jun 21, 2020 at 2:46