31
\$\begingroup\$

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.

\$\endgroup\$
  • 7
    \$\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

2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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]
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I read this as "curry golf" :P \$\endgroup\$ – Downgoat Apr 27 '16 at 21:57
2
\$\begingroup\$

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>

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Interesting approach. I think you can use a='' at the start because the added space works as a 0 anyway. \$\endgroup\$ – user81655 Apr 26 '16 at 14:46
  • \$\begingroup\$ I think you can omit the last semicolon. \$\endgroup\$ – Erik the Outgolfer Jun 18 '16 at 9:25
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ you can't, a for statement needs a body \$\endgroup\$ – edc65 Jun 18 '16 at 9:54
  • \$\begingroup\$ @edc65 Like when the last statement does not need the semicolon... \$\endgroup\$ – Erik the Outgolfer Jun 18 '16 at 9:55
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ ??? (anyway, try it, it won't work) \$\endgroup\$ – edc65 Jun 18 '16 at 10:01
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Not initialising with j=0 is ugly but ok here. :-) \$\endgroup\$ – rexkogitans Apr 25 '16 at 15:02
  • \$\begingroup\$ 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 ... \$\endgroup\$ – Olivier Dulac Apr 25 '16 at 15:40
  • \$\begingroup\$ @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 \$\endgroup\$ – Robert Benson Apr 25 '16 at 17:27
  • \$\begingroup\$ @rexkogitans I agree that not initializing j=0 is ugly and I would never do it in production code... but this is PPCG :) \$\endgroup\$ – Robert Benson Apr 25 '16 at 17:28
  • 2
    \$\begingroup\$ @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") \$\endgroup\$ – Olivier Dulac Apr 26 '16 at 6:55
2
\$\begingroup\$

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

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

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.

Corrected answer:

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 (%).

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Where is the spec for this language? \$\endgroup\$ – Erik the Outgolfer Jul 19 '16 at 17:05
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ It should be a the bottom of the page in the link \$\endgroup\$ – KoreanwGlasses Jul 19 '16 at 17:06
  • \$\begingroup\$ Not the syntax, the states, what they mean, etc. \$\endgroup\$ – Erik the Outgolfer Jul 19 '16 at 17:08
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Is this what you're looking for? en.wikipedia.org/wiki/Turing_machine#Informal_description \$\endgroup\$ – KoreanwGlasses Jul 19 '16 at 17:16
  • \$\begingroup\$ 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. \$\endgroup\$ – Erik the Outgolfer Jul 19 '16 at 18:09
2
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ +1 for interest. Remember to +1 on challenges that you're interested in, and fav the most challenging ones. \$\endgroup\$ – Erik the Outgolfer Jul 25 '16 at 5:17
2
\$\begingroup\$

Sesos (non-competing), 3 bytes

Hexdump of generated binary file:

0000000: 16f8ce                                            ...

Size   : 3 byte(s)

Try it online!

Assembler:

set numin
set numout
get
jmp
sub 1
fwd 1
add 1
put
rwd 1
           ;implicit jnz

Brainfuck pseudocode: ,[->+.<]

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Wow...this is so terse for SQL. \$\endgroup\$ – Erik the Outgolfer Jun 30 '17 at 16:42
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Please seprate this answer. We don't want polyglots in this challenge. \$\endgroup\$ – Erik the Outgolfer Jun 7 '16 at 16:40
  • \$\begingroup\$ @Erik Kona is an open-source implementation of the proprietary k language - they're equivalent rather than separate \$\endgroup\$ – Simon Major Jun 8 '16 at 9:20
  • \$\begingroup\$ Doesn't "Kona" have less abilities than "K" then? \$\endgroup\$ – Erik the Outgolfer Jun 8 '16 at 9:47
  • \$\begingroup\$ 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. \$\endgroup\$ – Simon Major Jun 8 '16 at 19:27
  • \$\begingroup\$ I prefer to be notified (and no, @Erik is not a notification, use @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ instead), not the last commenter. @SimonMajor \$\endgroup\$ – Erik the Outgolfer Jun 9 '16 at 6:41
2
\$\begingroup\$

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)

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 5 bytes by switching from Cells(I, 1)=I to Debug.?I \$\endgroup\$ – Engineer Toast Mar 29 '17 at 19:10
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Engineer Toast Mar 29 '17 at 19:32
  • \$\begingroup\$ @EngineerToast I have corrected this, thanks for pointing it out :) \$\endgroup\$ – Taylor Scott Mar 30 '17 at 2:57
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ -nl is not "+4", and this isn't Stack Cats, but Stack Cats +O: -nl (you aren't forced to this specific header). \$\endgroup\$ – Erik the Outgolfer Mar 13 '18 at 10:00
  • \$\begingroup\$ @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. \$\endgroup\$ – Bubbler Mar 13 '18 at 23:12
  • \$\begingroup\$ 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. \$\endgroup\$ – Erik the Outgolfer Mar 13 '18 at 23:20
1
\$\begingroup\$

Jelly, 1 byte

R

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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%
}
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Are you sure you need:=0 and not just =0 \$\endgroup\$ – downrep_nation Apr 26 '16 at 6:49
  • \$\begingroup\$ @downrep_nation Yes \$\endgroup\$ – Michelfrancis Bustillos Apr 27 '16 at 13:36
1
\$\begingroup\$

J, 4 bytes

>:i.

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ (sorry fo the late comment) but don't you need some form of x:? \$\endgroup\$ – Conor O'Brien Jun 6 '16 at 19:09
1
\$\begingroup\$

Bash, 17 bytes

eval echo {1..$1}
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ I'm not sure you can count up to 2^64 (or even quite a bit less) ? \$\endgroup\$ – Olivier Dulac Apr 25 '16 at 15:50
  • 1
    \$\begingroup\$ Isn't this limited by the maximum length of command line which is unlikely to gain 2^64 times average length of numbers? \$\endgroup\$ – rexkogitans Apr 26 '16 at 20:58
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – tripleee Apr 27 '16 at 4:53
  • \$\begingroup\$ @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). \$\endgroup\$ – rexkogitans Apr 27 '16 at 6:38
  • \$\begingroup\$ eval "echo -e \"\n\"{1..$1}" to separate numbers with a newline. \$\endgroup\$ – Yeti May 21 '16 at 22:12
1
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Befunge, 21 bytes

&>:  v
@^-1:_>,#a:#._

Counts down on the stack, then counts up. Compliance to the 2^64 rule may depend on implementation.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Rule is 2^64-1 because I was forced to deny standard loopholes. \$\endgroup\$ – Erik the Outgolfer Apr 26 '16 at 8:20
1
\$\begingroup\$

PHP, 32 bytes

for(;$i!=$argv[1];)echo++$i." "; 

57 Bytes

$c=function($e){$i=0;while($i!=$e)echo ++$i." ";};$c(10);

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Martin Barker Apr 25 '16 at 14:30
  • \$\begingroup\$ I want it starting at 1. \$\endgroup\$ – Erik the Outgolfer Apr 25 '16 at 16:06
  • 1
    \$\begingroup\$ You can trim your solution down to 32 bytes: for(;$i!=$argv[1];)echo++$i." ";. \$\endgroup\$ – insertusernamehere Apr 25 '16 at 21:55
  • \$\begingroup\$ @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 \$\endgroup\$ – Martin Barker Apr 26 '16 at 1:05
  • 1
    \$\begingroup\$ 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 "; \$\endgroup\$ – manatwork Apr 26 '16 at 7:22
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Erik the Outgolfer Apr 29 '16 at 17:34
1
\$\begingroup\$

Go, 90 bytes

Naive solution:

package main
import ."fmt"
func main(){var n,b uint64
Scan(&n)
for;b<n;b++{Println(b+1)}}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Can this count up to 2^64? \$\endgroup\$ – Distjubo Apr 25 '16 at 14:50
  • \$\begingroup\$ @Distjubo Only to 2^32-1 if I'm not horribly mistaken. Ought to be unsigned. \$\endgroup\$ – seequ Apr 25 '16 at 20:27
  • \$\begingroup\$ @Distjubo It's okay though (2^64-1, I've changed the limit 'cause standard loopholes) \$\endgroup\$ – Erik the Outgolfer Apr 26 '16 at 8:12
  • \$\begingroup\$ @Distjubo changed it to uint so I can get to 2^64-1 \$\endgroup\$ – Kristoffer Sall-Storgaard Apr 26 '16 at 8:16
  • \$\begingroup\$ Btw a signed 64-bit int has a max value of 2^63-1 and not 2^32-1 \$\endgroup\$ – Distjubo Apr 26 '16 at 12:18
1
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 15 bytes + 8

Works with the -l and -Mbigint switch, and should work up to 2^64 with BigInt. Thanks to @andlrc for the 7 bytes shaved off.

print for 1..<>
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You should use BigInt then. \$\endgroup\$ – Erik the Outgolfer Apr 25 '16 at 13:45
  • \$\begingroup\$ But you can do arbitary precision, thus you can count to pretty much infinite (2^65536 at least) \$\endgroup\$ – Bálint Apr 25 '16 at 14:52
  • 1
    \$\begingroup\$ You can use print for 1..<> \$\endgroup\$ – andlrc Apr 25 '16 at 15:33
  • \$\begingroup\$ @andlrc Thanks ! \$\endgroup\$ – Paul Picard Apr 26 '16 at 6:49
  • \$\begingroup\$ @ΈρικΚωνσταντόπουλος Edited to match this. \$\endgroup\$ – Paul Picard Apr 26 '16 at 6:49
1
\$\begingroup\$

AutoHotKey

I wasn't the first to post an answer in AHK, but mine are shorter for now. AutoHotKey doesn't like golf so the golfed versions are the same as the ungolfed versions. As for AutoHotKey's integer support:

For integers, 64-bit signed values are supported, which range from -9223372036854775808 (-0x8000000000000000) to 9223372036854775807 (0x7FFFFFFFFFFFFFFF). Any integer constants outside this range are not supported and might yield inconsistent results. By contrast, arithmetic operations on integers wrap around upon overflow (e.g. 0x7FFFFFFFFFFFFFFF + 1 = -0x8000000000000000).

Really Annoying Version - 25 bytes

Prints each number in a message box (super annoying) and takes input as the first argument on the command line.

i=1
Loop %1%
MsgBox % i++

i=1 - Loop counter set at 1
Loop %1% - Loop as many times as specified on the CLI
MsgBox % i++ = Display a message box with the digit in it, then increment

Alternate, Less Annoying Version - 43 bytes

This version displays only one message box but is much longer.

i=1
j=
Loop %1%
j:=j . i++ . " "
MsgBox %j%

i=1 - Loop counter set at 1
j= - Variable j set to an empty string
Loop %1% - Loop as many times as specified on the CLI
j:=j . i++ . " " - concatenate i and a space to j, then increment i
MsgBox %j% = Display a message box with j in it

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I hope there's a way to escape the loop! Counting just to 30 would be incredibly annoying. \$\endgroup\$ – Stewie Griffin Apr 26 '16 at 6:14
  • \$\begingroup\$ @StewieGriffin I mean, you can kill the process... But otherwise, no. \$\endgroup\$ – Nakaan Apr 26 '16 at 11:46
  • \$\begingroup\$ I think in the maximum signed int64 value you have 2 extra Fs. \$\endgroup\$ – Erik the Outgolfer May 2 '16 at 12:45
  • \$\begingroup\$ Here's a 26 byte less annoying version using Send as the output method (using \n to indicate a new line in the program because I need to use the accent/backtick in the program that messes with SE): Loop,%1%{ \n i++ \n Send,` %i% \n } \$\endgroup\$ – Engineer Toast Mar 28 '17 at 12:45
1
\$\begingroup\$

Jolf, 29 + 1 = 30 bytes

This in fact is valid! It uses the same technique as the JavaScript answer, keeping an array of digits. It should thus theoretically work, but may not work due to time limitations (browser crashing, etc.) Make sure to have "Pretty Output" enabled if it isn't already. (Apparently, making sure is an extra byte, like a flag, so there's that.) Try it here!

Ζ²1W<ni)aRζEΖZWζγwlζh.ζγonhn}

Ungolfed:

Z ~: 1
W < n i )
  a R ζ E
  Ζ ZW
    ζ
    w lζ
    h .ζ w lζ
  on h n
}

That's real clean-looking for a golfing language! :D

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What encoding is this? \$\endgroup\$ – Erik the Outgolfer Apr 26 '16 at 18:16
  • \$\begingroup\$ You should add 1 byte because I must choose pretty output on. \$\endgroup\$ – Erik the Outgolfer Apr 26 '16 at 18:17
  • \$\begingroup\$ @ΈρικΚωνσταντόπουλος You are right on the latter. Also, it's encoded in ISO-8859-7. You can upload a file on the website. (It's the encoding that bears your name in 1-byte characters ;)) \$\endgroup\$ – Conor O'Brien Apr 26 '16 at 18:22
  • \$\begingroup\$ That's right. 2 more to go... \$\endgroup\$ – Erik the Outgolfer Apr 26 '16 at 18:41
  • \$\begingroup\$ I don't think the encoding has to do something with here, as my name uses Unicode I think. \$\endgroup\$ – Erik the Outgolfer Apr 29 '16 at 17:40
1
\$\begingroup\$

C, 61 bytes

long i,n;main(){for(scanf("%ld",&n);i++<n;printf("%ld ",i));}

Essentially the same as my C++ answer, but C (or gcc) is more lenient of missing #includes, a missing return-type for main, and missing return-statements.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I disagree. Not all compilers support missing #includes. So you should add 18 bytes. \$\endgroup\$ – Erik the Outgolfer Apr 27 '16 at 12:35
  • \$\begingroup\$ gcc is considered sort of a golden standard as far as C compilers are concerned. Since default gcc has always supported missing #include <stdio.h>, and corner-cutting is in general encouraged in code-golf, I still think this is legit. @Dennis seems to agree in a comment to this question. \$\endgroup\$ – tucuxi Apr 27 '16 at 13:07
  • \$\begingroup\$ You mean this comment? \$\endgroup\$ – Erik the Outgolfer Apr 27 '16 at 13:18
  • \$\begingroup\$ Yes - and thanks to you, I now know how to link comments :-) \$\endgroup\$ – tucuxi Apr 27 '16 at 16:17
  • \$\begingroup\$ Remember, you always learn, no matter your age. The date of the comment is it's link (click on it to be linked to that particular comment). \$\endgroup\$ – Erik the Outgolfer Apr 27 '16 at 17:29

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.