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

0
\$\begingroup\$

Tcl, 26 bytes

time {puts [incr i]} $argv

Try it online!

First approach can't get more than 2^32-1 as it says integer value too large to represent as non-long integer if n is bigger.

The limit can be even less, as I don't see output, but I am using an online compiler so I can not guarantee what happens on a local one.

For 2123456789 there is output.


Tcl, 32 bytes - 20% = 25.6 bytes

while {[incr i]<$argv} {puts $i}

Try it online!

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

Fortran (GFortran), 45 - 20% = 36 bytes

INTEGER(16)I
READ*,R
DO1 I=1,R
1 PRINT*,I
END

Try it online!

The entire first line can be suppressed if the bonus is optional. In that case, the program will have only 32 bytes, i.e., the bonus is not worth of it! The default integer type in Fortran (which is implicitly assigned to variables whose names starts with I-N) uses only 4 bytes, then this lengthy declaration is necessary to handle such large numbers...

Fortran (GFortran), 32 bytes

READ*,R
DO1 I=1,R
1 PRINT*,I
END

Try it online!

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

Canvas, 2 1 bytes

Try it online!

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

F#, 36 bytes

Try it online!

let x n=for i=1 to n do printfn"%i"i

Pretty straight-forward. This version does not count towards the bonus, because in F# you can only use int32 values for the for...to...do loops.

For the bonus you can use this for 45 bytes:

let r n=Seq.iter(fun x->printfn"%u"x)[1UL..n]

Creates an array of n numbers, and prints each one out. The only thing is that it takes a lot of time and RAM. It's using about 20GB on my PC already, and it hasn't even printed a number yet. But hey, the code is small, right?

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

Gol><>, 6 - 1.2 (20%) = 4.8 bytes

IFPD|;

Try it online!

The output looks like this:

[1]
[2]
[3]
...
[n]

How it works

IFPD|;

I       Take input as int
 F  |   Pop n and repeat n times...
  PD    Increment top and print stack content as `[x]`
        Uses implicit zero on first iteration
     ;  Halt
\$\endgroup\$
0
\$\begingroup\$

Pepe, 30 25 bytes

That's an amazing score for Pepe!

And now, I've saved 5 bytes because of the "flags" update. Now, instead of duplicating the item, I can set a flag to not remove it.

Also, Pepe seems to beat few practical languages now.

REeEREErEEEEErreEEreeERee

Try it online

This challenge made me rethink the control flows in Pepe. It helped me realize that I've created them completely wrong, so I remade them. ¯\_(ツ)_/¯ Thanks!

Warning: If you don't give any input or it will be smaller than 0, your browser will die.

Don't know if the bonus applies, if int's in JS support these numbers, then yes, otherwise no. My PC is not strong enough to print this large number in HTML.

Ungolfed:

REeE
REE
  rEEEEE
  r reEE
  reeE
Ree

Explanation:

  • REeE - Input number to stack R
  • REE - Define label input
    • rEEEEE - Increment item in stack r
    • rEEEeE - Duplicate item in stack r
    • r reEE - Pop Get value from stack r and print popped item as number - Flag r prevents removal of the item
    • reeE - Output newline
  • Ree - Goto label input if selected items in both stacks are equal

Basically, loop until the certain value is reached.

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

Brain-Flak, 14 bytes

{(({})[()])}{}

Try it online!

simply pushes the top stack value -1 until it reaches 0. Then it pops the zero and implicitly prints the stack

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

Wren, 15 bytes - 3 = 12 bytes

This is the exact same result before an interation of the thing.

Fn.new{|n|1..n}

Try it online!

Wren, 24 bytes - 4.8B = 19.2 bytes

Wren supports large integers by default, so... can I have the bonus?

(Technically a returned operand of a function is a kind of output. Also the challenge didn't explicitly say that it has to be in Standard Output.)

Fn.new{|n|(1..n).toList}

Try it online!

Wren, 38 bytes

If this has to be in Standard Output I could do this...

Fn.new{|n|System.print((1..n).toList)}

Try it online!

\$\endgroup\$
-1
\$\begingroup\$

Maple, 9 bytes

seq(1..n)

Usage:

> seq(1..10);
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
\$\endgroup\$
-1
\$\begingroup\$

Ruby 17Byte

1.upto(n){|i|p i}
\$\endgroup\$

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.