# Introduction

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.

# Challenge

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

# Rules

• 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.

# Bonuses

## 20%

• 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.

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

# V, 11 Bytes

é1@añYpñdd


Since this contains nasty UTF-8 and unprintables, here is a reversible hexdump:

00000000: e931 4061 f159 7001 f164 64              [email protected]


V is an unfinished language I wrote, but this is working as of commit 19. This answer was a little more verbose than I'd like, but that's mostly because V has no knowledge of integers, only strings. So it's a decent answer! This will work up to 2^64, but it will probably take a very long time.

To make my explanation easier to read/write, I will work with this "Human readable form", which is actually how you would type this in vim.

<A-i>1@a<A-q>Yp<C-a><A-q>dd


Explanation:

'Implicit: register "a" == arg 1, and any generated text is printed.

<A-i>1                       'Insert a single character: "1"
@a                     ' "a" times,
<A-q>       <A-q>    'Repeat the following:
Yp<C-a>         'Duplicate the line, and increment it
dd  'Delete the last line, since we have one too many.


If loopholes are allowed, here's a shorter version that prints 1 to n, but also prints a 0 (8 bytes):

é0@añYp


And in readable form:

<A-i>1@a<A-q>Yp<C-a>


This is shorter because the <A-q> at the end is implicit, so we don't need it if we don't have to delete the last line.

• It can take as long as it wants. Glad to see an answer to work with 2^64, especially with an unfinished language. +1 Apr 25, 2016 at 13:42
• I have changed the limit to 2^64-1 because standard loopholes are disallowed now, and I don't want to cut answers out too much. Apr 26, 2016 at 8:10

# JavaScript (ES6), 84 80 bytes

x=>{for(n=[r=i=0];r!=x;)(n[i]=-~n[i++]%10)&&alert(r=[...n].reverse(i=0).join)}


Input is a string. Outputs as alert. Change alert to console.log if you want to keep your sanity.

I tried a few different methods then went to the print to infinity question to see how the JS answer there did it and found that I was the one who answered it. *facepalm* This approach is actually shorter though, so it worked out fine.

## Hoon, 13 bytes

(cury gulf 1)


++gulf returns a list containing the numbers from a to b. Returned a curried function with 1 for a.

Usage:

> %.  9
(cury gulf 1)
~[1 2 3 4 5 6 7 8 9]

• I read this as "curry golf" :P Apr 27, 2016 at 21:57

# JavaScript (ES6), 99 103

String increment using regexp, not limit except the string length
Edit 1 byte saved thx @user81655

x=>{for(a='';a!=x;console.log(a=(' '+a).replace(/.9*$/,x=>++x[0]+'0'.repeat(x.length-1)).trim()));}  Test f=x=>{for(a='';a!=x;console.log(a=(' '+a).replace(/.9*$/,x=>++x[0]+'0'.repeat(x.length-1)).trim()));}

console.log=x=>o.push(x)

function test() {
var v = I.value
o=[]
if (/^\d+$/.test(v)) f(v) else console.log('Invalid number' + v) if(o.length > 30) o.splice(15,o.length-30,'','...',''); O.textContent=o.join\n } test() <input id=I value=10000><button onclick='test()'>go</button> (this test snippet will show just the first and last 15 lines of the output) <pre id=O></pre> • Interesting approach. I think you can use a='' at the start because the added space works as a 0 anyway. Apr 26, 2016 at 14:46 • I think you can omit the last semicolon. Jun 18, 2016 at 9:25 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ you can't, a for statement needs a body Jun 18, 2016 at 9:54 • @edc65 Like when the last statement does not need the semicolon... Jun 18, 2016 at 9:55 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ ??? (anyway, try it, it won't work) Jun 18, 2016 at 10:01 ## AWK, 25 23 Bytes {for(;j<=$1;)print++j}


It seems like I should be able to make this smaller, but I can't seem to figure out how. :(

OK, I must have been asleep not to realize I could drop those braces... thanks Olivier.

An example usage would be to store this code in a file, FILE then do something along the lines of:

awk -f FILE <<< 4242342

• Not initialising with j=0 is ugly but ok here. :-) Apr 25, 2016 at 15:02
• should be "<$1" (you go from 0 to$1-1, as you ++ before you print). You can also drop the {} after while, as you only have 1 command : {for(;j<$1;)print++j} . But : $1 is not valid in awk.. you're mixing shell's variable and awk variables. If you want $1, you also need to add the awk "..." wrapping ... Apr 25, 2016 at 15:40 • @OlivierDulac I can't use <$1 since j starts at 0, the first ++ gets it up to 1, so I need the <=. I didn't think I would need the awk "..."  part since this could simply be placed in a file and called however you like (essentially like compiling a file in other languages). E.g. awk -f FILE <<< 21657 Apr 25, 2016 at 17:27
• @rexkogitans I agree that not initializing j=0 is ugly and I would never do it in production code... but this is PPCG :) Apr 25, 2016 at 17:28
• @RobertBenson: If you edit (gain 2 bytes by taking out the curly braces around the printf, for example, and include a note showing the way to invoke it (like the one in your last comment), you'll gain 2 bytes + I would be able to revert my downvote to a vote. (right now it's locked "until it is edited") Apr 26, 2016 at 6:55

## C++14, 142 bytes

#include<numeric>
#include<list>
using namespace std;[](auto n)->list<decltype(n)>{list<decltype(n)>t(n);iota(t.begin(),t.end(),1);return t;};


This declares an anonymous lambda function which can be captured and subsequently called. The function returns a std::list containing values of the same type as n. The unsigned long long data type can be used on 64-bit machines to support the full 64-bit unsigned integer range (calling it like f(18446744073709551615ULL)).

Try it online

• I think this is only valid in C++14, not C++11 Apr 27, 2016 at 14:51
• @anatolyg std::iota, std::decltype, long long and lambda functions were all added in C++11.
– user45941
Apr 27, 2016 at 18:34
• I mean, a lambda function with auto-typed argument. Isn't it new in C++14? Apr 27, 2016 at 19:05
• @anatolyg Oh, you're right, I forgot that generic lambdas weren't added until C++14. Fixing it now, thanks!
– user45941
Apr 27, 2016 at 19:06
• Do you need newline after std;? Apr 29, 2016 at 17:43

# Dyalog APL, 1 byte

⍳


This has worked since the very first APL, back in the seventies:

⍳⎕


No built-in (also always worked):

+\⎕⍴1


Cumulative sum +\ of input ⎕ number of ⍴ 1s.

Obviously you will need enough memory to contain the result. Here is a print loop to avoid memory full:

{⎕←1+⍵}⍣⎕⊢0


{return and print ⎕← 1+ argument ⍵ } applied ⍣ input ⎕ times to ⊢ 0.

# PowerShell, 32 26 bytes

After reading the question thoroughly, I realized that my original answer does not support up to 2^64-1 since the range operator (..) in PS only supports 32bit integers.

for($x=0;$x-le$args[0];$x++){$x} while($x++-ne$args[0]){$x}


Usage (save as count.ps1):

PS>count.ps1 8
1
2
3
4
5
6
7
8


Old method, supports only up to 2^32-1 taking input from the pipeline:

%{1..$_}  Since expressions are only allowed as the first element in the PowerShell pipeline, I have to wrap it in a foreach (%). # Turing Machine Simulator - 1366 Bytes (124 Lines) 0 * * r 0 0 _ , r 1 1 _ 1 r , , _ , l 2 2 * * l 2 2 , * l 3 3 _ * r 4 3 1 0 l 4 3 2 1 l 4 3 3 2 l 4 3 4 3 l 4 3 5 4 l 4 3 6 5 l 4 3 7 6 l 4 3 8 7 l 4 3 9 8 l 4 3 0 9 l 3 4 * * l 4 4 _ * r 5 5 0 _ r 5 5 , * * n 5 * * r 6 6 * * r 6 6 , * r 9 9 * * r 9 9 _ * l z z * * l m m * * l m m , * r 7 7 * * r 7 7 , * * x 7 0 p r p 7 1 q r q 7 2 w r w 7 3 e r e 7 4 r r r 7 5 t r t 7 6 y r y 7 7 u r u 7 8 i r i 7 9 o r o 8 * * l 8 8 , * l m p * * r p p . * r P p _ . r P P * * r P P _ 0 l 8 q * * r q q . * r Q q _ . r Q Q * * r Q Q _ 1 l 8 w * * r w w . * r W w _ . r W W * * r W W _ 2 l 8 e * * r e e . * r E e _ . r E E * * r E E _ 3 l 8 r * * r r r . * r R r _ . r R R * * r R R _ 4 l 8 t * * r t t . * r T t _ . r T T * * r T T _ 5 l 8 y * * r y y . * r Y y _ . r Y Y * * r Y Y _ 6 l 8 u * * r u u . * r U u _ . r U U * * r U U _ 7 l 8 i * * r i i . * r I i _ . r I I * * r I I _ 8 l 8 o * * r o o . * r O o _ . r O O * * r O O _ 9 l 8 x * * r x x _ , l b b . 1 l c b 0 1 l c b 1 2 l c b 2 3 l c b 3 4 l c b 4 5 l c b 5 6 l c b 6 7 l c b 7 8 l c b 8 9 l c b 9 0 l b c * * l c c _ * r v v * * r v v , * * 2 n * * r n n _ * r halt n , _ r n n . _ r n n p 0 r n n q 1 r n n w 2 r n n e 3 r n n r 4 r n n t 5 r n n y 6 r n n u 7 r n n i 8 r n n o 9 r n  You can try it out here - link Just set the initial input to the upper limit. Supports arbitrarily large integers • Where is the spec for this language? Jul 19, 2016 at 17:05 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ It should be a the bottom of the page in the link Jul 19, 2016 at 17:06 • Not the syntax, the states, what they mean, etc. Jul 19, 2016 at 17:08 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Is this what you're looking for? en.wikipedia.org/wiki/Turing_machine#Informal_description Jul 19, 2016 at 17:16 • No, i wouldn't have asked if it was there. I don't mean the syntax, or how the states work, I mean what the states are. Jul 19, 2016 at 18:09 # JAISBaL, 15 bytes c1I0¯K0DQc1+I0´  Verbose: # \# enable verbose parsing #\ pushnum 1 \# push 1 onto the stack #\ store 0 \# store the top value of the stack into var0 #\ for \# start for loop #\ load 0 \# push the value in var0 onto the stack #\ duplicate \# duplicate the top value of the stack #\ popoutln \# pop the top value off a stack and print it with a new line #\ pushnum 1 \# push 1 onto the stack #\ add \# add the top two values of the stack #\ store 0 \# store the top value of the stack into var0 #\ end \# end current language construct #\  JAISBaL Noncompeting Answers These answers are noncompeting because they use the "range" instruction, which I added because of this challenge. I did not add them specifically to complete this challenge, rather this challenge brought the need for a range instruction to my attention. Manual output, 4 bytes: (Manual output because the output at the end of a JAISBaL program can be considered debug information, although it is always enabled) c1ØP  Verbose: # \# enable verbose parsing #\ pushnum 1 \# push 1 onto the stack #\ rangein \# push an array containing all numbers in the range of the two numbers on the top of the stack, inclusivley #\ popout \# pop the top value of a stack and print it #\  Implicit Output, 3 bytes: c1Ø  Same concept as the manual output answer, just without the print instruction. Instruction codes and testing compatible with JAISBaL-0.0.6 • +1 for interest. Remember to +1 on challenges that you're interested in, and fav the most challenging ones. Jul 25, 2016 at 5:17 # Python 3, 31 bytes print(*range(1,int(input())+1))  Outputs the range of numbers space-separated. Uses * operator magic to explode the output of range into separate arguments to print, which separates its arguments with spaces by default. # ><>, 10 bytes lnao:l(?;:  Try it online! Input should be on stack before execution. Can be done via command line by launching the program with the -v flag, I've yet to understand how/if this should be counted. ### Explanation At each iteration we print the length of the stack followed by a newline (lnao), then if the length of the stack has reached the input number we end the program (:l(?;), otherwise we duplicate the top of the stack (which will bethe input number) and continue. # T-SQL, 61 bytes DECLARE @ INT=0L:SET @+=1PRINT @ IF @<(SELECT i FROM t)GOTO L  I used a different technique than Pete Arden's SQL answer. I took my input from column i of pre-existing table t, per our IO standards. Formatted: DECLARE @ INT=0 L: SET @+=1 PRINT @ IF @<(SELECT i FROM t) GOTO L  • Wow...this is so terse for SQL. Jun 30, 2017 at 16:42 # PHP, 26 bytes -20% = 20.8 <?=join(_,range(1,$argn));


run as pipe with -F.

or

while($i<$argn)echo++$i,_;  prints one trailing delimiter; run as pipe with -nR. # k/kona, 3 Bytes 1+!  e.g. k)1+!15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  • Please seprate this answer. We don't want polyglots in this challenge. Jun 7, 2016 at 16:40 • @Erik Kona is an open-source implementation of the proprietary k language - they're equivalent rather than separate Jun 8, 2016 at 9:20 • Doesn't "Kona" have less abilities than "K" then? Jun 8, 2016 at 9:47 • Presently, yeah, but it's aiming to be a full implementation of k. At which point it's the same thing as with Cython, Jython or IronPython - it's all a matter of semantics. Jun 8, 2016 at 19:27 • I prefer to be notified (and no, @Erik is not a notification, use @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ instead), not the last commenter. @SimonMajor Jun 9, 2016 at 6:41 # Excel VBA, 48 47 46 22 21 Bytes Immediates Window Function Anonymous VBE Immediates Windows Function that takes input of the expected type Variant\Integer and from cell [A1] outputs to the VBE immediates window For I=1To[A1]:?I:Next  Old Subroutine Version Code: Sub F(N):For I=1To N:Cells(I, 1)=I:Next:End Sub  Usage: Sub Test(): F 100: End Sub  Changes: -24 Bytes for converting to Immediates Window Function -1 Byte for condensing I=1 To to I=1To -1 Byte thanks to Engineer Toast for Changing Cells(I,1) to Debug.? -1 Byte for removing whitespace ## Output to ActiveSheet, 30 28 Bytes Anonymous VBE immediate window function that takes input from range [A1] and outputs to the range [1:1] [A1].Resize([A1],1)="=Row()"  -2 Bytes for removing A1 from Row(A1) • You can save 5 bytes by switching from Cells(I, 1)=I to Debug.?I Mar 29, 2017 at 19:10 • Also, this only works up to a certain point before VBA cuts off the significant digits. After that, iterating by 1 won't work because it'll truncate it and return the same value. Mar 29, 2017 at 19:32 • @EngineerToast I have corrected this, thanks for pointing it out :) Mar 30, 2017 at 2:57 # Stack Cats with -nl, 18 bytes *(>]>{<:_-!:]}[*)>  Try it online! Flags are not counted as bytes, but as a separate language as per this Meta consensus. Flags: • -n to do integer I/O • -l to implicitly mirror to the left; the actual code is <(*]{[:!-_:>}<[<)*(>]>{<:_-!:]}[*)>. Adapted from this answer for printing out 1 to 10. I had to fall back to the <(...)*(...)> structure to handle the input correctly. (I once tried _(...)_(...)_ instead, but I realized it is incorrect for n=1.) How it works:  [-1 n*] < [*] [-1 n] (..) Skip * [1*] [-1 n] ( >]> [1] [-1] [n] [*] { Remember 0 <: [1] [-1] [n 0*] [...] _-! [1] [-1] [n n-1*] [...] :] [1] [-1] [n-1] [... n*] } Exit if top is 0 [ Remove 0 at the top * Make the top positive ) Top = 1; exit > Return to the stack of numbers  • -nl is not "+4", and this isn't Stack Cats, but Stack Cats +O: -nl (you aren't forced to this specific header). Mar 13, 2018 at 10:00 • @EriktheOutgolfer I'm just following the answers by Martin Ender and Sp3000, and I haven't seen any other submission using that kind of header. Mar 13, 2018 at 23:12 • True, it's a new consensus, ~1 month old or something IIRC. You're not required to put that all in the header though, if it feels uncomfortable. Mar 13, 2018 at 23:20 # Integral, 8 Bytes  ⌡◙▲⌡•▼e  Try it! ## Explanation  (space) Push 1 ⌡◙▲⌡ "◙▲", standing for "duplicate, increment" when evaluated • Swap third-to-top to the top ▼ Decrement e Repeat that many times  • Similar 8 bytes: ▼ ⌡◙▲⌡•e Aug 6, 2020 at 11:32 # Vyxal, 0.8 bytes ɾ  Try it Online! 1-byte builtin. Python's default number type is arbitrary-size integers, so so is Vyxal's. I mean, it won't finish in reasonable time, but if you gave it enough memory and time... ## QBIC, 6 bytes :[a|?b  Start a FOR-loop ranging from 1 (default start of FOR-loops) to a, where a is read by : from the command line parameters. At every iteration, b (our FOR-loop counter) is printed (?). # Jelly, 1 byte R  Try it online! ## AutoHotKey, 58 bytes Golfed and ungolfed are the same. ### Golfed: ### Ungolfed: c:=0 x:=0 InputBox, x while c<x{ c:=c+1 tooltip %c% }  • Are you sure you need:=0 and not just =0 Apr 26, 2016 at 6:49 • @downrep_nation Yes Apr 27, 2016 at 13:36 # J, 4 bytes >:i.  Try it online! • (sorry fo the late comment) but don't you need some form of x:? Jun 6, 2016 at 19:09 # Bash, 17 bytes eval echo {1..$1}

• I'm not sure you can count up to 2^64 (or even quite a bit less) ? Apr 25, 2016 at 15:50
• Isn't this limited by the maximum length of command line which is unlikely to gain 2^64 times average length of numbers? Apr 26, 2016 at 20:58
• @rexkogitans The ARG_MAX constant is a kernel limit for the exec() system call, but echo is a Bash built-in, so no exec() is involved, and thus you can build the argument list to fill up available memory if you like. Unfortunately, this solution does precisely that. But given enough time and memory, it works up to the maximum number allowed in arithmetic expressions, which seems to be only 2^32-1 on OSX Yosemite (Bash 3.2) but should work up to the specified limit with newer versions. Apr 27, 2016 at 4:53
• @tripleee, yes it was exactly ARG_MAX I was thinking of, but this solution only expands the string twice. So, no reason for ARG_MAX. However, it loads the entire output into memory first (could become a problem with higher values). Apr 27, 2016 at 6:38
• eval "echo -e \"\n\"{1..$1}" to separate numbers with a newline. – Yeti May 21, 2016 at 22:12 # J - 27 bytes 9!:37[0 _ _ _ (,~<:@{.)^:<:  ## Usage  (,~<:@{.)^:<: 20x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  ## Explanation This avoids using the built-in i. to generate a range. First, J will truncate output if it's too long, so we disable that by setting the values to infinity _ 9!:37[0 _ _ _  Second, the input has to be given as an extended integer, which can be done by marking it with a suffix of x. The actual function is only 13 bytes, the change in settings another 13 bytes, and the newline between them is a byte, so 13 + 1 + 13 = 27. (,~<:@{.)^:<: <: - Decrement the input ^: - Repeat the given verb that many times, nesting its calls {. - Take the head of the input <: - Decrement it ,~ - Prepend it to the input  # Befunge, 21 bytes &>: v @^-1:_>,#a:#._  Counts down on the stack, then counts up. Compliance to the 2^64 rule may depend on implementation. • Rule is 2^64-1 because I was forced to deny standard loopholes. Apr 26, 2016 at 8:20 # PHP, 32 bytes for(;$i!=$argv[1];)echo++$i." ";


57 Bytes

$c=function($e){$i=0;while($i!=$e)echo ++$i." ";};$c(10);  • i just wish some would make a CG version of php just change reserved words into letters E.G function = func,while = w for = f foreach = fe Apr 25, 2016 at 14:30 • I want it starting at 1. Apr 25, 2016 at 16:06 • You can trim your solution down to 32 bytes: for(;$i!=$argv[1];)echo++$i." ";. Apr 25, 2016 at 21:55
• @insertusernamehere If your language does not have any way to support huge integers up to 2^64, the upper limit of that particular language must be supported instead Apr 26, 2016 at 1:05
• If you increment inside the for, then you can spare 1 character by replacing string concatenation with variable embedding. I think another character can be spared by replacing != with < (though not sure, not read the entire requirement). for(;$i++<$argv[1];)echo"\$i "; Apr 26, 2016 at 7:22

# Julia, 11 bytes

n->[1:n...]


This is an anonymous function that accepts an integer and returns an array from 1 to the input. This can handle large inputs just fine, it just requires passing n as a larger type, e.g. Int128 or BigInt.

• You may be able to implement support for large integers without providing a different type (although there is no such thing in the rules, it's still a creative idea). Apr 29, 2016 at 17:34

# Go, 90 bytes

Naive solution:

package main
import ."fmt"
func main(){var n,b uint64
Scan(&n)
for;b<n;b++{Println(b+1)}}

• Can this count up to 2^64? Apr 25, 2016 at 14:50
• @Distjubo Only to 2^32-1 if I'm not horribly mistaken. Ought to be unsigned. Apr 25, 2016 at 20:27
• @Distjubo It's okay though (2^64-1, I've changed the limit 'cause standard loopholes) Apr 26, 2016 at 8:12
• @Distjubo changed it to uint so I can get to 2^64-1 Apr 26, 2016 at 8:16
• Btw a signed 64-bit int has a max value of 2^63-1 and not 2^32-1 Apr 26, 2016 at 12:18

# Perl 6, 5 bytes

1..+*


Sadly, 1..* is an infinite range, so I've got to cheat to make Perl 6 understand it's actually an anonymous function (and * is the placeholder).