40
\$\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.

asked Apr 25, 2016 at 12:04
\$\endgroup\$
11
  • 7
    \$\begingroup\$ Related. \$\endgroup\$ Commented Apr 25, 2016 at 12:20
  • \$\begingroup\$ "You can get n any way." Does that mean we can assume n to be saved in a variable? \$\endgroup\$ Commented May 21, 2016 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\$ Commented May 22, 2016 at 10:26
  • \$\begingroup\$ It might be useful to link to the accepted I/O methods \$\endgroup\$ Commented Jun 2, 2017 at 12:26
  • 1
    \$\begingroup\$ @Ephphatha Yes it probably is, this challenge is from the old times where I was an utter newb. \$\endgroup\$ Commented Jun 2, 2017 at 12:29

149 Answers 149

2
\$\begingroup\$

Python 2, (削除) 37 (削除ここまで) (削除) 33 (削除ここまで) (削除) 32 (削除ここまで) 33 bytes

for i in xrange(input()):print-~i

Presumably works up to 2**64 and beyond.

Shot down four bytes thanks to @dieter, and another thanks to @orlp. But apparently, as @Sp3000 found out, range() might have issues with higher values, so the function was changed to xrange(). Note: even xrange() might have issues, at least in 2.7.10.

Erik the Outgolfer
40.8k5 gold badges46 silver badges125 bronze badges
answered Apr 25, 2016 at 12:19
\$\endgroup\$
10
  • 1
    \$\begingroup\$ Python 2, to be exact :) \$\endgroup\$ Commented Apr 25, 2016 at 12:20
  • \$\begingroup\$ 33 bytes -> for i in range(input()):print i+1 \$\endgroup\$ Commented Apr 25, 2016 at 12:24
  • 2
    \$\begingroup\$ 32 bytes -> for i in range(input()):print-~i \$\endgroup\$ Commented Apr 25, 2016 at 12:25
  • 1
    \$\begingroup\$ "Presumably works up to 2**64 and beyond." - doubt it in Python 2, but it might with xrange (edit: even xrange might have issues, at least in 2.7.10) \$\endgroup\$ Commented Apr 25, 2016 at 12:34
  • \$\begingroup\$ How does -~ work? Edit: I figured it out. Also, nice trick! \$\endgroup\$ Commented Apr 27, 2016 at 8:26
2
\$\begingroup\$

Zsh, 12 bytes

echo {1..1ドル}

This works because variables are expanded before the braces.

answered Apr 25, 2016 at 15:36
\$\endgroup\$
3
  • 2
    \$\begingroup\$ I'm not sure you can count up to 2^64 (or even quite a bit less) ? \$\endgroup\$ Commented Apr 25, 2016 at 15:50
  • \$\begingroup\$ @OlivierDulac 2^64-1 is fine now. \$\endgroup\$ Commented Apr 26, 2016 at 8:17
  • 1
    \$\begingroup\$ zsh's maximum is 2^63 - 1 \$\endgroup\$ Commented Apr 26, 2016 at 14:49
2
\$\begingroup\$

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.

answered Apr 25, 2016 at 13:41
\$\endgroup\$
2
  • \$\begingroup\$ It can take as long as it wants. Glad to see an answer to work with 2^64, especially with an unfinished language. +1 \$\endgroup\$ Commented Apr 25, 2016 at 13:42
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 26, 2016 at 8:10
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.

answered Apr 25, 2016 at 15:39
\$\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]
answered Apr 26, 2016 at 0:06
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I read this as "curry golf" :P \$\endgroup\$ Commented Apr 27, 2016 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>

answered Apr 26, 2016 at 9:03
\$\endgroup\$
5
  • \$\begingroup\$ Interesting approach. I think you can use a='' at the start because the added space works as a 0 anyway. \$\endgroup\$ Commented Apr 26, 2016 at 14:46
  • \$\begingroup\$ I think you can omit the last semicolon. \$\endgroup\$ Commented Jun 18, 2016 at 9:25
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ you can't, a for statement needs a body \$\endgroup\$ Commented Jun 18, 2016 at 9:54
  • \$\begingroup\$ @edc65 Like when the last statement does not need the semicolon... \$\endgroup\$ Commented Jun 18, 2016 at 9:55
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ ??? (anyway, try it, it won't work) \$\endgroup\$ Commented Jun 18, 2016 at 10:01
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

answered Apr 26, 2016 at 9:31
\$\endgroup\$
7
  • \$\begingroup\$ I think this is only valid in C++14, not C++11 \$\endgroup\$ Commented Apr 27, 2016 at 14:51
  • \$\begingroup\$ @anatolyg std::iota, std::decltype, long long and lambda functions were all added in C++11. \$\endgroup\$ Commented Apr 27, 2016 at 18:34
  • \$\begingroup\$ I mean, a lambda function with auto-typed argument. Isn't it new in C++14? \$\endgroup\$ Commented Apr 27, 2016 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\$ Commented Apr 27, 2016 at 19:06
  • \$\begingroup\$ Do you need newline after std;? \$\endgroup\$ Commented Apr 29, 2016 at 17:43
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 (%).

answered Jul 19, 2016 at 16:38
\$\endgroup\$
0
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

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

answered Jul 24, 2016 at 23:04
\$\endgroup\$
1
  • \$\begingroup\$ +1 for interest. Remember to +1 on challenges that you're interested in, and fav the most challenging ones. \$\endgroup\$ Commented Jul 25, 2016 at 5:17
2
\$\begingroup\$

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.

answered Jul 27, 2016 at 18:41
\$\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.

answered Mar 26, 2017 at 17:19
\$\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
answered Jun 30, 2017 at 16:40
\$\endgroup\$
1
  • \$\begingroup\$ Wow...this is so terse for SQL. \$\endgroup\$ Commented Jun 30, 2017 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.

answered Nov 2, 2016 at 17:42
\$\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
answered Jun 6, 2016 at 15:38
\$\endgroup\$
5
  • \$\begingroup\$ Please seprate this answer. We don't want polyglots in this challenge. \$\endgroup\$ Commented Jun 7, 2016 at 16:40
  • \$\begingroup\$ @Erik Kona is an open-source implementation of the proprietary k language - they're equivalent rather than separate \$\endgroup\$ Commented Jun 8, 2016 at 9:20
  • \$\begingroup\$ Doesn't "Kona" have less abilities than "K" then? \$\endgroup\$ Commented Jun 8, 2016 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\$ Commented Jun 8, 2016 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\$ Commented Jun 9, 2016 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)

answered Nov 10, 2016 at 12:24
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can save 5 bytes by switching from Cells(I, 1)=I to Debug.?I \$\endgroup\$ Commented Mar 29, 2017 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\$ Commented Mar 29, 2017 at 19:32
  • \$\begingroup\$ @EngineerToast I have corrected this, thanks for pointing it out :) \$\endgroup\$ Commented Mar 30, 2017 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
answered Mar 13, 2018 at 1:51
\$\endgroup\$
3
  • \$\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\$ Commented Mar 13, 2018 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\$ Commented Mar 13, 2018 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\$ Commented Mar 13, 2018 at 23:20
2
\$\begingroup\$

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
answered Aug 6, 2020 at 10:09
\$\endgroup\$
1
  • \$\begingroup\$ Similar 8 bytes: ▼ ⌡◙さんかく⌡•e \$\endgroup\$ Commented Aug 6, 2020 at 11:32
2
\$\begingroup\$

MAWP 1.0, 30 bytes

%@_1A[1A~25WWM~]~[!1A]%[:48W;]

Output takes a little while for larger numbers.

Try it!

answered Aug 8, 2020 at 15:56
\$\endgroup\$
2
\$\begingroup\$

Flurry, 16 - 3.2 = 12.8 bytes

{}{<><<>()>[]}{}

Run example

$ ./flurry -inn -c "{}{<><<>()>[]}{}" 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ ./flurry -inn -c "{}{<><<>()>[]}{}" 1
1

Generates the range using the stack height. Since the task requires 1-based, each stack height should be incremented (produce 2 when stack height is 1, 3 when stack height is 2, etc.).

main = {} f {} -- n f 1
 -- Apply f n times to the starting value of 1
f = {<><<>()>[]} -- \x. push(x); return (succ height)
 -- Implicitly push the argument and ignore it,
 -- returning the value of height + 1
 -- so that it can be pushed at the next iteration

Flurry uses Church numerals as the number representation, so it theoretically supports infinite-precision positive integers. But it will take a very long time to actually print the result.

answered Aug 14, 2020 at 4:24
\$\endgroup\$
2
\$\begingroup\$

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

emanresu A
46.2k5 gold badges111 silver badges257 bronze badges
answered Jul 22, 2016 at 21:25
\$\endgroup\$
0
2
\$\begingroup\$

AWK 18.4 (削除) 25 (削除ここまで) (削除) 23 bytes (削除ここまで)

Score includes 21 bytes of code, 2 bytes for option -M (added to support arbitrary precision) and -4.6 bonus.

{for(;j<1ドル;)print++j}

Try it online!

Note: The TIO link will halt after it fills its buffer, or after it runs for more than 60 seconds.

The following version shows how to count the last 10 integers that preceed the input to verify accuracy.

{for(j=1ドル-10;j<1ドル;)print++j}

Try it online!

answered Apr 25, 2016 at 14:22
\$\endgroup\$
8
  • 2
    \$\begingroup\$ Not initialising with j=0 is ugly but ok here. :-) \$\endgroup\$ Commented Apr 25, 2016 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\$ Commented Apr 25, 2016 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\$ Commented Apr 25, 2016 at 17:27
  • 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\$ Commented Apr 26, 2016 at 6:55
  • 1
    \$\begingroup\$ I don't see why you would need it to be <= to account for 0, as 0 is still less than the input. it seems to work for me: Try it online! \$\endgroup\$ Commented Dec 23, 2024 at 6:42
1
\$\begingroup\$

Jelly, 1 byte

R

Try it online!

answered Apr 25, 2016 at 12:44
\$\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%
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Are you sure you need:=0 and not just =0 \$\endgroup\$ Commented Apr 26, 2016 at 6:49
  • \$\begingroup\$ @downrep_nation Yes \$\endgroup\$ Commented Apr 27, 2016 at 13:36
1
\$\begingroup\$

J, 4 bytes

>:i.

Try it online!

answered Apr 25, 2016 at 15:07
\$\endgroup\$
1
  • 1
    \$\begingroup\$ (sorry fo the late comment) but don't you need some form of x:? \$\endgroup\$ Commented Jun 6, 2016 at 19:09
1
\$\begingroup\$

Bash, 17 bytes

eval echo {1..1ドル}
answered Apr 25, 2016 at 15:30
\$\endgroup\$
6
  • 2
    \$\begingroup\$ I'm not sure you can count up to 2^64 (or even quite a bit less) ? \$\endgroup\$ Commented Apr 25, 2016 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\$ Commented Apr 26, 2016 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\$ Commented Apr 27, 2016 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\$ Commented Apr 27, 2016 at 6:38
  • \$\begingroup\$ eval "echo -e \"\n\"{1..1ドル}" to separate numbers with a newline. \$\endgroup\$ Commented May 21, 2016 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
answered Apr 25, 2016 at 19:54
\$\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.

answered Apr 25, 2016 at 22:31
\$\endgroup\$
1
  • \$\begingroup\$ Rule is 2^64-1 because I was forced to deny standard loopholes. \$\endgroup\$ Commented Apr 26, 2016 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);

(削除ここまで)

answered Apr 25, 2016 at 14:19
\$\endgroup\$
5
  • \$\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\$ Commented Apr 25, 2016 at 14:30
  • \$\begingroup\$ I want it starting at 1. \$\endgroup\$ Commented Apr 25, 2016 at 16:06
  • 1
    \$\begingroup\$ You can trim your solution down to 32 bytes: for(;$i!=$argv[1];)echo++$i." ";. \$\endgroup\$ Commented Apr 25, 2016 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\$ Commented Apr 26, 2016 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\$ Commented Apr 26, 2016 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.

answered Apr 25, 2016 at 14:05
\$\endgroup\$
1
  • \$\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\$ Commented Apr 29, 2016 at 17:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.