88
\$\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
    Commented 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\$ Commented 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
    Commented Nov 15, 2015 at 7:41
  • 5
    \$\begingroup\$ Can we have leading zeroes in the output? \$\endgroup\$ Commented 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\$ Commented Nov 15, 2015 at 20:45

232 Answers 232

1 2 3
4
5
8
2
\$\begingroup\$

Jelly, 3 bytes

Ṅ‘ß

Try it online! Be careful though, since this is a long output after all!

Ṅ            Print with newline
‘            Increment
ß            Call current link (function) again

Since there are no input arguments, the initial argument is taken to be 0.


Previous version:

Ṅ‘$ÐL

Try it online!

Ṅ            Print with newline
‘            Increment
$            Combine previous two
ÐL           Loop until
\$\endgroup\$
1
  • \$\begingroup\$ This should be ‘Ṅß instead, since the implicit argument is 0, so incrementation goes first. As it is right now, it's not valid at all. Your previous version also needs to be fixed. \$\endgroup\$ Commented Oct 6, 2016 at 15:46
2
\$\begingroup\$

Perl, 20 19 bytes

perl -E 's//0/;say while++$_'

Magic string increment makes this work without bigint

\$\endgroup\$
1
  • \$\begingroup\$ You don't need the s//0/;; say while++$_ works by itself \$\endgroup\$
    – Mark Reed
    Commented Jan 2, 2023 at 18:42
2
\$\begingroup\$

Retina, 20 bytes

<empty line>
39$*0
+:T`9d`d`.9*$

Can count to 10**39-1.

Try it online!

Without leading zeroes (can count beyond 10**39): 23 bytes

+:(T`9d`d`.9*$
^0*$
1$&

Try it online!

13 bytes if pre-initialized with input as 39 "0"s:

+:T`9d`d`.9*$

Try it online!

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

Retina, 73 bytes

No unary! Taken mostly from this SO answer. As noted by Martin, this code doesn't work in the newest version of Retina. Change (;`(\d+) to {;`(\d+) for it to work.

:`^$
1
(;`(\d+)
0$1~01234567890
:`^0(?!9*~)|(\d)(?=9*~\d*?\1(\d))|~\d*
$2

Try it online

It is much shorter if it was able use unary.

\$\endgroup\$
4
  • \$\begingroup\$ It doesn't work. \$\endgroup\$
    – Leaky Nun
    Commented Apr 23, 2016 at 23:27
  • \$\begingroup\$ @KennyLau It used to at the time. I made some changes since then which broke backward-compatibility. You'd just need to turn the ( into a { to fix it. \$\endgroup\$ Commented Apr 25, 2016 at 13:55
  • \$\begingroup\$ Perhaps you should include a version number in the answer header, then. \$\endgroup\$
    – msh210
    Commented Apr 25, 2016 at 18:34
  • 2
    \$\begingroup\$ @msh210 I'm not going to edit every single Retina answer I've ever posted every time Martin updates Retina. Look at the date I posted the answer, and use the version of Retina that corresponds to it. \$\endgroup\$
    – mbomb007
    Commented Apr 25, 2016 at 18:37
2
\$\begingroup\$

UGL, 14 bytes

cul$oucuuu$*O:

Try it online!

How it works:

cul$oucuuu$*O:
cu              #i=1
  l          :  #while i:
   $o           #    print(i)
     u          #    i++
      cuuu$*O   #    print(chr(9))
\$\endgroup\$
2
\$\begingroup\$

Oracle SQL, 42 Bytes

select level from t connect by level=level
\$\endgroup\$
2
\$\begingroup\$

Braingolf, 10 bytes

1[!_2+# @]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 9 bytes

{∇1+⎕←⍵}1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Aceto, 5 bytes

IkpnO

Explanation:

Aceto follows a Hilbert Curve on a square program. If the program is not square, spaces are added to the top and right. (spaces do nothing) I

I - pops a value, increments it, and pushes it back on
k - makes the stack 'sticky', meaning that when popping a number, instead of removing the value it copies it
p - prints the top value
n - prints a newline
O - Returns the program back to the beginning

Also, can anyone explain to me why 'k' is needed? Shouldn't 'I' do the job just fine? When I omit 'k', it prints out lines and lines of '1's.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Because p pops the value off the stack. \$\endgroup\$
    – L3viathan
    Commented Dec 12, 2017 at 13:55
  • 1
    \$\begingroup\$ Glad to see people using it! \$\endgroup\$
    – L3viathan
    Commented Dec 12, 2017 at 18:28
2
\$\begingroup\$

Alumin, 6 bytes

hqhanp

Alumin is a new language, made by @Conor O'Brien.

Explanation:

hqhanp
h      # Push 1
 q   p # Begin/end loop
  ha   # Push 1, add top two stack elements together (increments top)
    n  # Prints the top of stack as a number
\$\endgroup\$
2
\$\begingroup\$

Wumpus, 5 bytes

)=O
N

Try it online!

Explanation

The program runs )=ON in an infinite loop (see the "cat" answer for why that is).

)   Increment the top of the stack. Initially, this turns an implicit zero
    into an explicit one.
=   Duplicate.
O   Print as a decimal integer.
N   Print a linefeed.
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 4 bytes

ℕ₁ẉᶠ

Explanation

ℕ₁      --  Get a integer which is at least 1
  ẉᶠ    --      and print all possible values (ordered from 1 to inf)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Deoxyribose, 35 29 bytes

ATGCCAGAAAAACATAACGGTTTAAATCC

Explanation:

ATG
CCA     (*) Divide (null/null yields 1)
  GAA     Dupe
  AAA     Pop
  CAT     Push
    AAC     1
  GGT     Move 
  TTA     Plus
AAT     Loop
  CC      *

I feel like this can get shorter.

Edit: Saved 6 bytes by making creative use of division of two empty stacks.

\$\endgroup\$
2
\$\begingroup\$

CJam, 7 bytes

X{_p)}h

Really feels like there should be a way to take off one or two bytes, but oh well.

X           Initialize the stack with 1
 {          Begin loop
  _p        Duplicate top of stack and print
    )       Add 1
     }h     Loop while top of stack is truthy (non-consuming)
\$\endgroup\$
2
\$\begingroup\$

Arn, 4 bytes

ëçJx

Explanation

Unpacked: [1{+1 A sequence in arn is defined within [..]. A sequence is made up of hard-coded digits, a block determining how future entries are calculated, and (possibly) an array pointing towards the length of the sequence.

In this case, it is simply defining a sequence where the first entry is 1, and every future entry is 1 higher. Because of Arn's implied closings, the final }] can be removed. Arn uses BigInts.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 3 bytes

∞€,

Try it online!

tio.run truncates the input, but in theory this would count forever. Also on tio.run, there's a [ at the beginning which I don't know how to get rid of.

Explanation

∞    # push infinite list ([1, 2, …, ∞])
 €   # pop a; apply next command for each in a
  ,  # pop a; print(a) (with newline)
\$\endgroup\$
2
  • \$\begingroup\$ You can get rid of the [ in the output by using the argument-flag --no-lazy. Has something to do with printing within a map with lazy-loading in Elixir, where 05AB1E is built in. But the --no-lazy will disable this lazy-loading, so you won't have this in the output. :) \$\endgroup\$ Commented Jan 3, 2020 at 8:54
  • \$\begingroup\$ @KevinCruijssen oh, thanks! does it still count with the [? \$\endgroup\$ Commented Jan 4, 2020 at 23:24
2
\$\begingroup\$

JavaScript (ES6), 22 21 bytes

for(i=0n;;)alert(++i)

This uses BigInts, which can go to high integers without losing accuracy. Per earlier submissions, I'm assuming that using alert, with its automatic separation between popups, means that outputting an extra character is not necessary.

\$\endgroup\$
2
  • \$\begingroup\$ You can save a byte by doing the ++ in the alert call, so it becomes: for(i=0n;;)alert(++i) \$\endgroup\$ Commented Mar 16, 2021 at 20:11
  • 1
    \$\begingroup\$ @Luca_Scorpion Done, thanks. \$\endgroup\$
    – Yair Rand
    Commented Apr 8, 2021 at 0:40
2
\$\begingroup\$

ABC, 4 bytes

acal

a increments the accumulator at start, c outputs it, then a increments again, and l loops to beginning.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 16 14 bytes

≔¹ξW¹«PIξ≦⊕ξR⁰

Try it online! Link is to verbose version of code.

-2 bytes thanks to Neil in the chat

I think the The output with trailing separator is valid.

Explanation:

≔¹ξ

Assign 1 to X

W¹«

Start Infinite Loop

PIξ

Print X, In decimal to avoid memory error

≦⊕ξ

Increment X

R⁰

Refresh

\$\endgroup\$
2
\$\begingroup\$

Seed, 10 bytes

4 28895602

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Unix coreutils jot, 7 bytes

jot 0 1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

(,), 23 21 Chars or 4.16 Bytes

(,,()((),(())()),,())

TIO

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

PHP, 31 bytes

<?for(;;)echo$i=bcadd($i,1),~õ;

I assume default settings, as they are without an .ini (you may disable your local .ini with the -n option).


Sample Usage

$ php -n countup.php
\$\endgroup\$
8
  • \$\begingroup\$ You can save 2 bytes by saying they should run it using php -r '...', \$\endgroup\$
    – Kenney
    Commented Nov 15, 2015 at 2:24
  • \$\begingroup\$ @Kenney r'' should cost three. It also seems to violate the constraint, "Your program must be a full, runnable program, and not a function or snippet." \$\endgroup\$
    – primo
    Commented Nov 15, 2015 at 8:07
  • 1
    \$\begingroup\$ As far as I am concerned, <? is at the templating level, and your code only really starts at for. It doesn't really say how the program is to be fed into the interpreter. But you're probably right! \$\endgroup\$
    – Kenney
    Commented Nov 15, 2015 at 15:50
  • \$\begingroup\$ @Kenney the problem with that interpretation, is solutions that have anything outside the code tags, and also the use of <?=, e.g. codegolf.stackexchange.com/a/12569 \$\endgroup\$
    – primo
    Commented Nov 16, 2015 at 0:53
  • 1
    \$\begingroup\$ @DougMcLean bcadd is necessary, because when the value exceeds PHP_MAX_INT, or the exactly representable floating point range (2^56), whichever comes first, it would fail to display an integer. \$\endgroup\$
    – primo
    Commented Nov 18, 2015 at 2:29
1
\$\begingroup\$

Julia, 35 bytes

i=big(0);while 1>0 println(i+=1)end

This uses Julia's GMP-based arbitrary precision integers.

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

C++, 104 bytes

#include<cstdio>
int a,c[50]={-38};int main(){for(a=49;++c[a]>9;c[a--]=0);for(b:c)putchar(b+48);main();}

A translation of my C answer. This uses a terse range-based for loop, which is a C++1z feature briefly added to some compilers but later voted out of C++17. clang++ 3.5.2 compiles my code fine, but I'm not sure if it counts as "C++". I can change it to for(int b:c) if it's deemed invalid.

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

Go, 104 99 95 bytes

package main
import."math/big"
func main(){a:=&Int{}
for{println(a.Add(a,NewInt(1)).String())}}

Not terribly golfed yet, I might take the time to refine it later.

Note that this will not run on the playground since it runs forever.

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

VB.NET, 113 99 bytes

Well, pretty straightforward for .NET Framework 4. Needs a reference to System.Numeric.dll:

Module m
Sub Main
Dim i As Numerics.BigInteger
do
i+=1
Console.WriteLine(i)
loop
end sub
End Module

Not exactly original, but hey, even not that much longer than the C# answer ;-)

Thanks Berend for making me dig a little deeper into the compilation process, saving 14 bytes in the process.

\$\endgroup\$
9
  • \$\begingroup\$ You can remove System. (twice), for an extra 14 bytes. \$\endgroup\$
    – Berend
    Commented Nov 16, 2015 at 10:28
  • \$\begingroup\$ @Berend No I can't. It may work when compiled in Visual Studio due to automatic namespace imports, but I consider that cheating ;-) Here I would need to add Imports System to the code, which adds the 14 bytes right back in \$\endgroup\$
    – Jens
    Commented Nov 16, 2015 at 12:51
  • \$\begingroup\$ How do you compile then? Both Visual Studio (2013) and MSBuild don't care about the missing system. \$\endgroup\$
    – Berend
    Commented Nov 16, 2015 at 12:59
  • \$\begingroup\$ I plug the code above directly into the codedom compiler. Figured it is the most "raw" form of compiling vb.net. Sounds like a meta discussion in all honesty :) \$\endgroup\$
    – Jens
    Commented Nov 16, 2015 at 13:05
  • 1
    \$\begingroup\$ Maybe a silly question, but as requirements are at least 2^128 ; does BigInteger really required ? if not, you could gain an extra 22 bytes by declaring i with Dim i# (using double identifier type character) \$\endgroup\$
    – Sehnsucht
    Commented Nov 18, 2015 at 2:49
1
\$\begingroup\$

Python 2, 24 bytes

Same method as the Python 3 answer.

i=1
while 1:print i;i+=1
\$\endgroup\$
2
  • \$\begingroup\$ I know I'm late but never mind; here's my version that's not as golfed, but uses an interesting approach: i=0\nwhile i<[]:i+=1;print i, 27 bytes. I abused the fact that every integer is lesser than a list \$\endgroup\$ Commented Feb 7, 2023 at 13:18
  • \$\begingroup\$ @UndoneStudios Nice. That's pretty close to what I had, and pretty succinct as well. \$\endgroup\$
    – mbomb007
    Commented Feb 7, 2023 at 19:32
1
\$\begingroup\$

Mouse-2002, 17 bytes

aa:(a.!"!"a.b+a:)

It seems there's a flaw in the interpreter which messes up using the stack for while loops, I dunno, so we use a variable instead.

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

Forth, 67 71 bytes

Forth doesn't have a builtin word for exponentiation, so we need to define one, because loops in Forth, which are frowned upon, need clearly defined bounds.

According to the language's original spec, this will segfault or run out of memory when it gets to 32,767 but that's not the case for mighty gforth! Instead, this runs out of memory whenever it's unable to continue storing numbers, as the stack's allocation is not dynamic like the heap's is, and do's operands (the limit and index) are put on the heap, not the stack, else they would interfere with the stack.

Essentially, it will stop at the same point every other answer here will.

The loop construct normally looks like 10 0 do ... loop where 10 is the limit and 0 is the index, i.e starting point.

In this example, we need to add one to the upper bound before counting there.

Edit: apparently do is a "compile-only" word, and it can only be found inside macro definitions.

: p over swap 1 ?do over * loop nip ;
: c 2 128 p 1 + 0 do i . loop ;
c
\$\endgroup\$
1 2 3
4
5
8

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.