71
\$\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\$
  • 29
    \$\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 '15 at 14:25
  • 6
    \$\begingroup\$ For future challenges, may I recommend the sandbox such that all these details could be sorted out before people start posting answers? :) \$\endgroup\$ – Martin Ender Nov 14 '15 at 14:32
  • 3
    \$\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 '15 at 7:41
  • 5
    \$\begingroup\$ Can we have leading zeroes in the output? \$\endgroup\$ – Paŭlo Ebermann Nov 15 '15 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\$ – gregsdennis Nov 15 '15 at 20:45

190 Answers 190

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\$
  • \$\begingroup\$ Will this work up to 2^128? \$\endgroup\$ – a spaghetto May 1 '17 at 18:37
  • \$\begingroup\$ @quartata I would have thought so \$\endgroup\$ – Beta Decay May 1 '17 at 19:06
0
\$\begingroup\$

Braingolf, 15 bytes [non-competing]

V# R1[l!_v!@R>]

Explanation:

V# R1[l!_v!@R>]
V               Creates new stack and switches to it
 #<space>       Pushes 32 (ASCII value of a space) to stack
   R            Return to main stack
    1           Push 1
     [........] do-while loop. Will always run once, then checks if 
                first value in stack is 0 after each loop, if it is,
                breaks out of the loop
      l         Push current length of stack
       !_       Print last item on stack without popping
         v      Switch to next stack
          !@    Print last item on stack as ASCII char without popping
            R   Return to main stack
             >  Move last item to the start of the stack
\$\endgroup\$
0
\$\begingroup\$

Groovy, 25 bytes

for(i=1g;;i++){println i}

Try it online!

Groovy allows numeric literals of type BigInteger or BigDecimal to be declared by using the suffix g. BigNumber types are arbitrary length/precision numbers. This code declares a BigInteger then continuously prints (with a newline) and increments it.

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

Check, 13 bytes

 >1#v
#<)##p#

Check is my new esolang. It uses a combination of 2D and 1D semantics.

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

tcl, 23

while 1 {puts [incr i]}

demo

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

Jelly, 3 bytes

Non-competing as Jelly is newer than this challenge.

‘Ṅß

Try it online!

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

8th, 31 bytes

Code

: f n:1+ dup . cr recurse ; 0 f
\$\endgroup\$
0
\$\begingroup\$

Casio Basic, 27 bytes

For 1⇒z To ∞
Print z
Next

The largest integer the fx-CP400 can store precisely is a bit over 22032, or 611 digits. Chances are it'll keep counting up to 101000 before overflowing, though.

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

Cubically, 12 11 bytes

R3D1R1(+0%6)

Explanation:

R3D1R1       get UP's faceval to 1
      (      open loop that can always be jumped to
       +0     add 1 to notepad
         %    print notepad
          )  loop
\$\endgroup\$
0
\$\begingroup\$

RProgN 2, 6 bytes

1]p1+{

Try it online!

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

Swift 4, 146 bytes

var a=[0];while true{var e=a.count-1;print(a.reduce("",{$0+"\($1)"}));while e>=0{a[e]+=1;if a[e]<10{break};a[e]=0;if e==0{a.insert(1,at:0)};e-=1}}

The code compiles as is from a .swift file using swiftc. The program outputs numbers in decimal form, and given enough time it will output 2128 (and beyond).

Ungolfed (and slightly modifieda) version: Swift Sandbox.


a: Starts just shy of 1040 (larger than 2128) and ends execution at 1040 exactly.

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

Husk, 1 byte

N

Try it online!

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

Anyfix, 6 bytes

This was intended to be a language that allowed programs to be prefix, infix, and postfix, sometimes all at once. However, because of the way it works, dyads and tryads will default to prefix and monads will default to postfix. It's quite confusing. (Inspired by Jelly and 05AB1E)

1¿"Ƥ‘»

Explanation

1¿"Ƥ‘»  Program
1         Push 1 onto the stack
 ¿       If the stack is non-empty, then look at the top of the stack off and execute the next token if that value is truthy
  "Ƥ‘"»  ComboToken:
  "      Duplicate the top of the stack
   Ƥ     Pop and print with a trailing newline
    ‘    Increment the value

Since this is written in Python, it can easily count up to way past 2 ** 16384... hehe

This language will be on TIO sometime in the future once Dennis has some time to spare.

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

><>, 15 Bytes

0>1+:n\
 \og10/

Start with zero. Add one, print the value, and then print the character at (0,1) in the codebox, which is a space. Repeat ad nauseam.

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

Implicit on TIO, 6 4 bytes

(.%ß

This requires that the input box on TIO is empty.

(.%ß
(...   « loop while top of stack truthy                       »;
 .     «  increment top of stack (read input if stack empty)  »;
  %    «  print integer                                       »;
   ß   «  print space                                         »;
       « just kidding, loop forever                           »;

Try it online!

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

TrumpScript, 117 bytes

am is 1000001
an is 1000000
As long as,an is an?;:
say am minus an
am is,am plus 1000001 minus an;!
America is great.

Try it online!

What, there was no TrumpScript submission? Well, now there is. I don't think we can golf it any further though.

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

ALGOL 68 (Genie), 58 54 bytes

BEGIN LONG LONG INT a:=0;WHILE1=1DO print(a+:=1)OD END

Try it online!

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

JavaScript (ES6), 56 Bytes, optimized from user81655

for(n=[],i=99;;)if(n[i]=-~n[i--]%10)alert(n.join``,i=99)
\$\endgroup\$
0
\$\begingroup\$

Pyt, 6 bytes

1`Đƥ⁺ł

Explanation:

1           Push 1
 `   ł      Loop while top of stack is not 0
  Đƥ        Duplicate top of stack, then print
    ⁺       Increment top of stack

Try it online!

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

brainfuck, 31 bytes

->+>+[[<.+]->-[->-]+>[->]<[>]+]

Try it online!

Prints the binary value of each number with char code 1 as 1, null byte as 0 and ÿ as the separator.

The output is the exact same as the previous brainfuck answer, however this can go as high as 2^29998 in the 300000 available brainfuck cells and is 13 bytes shorter.

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

Ace, 4 bytes (non - competing)

vIPO

Try it online!

This is a new language I am working on, plan on it officially being a language by August 2018

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

tinylisp, 34 bytes

(d f(q((n)(i(disp n)1(f(a n 1
(f 1

The program defines a function f that takes one argument n, prints it, and recurses on n+1; then it calls f with an initial argument of 1. Because tinylisp has tail-call optimization, infinite recursion is not a problem--the implementation handles it as an infinite loop instead.

Try it online!

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

Julia 0.6, 30 bytes

for i=1:big(2)^999 @show i end

Outputs lines like i = 1, i = 2. If the i = is disallowed, then for i=1:big(2)^999 println(i) end is 32 bytes. This will count to 2^999, well over the 2^128 required. You can increase that significantly with no additional bytes with big(2)^9^9 but it times out in TIO. big(2) is an arbitrary precision integer, and all math with smaller integer types promotes to arbitrary precision.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ "Your program must count at least as far as 2^128 (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." While the title says forever, the rules do not. Also 2^9^9 is still longer than the age of the universe if you count at one tick per femtosecond. \$\endgroup\$ – gggg Jan 29 '18 at 16:38
  • \$\begingroup\$ My bad, I didn't read through the challenge enough I guess. \$\endgroup\$ – MD XF Jan 29 '18 at 17:41
0
\$\begingroup\$

Forked, 5 bytes

1+%A!

Try it online!

  • 1 - push 1
  • + - add top two stack value together
  • % - print top of stack as integer
  • A - push 10 (ASCII newline)
  • ! - print as ASCII and pop

Forked is two-dimensional and the IP wraps, so this loops infinitely.

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

rk, 22 bytes

int i
a-
print: ++ i
a

Requires the -e flag. Try it online!

Ungolfed:

rk:start
  int i
  while 1 do
    print: ++ i
  done
rk:end

The golfed version uses gotos (goto_name- to declare, goto_name to goto).

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

MY-BASIC, 27 bytes

While 1
i=i+1
Print i;
Wend

Try it online!

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

uBASIC, 16 bytes

Anonymous function that takes no input and increments and outputs i forever. Note that the trailing newline is required for this code segment to function

1?i:i=i+1:GoTo1

Try it online!

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

Visual Basic .NET (.NET Core), 76 bytes

Declared subroutine that takes no input, and increments then outputs i forever.

Module M
Sub Main
Dim i
Do
i+=1
Console.WriteLine(i)
Loop
End Sub
End Module

Try it online!

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

Foam, 22 bytes

0[[+1 : <#]/-\ : ~]: ~
\$\endgroup\$
0
\$\begingroup\$

Noether, 6 bytes

(!iP?)

Try it online!

The online link has a delay of 1 second added to prevent the interpreter from freezing up.

\$\endgroup\$

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