It may sound strange, but we haven't got ONE challenge for counting from 1 to n, inclusive.

This is not the same thing. That one is a (closed) not well-explained challenge.
This is not the same thing. That one is about counting up indefinitely.


Write a program or function that prints every integer from 1 to n inclusive.


  • You can get n any way.
  • You can assume that n will always be a positive integer.
  • You can get n in any base, but you should always output in decimal.
  • Output must be separated by any character (or pattern) not in 0123456789. Non-decimal leading or trailing characters are allowed (for example when using arrays such as [1, 2, 3, 4, 5, 6]).
  • Standard loopholes are denied.
  • We want to find the shortest approach in each language, not the shortest language, so I will not accept any answer.
  • You must update your answer(s) after this edit, answers posted before the last edit must comply with the change rule about standard loopholes (I didn't want to deny them, but I didn't want to make the community roar, so I denied them).
  • You can use any post-dating language version (or language). You cannot use any language or language version made just for this challenge.



  • Your program must be able to count at least up to 18446744073709551615 (2^64-1). For example, if a new datatype is the only way to support big integers, you must construct it. If your language does not have any way to support huge integers up to 2^64-1, the upper limit of that particular language must be supported instead.

EDIT: I've changed the limit from 2^64 to 2^64-1 to allow more answers.

EDIT: I made the 2^64-1 rule a bonus, since there has not been much interest in this challenge. If your answer supports 2^64-1, you can now edit it to include the bonus. Also, you can post an answer not supporting it, if it is shorter.

  • 6
    \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Apr 25 '16 at 12:20
  • \$\begingroup\$ "You can get n any way." Does that mean we can assume n to be saved in a variable? \$\endgroup\$ – flawr May 21 '16 at 20:50
  • \$\begingroup\$ @flawr You can get n any way. You can save it in a variable, but it must not be hardcoded. \$\endgroup\$ – Erik the Outgolfer May 22 '16 at 10:26
  • \$\begingroup\$ It might be useful to link to the accepted I/O methods \$\endgroup\$ – Ephphatha Jun 2 '17 at 12:26
  • \$\begingroup\$ @Ephphatha Yes it probably is, this challenge is from the old times where I was an utter newb. \$\endgroup\$ – Erik the Outgolfer Jun 2 '17 at 12:29

130 Answers 130


Mouse-2002, 47 bytes

?1(&DUP " "!&DUP &ROT &DUP &ROT =[0^]&SWAP 1+)$

I do not know how to replace &DUP &ROT &DUP &ROT ...&SWAP, it's intended to duplicate the two top elements of the stack (...A B → ...B A A B), perform an operation on them, and then restore the stack to what it was before (...A B). Here is the explanation:

  • ? pushes a number (base 10) from input (... → ... <input num>)
  • 1 pushes 1 (... → ... 1)
  • ( starts an infinite loop
  • &DUP duplicates the ToS (... A → ... A A)
  • " " prints
  • ! prints the ToS as a number (... A → ...)
  • &DUP duplicates the ToS (... A → ... A A)
  • &ROT rotates the top three stack elements (... A B C → ... B C A)
  • &DUP duplicates the ToS (... A → ... A A)
  • &ROT rotates the top three stack elements (... A B C → ... B C A)
  • = checks if the ToS and the SToS are equal
  • [ skips to the matching ] if the ToS is not positive
  • 0 pushes 0 (... → ... 0)
  • ^ breaks out of the innermost loop if the ToS isn't positive (... A → ...)
  • &SWAP swaps the top two stack elements (... A B → ... B A)
  • 1 pushes 1 (... → ... 1)
  • + adds the top two stack elements (... A B → ... A+B)
  • ) closes the innermost loop
  • $ terminates the program

R, 35 bytes

This statistician's language also has a scan() function, but requires information about how many characters it will receive, and providing a value for that would either limit the highest possible x, or require a very, very long argument.

for(x in 1:readline()){cat(x,'\n')}


SQL, 108 Bytes



(It fails if I put it all on one line!)


@n INT
SET @a=1;
    WHILE @a<=@n 
        PRINT @a;
        SET @a=@a+1;





C++, 89 83-20%=66.4 bytes

auto f=[](auto n){for(decltype(n)i=0;i++<n;)std::cout<<i<<",";};

Should work for up to 2^64-1


R, 3 bytes


A built-in function that, when given a single positive integer n as argument, yields 1 to n.


PostL (Non-competing) 20 bytes

I made this language after the challenge was posted; thus, this answer is non-competing. It's a postfix-notation stack-based language based purely on the stack, a single variable, and many operators, and its interpreter is currently written in Java.



1                    Push 1 onto the stack
 N                   Negate it (gives -1; negative literals don't exist)
  0                  Push 0 onto the stack
   ,$                Take the input and parse it to an number
     )               Take the range, counting by -1, to 0, starting at whatever the input was
      B>             Check whether or not the stack is empty, and push that onto the stack (0 for not, 1 for empty)
        \`           Push on a SKIP processor flag, and move the value of the emptiness of the stack back to the top (swap the top two)
          ]`         Push on a TERMINATE processor flag, and swap the top two.
            =        Switch, based on whether or not the stack is empty. If not, retrieve the SKIP flag, and if so, retrieve the TERMINATE flag.
             >       Process whatever flag is there. If it's a SKIP flag, it doesn't do anything, but if it's a TERMINATE flag, it means that the stack is empty, and it terminates.
              1+     Add 1 (because the range is 0 to n - 1)
                .    Pop it off the stack and print it
                 6;> Go to index 6 (where the program checks if the stack is empty).

D, 55 bytes

(ulong n){import std.stdio;foreach(i;1..n+1)i.writeln;}

GameMaker Language, 38 bytes

  • \$\begingroup\$ Couldn't you do for(i=0;i<argument0;show_message(++i)) for -3 bytes? \$\endgroup\$ – Erik the Outgolfer Jun 18 '16 at 9:39
  • \$\begingroup\$ I suppose, thanks for the tip. \$\endgroup\$ – Timtech Jun 18 '16 at 21:00

><> (Fish), 16 bytes

Solution 1


Try it here!

Solution 2


Solution 3


Wow can't believe I don't see any ><> submissions, have 3! All the solutions are 16 bytes, just structured differently (the top one would be my "real" submission I guess). Simply place n on the stack and away you go!

Basic Explanation

0                push 0 to the stack
 1+              add 1
   :             copy the result
    {            shift stack left
     :           copy n
      ?!;        if n is 0, end
         1-      subtract 1 from n
           r     reverse the stack
            n    output a number
             ao  output a newline
               ! skip the next instruction (0)

Long Explanation

As I made these in reverse order, I'll explain them in reverse order (probably be easier to follow)... These programs are pretty simple, I invite you to check out what the instructions do here.

Explanation 3


This simply places 0 onto the stack (let's call it x), then tells the fish to move downwards, then to the right. We now have [n, x] on the stack, we'll place _s before temporary versions of these variables.

The fish immediately must swim over 1+:: which adds 1 to x and then duplicates it twice, giving us the stack of [n, __x, _x, x]. {:{ shifts the stack to the left, copies n (because it's now on the end of the stack), then shifts the stack left again giving us [_x, x, n, _n, __x].

Now the fish must figure out (?;, which starts with (. ( pops _n and __x off the stack and checks to see if _n is less than __x, pushing 1 to the stack if so, and 0 otherwise. Then ?; pops that result off the stack, executing ; (end of program) if it found a 1, and skipping over ; otherwise. This will end up with a stack of [_x, x, n].

If the fish continues forwards it will encounter rnao because of the wrap-around, this simply tells the fish to reverse the stack, putting _x at the front of the stack, then outputting it as a number with the instruction n. ao simply places a newline on the stack (character 10 or a), and then outputs it.

That's it!

Explanation 2

This is pretty much the same as Explanation 3. It doesn't have the fish mirror/movement instructions because it's only one line. It can do this because the ! at the end of the line causes the fish to skip the 0 at the beginning during the wrap-around.

Explanation 1

This solution is slightly different. I was hoping to save some bytes (you can see I didn't).

You can see it deviates pretty early with :{:, instead of copying x twice in the beginning I only do it once, then I only do one stack shift left instead of two.

I also don't use (?; and instead use ?!;, this is because n is actually being decremented each loop instead of just being compared to x. The ! is because ? actually does the opposite of what we want compared to before, so we invert its behaviour by executing ! when n>0, skipping the ;. Then the fish swims over 1- which simply subtracts 1 from n, and the program carrys on the same as the others!


Ruby, 12 bytes

Lambda. Splats a range and prints that. Try it online!

->n{p *1..n}

EDIT: I can't personally confirm that this counts up to 2^64-1, since it runs out of memory, but I believe Ruby uses bigints. If someone can confirm this, the score with bonus is 9.6.


Silicon, 3 bytes


1 pushes... well, 1.

I gets input and converts it to an integer before pushing it onto the stack.

\ takes the two top stack items and runs it through the Python range() function.

Output is implicit.


Java, 163 (163 - 20% = 130.4, not sure if applies for bonus) bytes

enum h{;public static void main(String[]a){long i=1;while(Long.compareUnsigned(i,Long.parseUnsignedLong(a[0]))!=1)System.out.println(Long.toUnsignedString(i++));}}

Uses Java 8 functions to use unsigned longs which can store numbers up to 2^64-1(according to this page). Number is taken from first number

Ungolfed version with comments:

enum h {

    public static void main(String[] a) {
        long i = 1;                                                         // Counter
        while (Long.compareUnsigned(i, Long.parseUnsignedLong(a[0])) != 1)  // While counter is smaller or equal to input
            System.out.println(Long.toUnsignedString(i++));                 // Show counter as unsigned long(to be in correct range)

REXX, 27 bytes

arg #
do i=1 to #
  say i

Maximum number is dependent on NUMERIC DIGITS setting.


Modified processing. 39-7.8 = 31.2 bytes

var n=0;for(var I=0;I<n;i++){print(I);}

Uses this version of processing. In practice it cannot do it all the way until the max number but it could in theory. Also a side note it did say input as any way I wanted and that input is replaced by n.


Sinclair ZX80 BASIC, 4/8K ROM, ~38 bytes (4K ROM) listing

This was an edit of my initial entry, however there is a slight issue with it, which is explained below:

1 LET S=1
4 LET S=S+1

ZX80 output

Whilst there is nothing wrong with the logic, when the ZX80's DFILE (or screen) is full, the program will halt and you will be returned to direct mode, which will show your program listing.

In order to get around this, press T on the ZX80's 'keyboard' and, on the 4K ROM version at least, the command CONTINUE should appear. Press NEW LINE and the program will continue where you left off. You can only do this once you have returned to direct mode though.

As you may know, a ZX80 with an 'old ROM' (original 4K ROM) will only handle 16-bit signed integers so the range is limited and I'm not going to win the bonus points. If you enter a number <1 then it will print 1 then exit.

This will work in other variants of Sinclair and 8-bit BASIC. On the ZX Spectrum though, this can be done in just two lines, like:

1 LET s=1: INPUT n
2 PRINT s,;: LET s=s+1: IF s<=n THEN GO TO 2

Using a FOR..NEXT loop will condense the program further to just one line.

Fortunately, the Spectrum, like all other more sane 8-bit personal computers, allows <= and >= in your conditions. If the 4K ROM ZX80 allows this, I've not found out how to do it yet.

  • 1
    \$\begingroup\$ The challenge is about counting from 1 to n, not from n to m, so you only have one input. Also, input prompts are unneeded. I think you can also remove CLS. \$\endgroup\$ – Erik the Outgolfer Mar 29 '17 at 15:24

Newline NC 23 bytes


\nis a newline. Try it online Input is in the variable numbers, that is STDIN for newline.

  • \$\begingroup\$ Why can't you just use multiple lines for newlines? \$\endgroup\$ – HyperNeutrino May 30 '17 at 3:30
  • \$\begingroup\$ @HyperNeutrino uhhhh idk takes up way to much space to view i guess \$\endgroup\$ – Christopher May 30 '17 at 12:29
  • \$\begingroup\$ Okay. This way looks a bit unclear but up to you. \$\endgroup\$ – HyperNeutrino May 30 '17 at 12:57
  • \$\begingroup\$ @HyperNeutrino it is also how the current interpreter works \$\endgroup\$ – Christopher May 30 '17 at 13:02
  • \$\begingroup\$ Ah. Okay. Makes sense. Thanks for clarifying. \$\endgroup\$ – HyperNeutrino May 30 '17 at 13:08

Japt, 1 byte

Outputs an array of integers.


Try it online


JavaScript (ES6), 32 bytes

Outputs an array of integers


Try It

<input id=i min=1 type=number><p id=o>


Braingolf v0.7, 14 bytes

UV# R&,{_v!@R}


UV# R&,{_v!@R}  Implicit input of n
U               Replace stack with ascending range 1-n
 V#<space>      Create new stack, switch to it and push space character
    R&,         Return to main stack and reverse stack
       {.....}  Foreach/Map loop, runs for each item in stack
        _       Pop and output last item on stack
         v!@    Switch to next stack and print a space
            R   Return to main stack

Rust, 35 bytes

|n|for i in 0..n{print!("{} ",i+1)}

An anonymous function that does the task described. It can be one byte shorter if an inclusive range was used, but that's currently unstable. It is able to count to 2^64-1 (provided you call it with an u64 as argument).


Whitespace, 50 * 0.8 = 40 bytes (assumes n is on the stack)



Try it online!

This code assumes n is already on the stack as it saves bytes over taking it from STDIN. The TIO link has a header provided to take n from STDIN so it can be tested more easily. An extra no-op command (stnn ; slide 0) has been added to the header to work around a TIO quirk where a newline gets introduced when the sections are concatenated.

Whitespace numbers are arbitrary precision so this program exceeds the upper limit specified in the question. This uses a fixed number of stack elements so will not run into memory limits either.


(s - space, t - tab, n - newline)

; The first four instructions are provided as a convenience to get input
;  from STDIN and leave it on the stack
sssn  ; push 0
sns   ; dup
tntt  ; getnum - read number from stdin and save at address 0
ttt   ; retrieve value from address 0 and place on the stack

; bytecount starts here
sssn  ; push 0 - initialise i
nssn  ; loop:
ssstn ; push 1
tsss  ; add - increment i
sns   ; dup
tnst  ; putnum - display i as a number
sssn  ; push 0 - use null byte as a separator, shortest constant
tnss  ; putchar - display null byte, often displayed as a space
sns   ; dup - copy i
stsstsn ; copy 2 - copy n
tsst  ; sub - calculate i-n
nttn  ; jlz "loop" - continue looping if i < n

MY, 3 * 0.8 = 2.4 bytes

Here's the hex:

1A 49 27


1A - Take input as an integer, then push to stack
49 - Pop n; Push [1 ... n]
27 - Pop n; Output n (with a trailing newline)

MY is written in Python, where integers have no size limit.


Swift 3, 53 bytes

func c(n:UInt){var l:UInt=0;while l<n{l+=1;print(l)}}

Note: this only works on 64-bit devices. Here is a 32-bit device solution (57 bytes):

func c(n:UInt64){var l:UInt64=0;while l<n{l+=1;print(l)}}
  • \$\begingroup\$ You only have to support natural upper limit. \$\endgroup\$ – Erik the Outgolfer Jun 30 '17 at 17:06

Swift 3, 40 bytes

func f(n:UInt){for i in 1...n{print(i)}}

Uses Swift ClosedRange (1...n) type. Prints up to UInt maximum aviable


cQuents, 2.4 bytes (3 bytes - 20%)


Fairly simple, does exactly what the challenge wants. Since cQuents is implemented in Python, it works as long as Python integers can continue growing.

Try it online!


      Implicit input n
::    Mode: Sequence 2. Output the sequence up to n
  $   Each item in the sequence is the current (1-based) index
  • \$\begingroup\$ Note current version uses & instead of :: (saves a byte) \$\endgroup\$ – Stephen Feb 1 at 4:50

Retina, 18 bytes


Try it online!


Brainfuck, 24 bytes (-4.8 = 19.2)


Technically it can go as high as you want as long as you set your cell size high enough.

Also all I/O is in ascii.

How it works is first it takes input into the third cell, then it duplicates itself (self-destructively) into the next two cells, and taking one away from the second one, repeating that unit it reaches 0. Then, it prints 1 and then goes back two spaces and prints until it reaches the beginning, where we skipped two spaces so it would terminate.

  • \$\begingroup\$ Does not seems to work... instead it prints the characters with the ascii from 1 to the ascii of the input character. \$\endgroup\$ – leo3065 Mar 15 '18 at 8:13
  • 1
    \$\begingroup\$ Also all I/O is in ascii \$\endgroup\$ – vityavv Mar 15 '18 at 10:57

Charcoal, 4 bytes


Try it online!


I     Cast (recursively vectorizes)
  ⊕    Incremented
   …N Range from 0 to next input as number (excluding the number itself)

Implicit, 10 5 bytes


Try it online!

¡(%ß;)   implicit integer input
¡        push 1..n
 (...)   while top of stack truthy
  %       print
   ß      print space
    ;     pop

Old version:


Input is implicit. When the first : is reached, there's nothing on the stack to duplicate so it reads an integer. This code is therefore equivalent to $(:-1);(%;). Explanation:

$            read integer input
 (...)       do-while
  :           duplicate
   -1         decrement
      ;      pop the zero
       (..)  do-while
        %;   print and pop

Take the input 3:

$             read input (stack contains 3)
 (:-1)        push 2, 1, 0 (stack contains 3, 2, 1, 0)
      ;       pop zero
       (%;)   print 1, 2, 3

Try it online! (Nonworking with (:-1);(%;) until TIO's SimpleStack interpreter is updated)


ABCR, 16 (-20%=12.8) bytes


Output separated by null bytes.


i                   Read n to the register.
 BB                 Enqueue it to queue B twice.
   a                Since we need to count UP, we'll reset the register to 0.
    C               Save the current register value in queue C.
     .7             If not(the difference between the register (0) and n (from B)):
       2B           Clone n back to queue B, since calculating the difference
                        consumes the front of the queue
         c)q        Print the register value as a number, restoring its value 
                     from C and incrementing 
            O       Print out a null byte separator 
                       (the default value from queue A as a character)

             C      Store it back in C
              .x    If the difference between the register and n is 0, exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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