86
\$\begingroup\$

Write a program that counts up forever, starting from one.

Rules:

  • Your program must log to STDOUT or an acceptable alternative, if STDOUT is not available.
  • Your program must be a full, runnable program, and not a function or snippet.
  • Your program must output each number with a separating character in between (a newline, space, tab or comma), but this must be consistent for all numbers.
  • You may print the numbers in decimal, in unary or in base 256 where each digit is represented by a byte value.
  • Your program must count at least as far as 2128 (inclusive) without problems and without running out of memory on a reasonable desktop PC. In particular, this means if you're using unary, you cannot store a unary representation of the current number in memory.
  • Unlike our usual rules, feel free to use a language (or language version) even if it's newer than this challenge. Languages specifically written to submit a 0-byte answer to this challenge are fair game but not particularly interesting.

    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.

  • This is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><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><script>var QUESTION_ID = 63834; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 39069; 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.toLowerCase(42), 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 > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) 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); } }</script>

\$\endgroup\$
26
  • 35
    \$\begingroup\$ I'm not sure how to combine must output each number with a separating character in between with may print the numbers [...] in base 256. \$\endgroup\$
    – Dennis
    Nov 14, 2015 at 14:25
  • 8
    \$\begingroup\$ For future challenges, may I recommend the sandbox such that all these details could be sorted out before people start posting answers? :) \$\endgroup\$ Nov 14, 2015 at 14:32
  • 4
    \$\begingroup\$ @IlmariKaronen I interpret that as being a memory limit, not a time limit. possibly a time limit on per-increment. just set the counter to 2**128-10 and see how long it takes to take those last ten steps. \$\endgroup\$
    – Sparr
    Nov 15, 2015 at 7:41
  • 5
    \$\begingroup\$ Can we have leading zeroes in the output? \$\endgroup\$ Nov 15, 2015 at 11:43
  • 4
    \$\begingroup\$ Ugh!!! I have an answer for TI-89 (56b), but I can't post b/c I'm new to the site and don't have Rep 10+! \$\endgroup\$ Nov 15, 2015 at 20:45

214 Answers 214

1
4 5
6
7 8
1
\$\begingroup\$

Python 3, 24 bytes

i=1
while[print(i)]:i+=1

Try it online!

[print(i)] is a list containing 1 element, which is always truthy. Thus the loop is repeated infinitely.

\$\endgroup\$
1
\$\begingroup\$

Billiards, 15 bytes/11 characters

Inspired by PhiNotPi's Challenge

1/\
 ↥↑
\^/

Explanation: 1 summons a ball with value 1. It falls onto the \, which deflects it into the ^.

Then, it levitates from the ^, goes to the which prints its value. Then it deflects off the /, and then hits the \ and begins falling again. It hits the which increments its value, and then hits the / and deflects back into the ^.

Bonus: Replace 1 with a to output the starting point.

Bonus: Add b to the end of the first row and < to the end of the last row to specify the ending point.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 3 bytes

‘Ṅß

Try it online!

‘Ṅß
‘      Increment
 Ṅ     Print w/ linefeed
  ß    Call this link again
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Wait there was actually never a Jelly answer on this before yesterday...?? Wow. But nice, +1! :D \$\endgroup\$
    – hyper-neutrino
    Jun 17, 2017 at 1:57
1
\$\begingroup\$

05AB1E, 1 byte

Try it online!

∞  # [1, 2, ..., ∞]
   # implicit output
\$\endgroup\$
1
\$\begingroup\$

Elixir, 40 bytes

f=fn f,i->IO.puts i;f.(f,i+1)end
f.(f,1)

Try it online!

Strangely enough, up to this point the leaderboard snippet doesn't list any answers in languages starting with E. Let's fix this!

Anyway, Elixir isn't a very convenient choice for this task, as to my knowledge it doesn't have a truly concise way of creating infinite loops/enums, named lambdas can't be recursive, while normal named functions can only be declared within a module. Therefore, we resort to a trick, where we create a named lambda, and pass it to itself as one of the arguments. Hacky, but still shorter than using a conventional way of creating an endless iterator:

Elixir, 45 bytes

Enum.map Stream.iterate(1,&(&1+1)),&IO.puts/1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

, 3 bytes

+.=

Unwrapped:

  +
. = . .
  .

Simple explanation:

+   Add one to memory cell (starts at zero)
=   Print memory cell as number
.   No-op

The instruction pointer starts at '+' then "falls" into '=', then the '.' at the bottom.
The IP then falls to the '.' on the far right, as if the code is mapped onto a cube.
The IP then falls to '+', completing the loop.

Interpreter

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Fun fact: this is also a valid APL function. \$\endgroup\$
    – Razetime
    Feb 26, 2021 at 16:20
1
\$\begingroup\$

Scratch 3.0, 57 bytes

57 bytes using scratchblocks syntax, 6 blocks total. say might not be an acceptable form of output because there are no separating characters, so inform me if that's the case.

when gf clicked
set[n v]to[
forever
change[n v]by[1
say(n
\$\endgroup\$
1
  • \$\begingroup\$ Using say is perfectly valid - I've used it for something like this before and no one complained. \$\endgroup\$
    – lyxal
    Mar 30, 2021 at 21:41
1
\$\begingroup\$

VBScript, 34 bytes

n=1
Do
Wscript.Echo n
n=n+1
Loop
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

{›…

Try it Online!

{   # Forever...
 ›  # Increment
  … # Print without popping
\$\endgroup\$
1
\$\begingroup\$

ErrLess, 9 bytes

1}@#a?1+{

Explanation

1  { Push one to the stack }
}  { Mark end of "comment" }
@# { Output top of stack as number }
a? { Print newline }
1+ { Add one to top of stack }
{  { Mark start of "comment" (skip adding another 1 to the stack) }

Try it online!

\$\endgroup\$
1
\$\begingroup\$

convey, 8 bytes

1/.]
1+}

Try it online!

Uses the indices command (/.) with an infinite input to print produce ascending numbers forever. However, they start from zero, neccesitating a +1 to be added. Technically I could reuse the 1s that are produced from the other end of the /., but I couldn't figure out a shorter way to do that, so it ends up going to a sink.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 37 27 bytes

for(i=1;1;)console.log(i++)

Try it online!

TIO truncates the output since it exceeds 128 KiB.

\$\endgroup\$
0
1
\$\begingroup\$

KonamiCode, 48 bytes

v(>)>(^)v(^^^>^^)>(>)L(>)<<<v((>))>(^)<<>(>)B(>)

This is actually quite similar to the 0-100 program, just without the comparison buffer being set.

\$\endgroup\$
1
  • \$\begingroup\$ The maximum value here does depend on the interpreter, so this works in theory but maybe not in practice. \$\endgroup\$
    – Ginger
    Nov 12, 2021 at 14:04
1
\$\begingroup\$

Python 3, 26 bytes

i=1
while(1):print(i);i+=1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

A0A0, 45 bytes

A0A0
A0C3G1G1A0
A0V1O0P9S1A0
A0A1G-3G-3A0
G-3

Prints 1 up to infinity, delimited by tabs. This uses a basic infinite loop construct, which executes the following infinitely:

V1 O0 P9 S1
V1          ; Operand, initialized with 1
   O0       ; Outputs the operand as a number
      P9    ; Prints a tab (ascii code 9)
         S1 ; Increments the operand by 1

Edit: switched to tabs as a delimiter, since this saves 1 byte

\$\endgroup\$
1
\$\begingroup\$

Knight, 15 13 bytes

Thanks @Steffan for -2 bytes

;=a 0W1O=a+1a

Try It Online!

I have no idea what I'm doing but if it works then it works lol.

\$\endgroup\$
2
  • \$\begingroup\$ =a +a 1 => =a+a 1 => =a+1a \$\endgroup\$
    – naffetS
    Aug 7, 2022 at 2:42
  • \$\begingroup\$ @Steffan oh yeah, nice \$\endgroup\$
    – Aiden Chow
    Aug 7, 2022 at 2:43
1
\$\begingroup\$

Seed, 10 bytes

4 28895602

Try it online!

\$\endgroup\$
1
\$\begingroup\$

GNU sed 4.2.2 -rn (/dev/random), 3 1 byte

=

Yup, that's it.

For this program to work correctly, it must be run on an infinite-length file that can output a newline byte; /dev/random works well for this. Run with sed -rn '=' /dev/random.

\$\endgroup\$
2
  • \$\begingroup\$ If we're taking /dev/random as input, then with additionally the -n flag you can just have = \$\endgroup\$
    – user41805
    Jan 3 at 19:37
  • \$\begingroup\$ @user41805 good point! \$\endgroup\$
    – Ginger
    Jan 3 at 19:46
1
\$\begingroup\$

Unix coreutils jot, 7 bytes

jot 0 1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Rattle, 5 bytes

[+p]0

Try it Online!

Explanation

[ ... ]0             infinite loop
  +                  increment
   p                 print
\$\endgroup\$
1
\$\begingroup\$

(,), 23 Chars or 4.55 Bytes

(,((),(())()),(()),,())
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 19 14 Bytes

for(){$i++;$i}

Removed echo as per user recommendation

\$\endgroup\$
9
  • 3
    \$\begingroup\$ Is $i really going to have an inferred type which goes as far as 2^128? \$\endgroup\$ Nov 16, 2015 at 19:38
  • \$\begingroup\$ @PeterTaylor I had no idea how big 2^128 was until this challenge was posted. @Chad Baxter for shortening output, you can leave off echo. \$\endgroup\$
    – Booga Roo
    Nov 16, 2015 at 21:14
  • \$\begingroup\$ Powershell is very good at making sure the memory space is large enough. If it won't fit in a 32 bit int, it will automatically convert it to a 64bit int...for anything higher it autoconcatenates \$\endgroup\$ Nov 16, 2015 at 21:54
  • \$\begingroup\$ So technically it will only fill two Int64 to meet requirements...but it can go higher \$\endgroup\$ Nov 16, 2015 at 21:57
  • 1
    \$\begingroup\$ Once you figure out the conversion to allow 2^128 (I'm not sure it's possible without using [bigint]), you can save a couple bytes by doing (++$i) instead of $i++;$i -- the prepending ++ ensures we start at 1, and enclosing in parens () will automatically output, since it's left on the pipeline alone. However, if you need [bigint], ignore this recommendation. \$\endgroup\$ Nov 25, 2015 at 14:15
0
\$\begingroup\$

Pylongolf2, 6 bytes

0>1+~<

Have a zero in the stack and continously add up to it.
I could give a more detailed explanation.

Pylongolf2, 20 bytes (bonus)

cn:A0>1+~_A@1=?t¿d<

Only works in beta 6, but this version asks for what number to count to.

\$\endgroup\$
0
\$\begingroup\$

PRINDEAL, 21

a c 
 i n 
 r 
 c
a r 
 p n 
 c 
 r

Preceds each number printed with n = , because it is not possible to print without such a prefix in PRINDEAL.

\$\endgroup\$
0
\$\begingroup\$

PHP, 95 74 64 bytes

<?for($n=_;;print strrev($n))for($i=0;!$n[$i]=($n[++$i]+1)%10;);

or

<?for($n=_;;print strrev($n))while($i*=!$n[$i]=($n[++$i]+1)%10);

http://php.net/bc_installation: These functions are only available if PHP was configured with --enable-bcmath.
http://php.net/gmp_installation: In order to have these functions available, PHP must be compiled with GMP support by using the --with-gmp option.

So I just implemented bcadd ... sort of. Uses underscore as separator.

\$\endgroup\$
0
\$\begingroup\$

stacked, 12 bytes

[1+out]0 ani

Try it online!

This works as follows:

[1+out]0 ani
[     ]0 ani   in an interval of 0 seconds, do:
 1+            add 1 to the counter
   out         output it
\$\endgroup\$
0
\$\begingroup\$

REXX, 34 bytes

numeric digits 39
do #=0
say #
end

(If your interpreter is set to a suitably large default precision, you may skip the numeric digits line.)

\$\endgroup\$
0
\$\begingroup\$

VBA, 157 103 bytes

The Variant / Decimal data type in VBA only allows up to 29 digits of precision. 2^128 is 39 digits long so we have to smoosh two variables together. Variant is the default for any undeclared variable so that's easy. Every time the right-hand side (a) rolls over, set it back to 0 and iterate the left-hand side (b) by 1. To handle leading zeros, we set the format to be 28 digits long the first time that a rolls over. This has a smaller max than the string method but it's well above 2^128.

Sub c()
Do
a=a+1
If a=10^28Then b=b+1:a=0:f=Replace(Space(28)," ",0)
Debug.?b &Format(a,f)
Loop
End Sub

Once it's formatted for humans, it looks like this:

Sub c()
    Do
        a = a + 1
        If a = 10 ^ 28 Then b = b + 1: a = 0: f = Replace(Space(28), " ", 0)
        Debug.Print b & Format(a, f)
    Loop
End Sub

Previous method using strings:
It starts with the right-most character, converts it to a number, and then keeps moving left until the result is less than 10.

Sub d()
s="1"
Do
Debug.?s
For i=Len(s)To 1Step -1
c=Val(Right(s,1))+1
s=Left(s,i-1)
If c<10Then Exit For
If i-1Then r=r &"0"
Next
s=s &c &r
r=""
Loop
End Sub
\$\endgroup\$
2
  • \$\begingroup\$ I am curious to see if there may be a LongLong (64 bit int) data type solution for vba and this problem - though admittedly I cannot think of one at this time \$\endgroup\$ Mar 31, 2017 at 7:50
  • \$\begingroup\$ @TaylorScott You were right, there totally was. I had thought it would be too awkward to squish them together but it ended up being shorter. \$\endgroup\$ Mar 31, 2017 at 14:24
0
\$\begingroup\$

QBIC, 8 bytes

{n=n+1?n

Explanation:

{       Start an infinite loop
n=n+1   Increment n (starts at 0)
?n      PRINT n
        Loop closed implicitly

Runs better tan expected. QBIC, when run in QBasic 4.5 on DOSBOX handles numbers up to and including 2^128 quite well, but it has a tendency to use scientific notation for the bigger numbers.

\$\endgroup\$
0
\$\begingroup\$

Fourier, 10 bytes

(i^~io32a)

A very simple program that loops forever.

Try it on FourIDE

Note, the program on FourIDE has been modified to make it more readable: it has a delay of two seconds between each number

\$\endgroup\$
2
  • \$\begingroup\$ Will this work up to 2^128? \$\endgroup\$ May 1, 2017 at 18:37
  • \$\begingroup\$ @quartata I would have thought so \$\endgroup\$
    – Beta Decay
    May 1, 2017 at 19:06
1
4 5
6
7 8

Your Answer

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

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