38
\$\begingroup\$

This might be a very simple challenge, but I am surprised it hasn't been done on code-golf yet:

Print all Integers from 1 to 10 inclusive in ascending order to standard output.

Your output format can be whatever your language supports. This includes arbitrary separators (commas, semicolons, newlines, combinations of those, etc., but no digits), and prefixes and postfixes (like [...]). However, you may not output any other numbers than 1 through 10. Your program may not take any input. Standard loopholes are disallowed.

This is , so shortest answer in bytes wins!

Leaderboard

var QUESTION_ID=86075,OVERRIDE_USER=42570;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 9
    \$\begingroup\$ Related (duplicate?) \$\endgroup\$ – Luis Mendo Jul 21 '16 at 9:07
  • 15
    \$\begingroup\$ If the only change is hard-coding a single parameter then that falls under the banner of "trivial change", and by the standards of this site still counts as a dupe. \$\endgroup\$ – Peter Taylor Jul 21 '16 at 9:54
  • 9
    \$\begingroup\$ @PeterTaylor The other challenge has a huge problem with the integer limits though. The way it's specified every TC language that doesn't have 64-bit integers needs to implement them. (And that affects quite a lot of languages.) \$\endgroup\$ – Martin Ender Jul 21 '16 at 10:01
  • 16
    \$\begingroup\$ @xnor Quite frankly, I'd rather close the other challenge as a duplicate of this one. The requirement pretty much ruins it. \$\endgroup\$ – Dennis Jul 21 '16 at 14:09
  • 8
    \$\begingroup\$ I can't believe every single of the (currently) 71 answers assumes the base should be decimal… \$\endgroup\$ – Skippy le Grand Gourou Jul 22 '16 at 15:05

203 Answers 203

2
\$\begingroup\$

UGL, 20 bytes

cuuuuu$+$Rl$d:_lorO:

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Hexagony, 37 Bytes

Minified:

........./..\..10$\("\.$...).>{<!@.._

Try It Online!

Layed Out:

   . . . .
  . . . . .
 / . . \ . .
1 0 $ \ ( " \
 . $ . . . )
  . > { < !
   @ . . _
\$\endgroup\$
2
\$\begingroup\$

Latex, 62 Bytes

\documentclass{book}\begin{document}12345678910\end{document}

Thanks @egreg

\$\endgroup\$
2
\$\begingroup\$

R, 9 bytes

cat(1:10)

cat prints, 1:10 creates the numbers 1 to 10.

\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 30 bytes

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

Try it online!

Explanation

For a documentation of what each command does, see the GitHub page.

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

((()()()()()){})                 push 10
                {(({})[()])}     while top != 0: pop top, push top, push top-1
                            {}   pop
\$\endgroup\$
2
\$\begingroup\$

Java, 47 bytes

()->{for(int i=0;i++<10;System.out.print(i));};

Ungolfed test program

public static void main(String[] args) {
    Runnable r = () -> {
        for (int i = 0; i++ < 10; System.out.print(i));
    };

    r.run();
}

36 bytes (non competing)

()->IntStream.range(0,11).toArray();

Doesn't include imports :(

\$\endgroup\$
2
\$\begingroup\$

Python3 : 22 19 bytes

print(*range(1,11))

Output:

1 2 3 4 5 6 7 8 9 10
\$\endgroup\$
  • 1
    \$\begingroup\$ That output is not for that code. range() with a single parameter generates starting from 0. \$\endgroup\$ – manatwork Nov 14 '16 at 10:53
  • \$\begingroup\$ @manatwork Oh, my bad. Updating it :) \$\endgroup\$ – ABcDexter Nov 14 '16 at 11:09
2
\$\begingroup\$

Dip, 2 bytes

Tl

Body must be at least 30 characters; you entered 16.

\$\endgroup\$
2
\$\begingroup\$

Befunge-98, 9 bytes

1+:.:9`kq

Shortened from 11 thanks to a suggestion from ais523.

A step by step description:

1    push 1 to stack.
+    pop top two stack items and add them. push result.
     popping an empty stack returns 0, which is why
     this works the first loop around.
:    dup top.
.    print top of stack as integer.
     Funge-98 standard states that integer printing
     puts a space afterward automatically.
:    dup top.
9    push 9.
`    pop two items, first b then a.
     push 1 if a > b, else 0.
k    execute next char n times.
     n is top of stack.
     if n is 0, the next char is just skipped.
q    quit.

This program loops because Funge-98 lines wrap around.

Old Versions

11 bytes

1+:.:a\`!kq
\$\endgroup\$
  • \$\begingroup\$ The not(10>TOS) (a\`!) calculation is taking up quite a lot of space here, and you might be able to shorten it. Wouldn't it be shorter to translate this simply as TOS>9 (9`)? \$\endgroup\$ – user62131 Nov 16 '16 at 4:39
  • \$\begingroup\$ Right, that is a lot better! Down to 9 chars now. \$\endgroup\$ – snail_ Nov 16 '16 at 19:10
  • \$\begingroup\$ My solution would have been a-:!k@b+:. for 10 bytes \$\endgroup\$ – MildlyMilquetoast Jun 19 '17 at 17:19
2
\$\begingroup\$

Vim, 9 keystrokes

I saw that there was already a vim answer, but it's not the shortest, so I thought I'd post the shortest one for completeness sake.

10O0<esc>v{g<C-a>

This only works in vim 8, or later versions of vim 7.4.

Explanation:

10                  " Ten times:
  O                 " Open a newline
   0<esc>           " And insert a 0
         v{         " Visually select everything
           g<C-a>   " And create an increasing sequence
\$\endgroup\$
  • \$\begingroup\$ huh. Didn't know about the g<C-a> trick. GG. \$\endgroup\$ – Zwei Nov 17 '16 at 2:23
2
\$\begingroup\$

QBIC, 9 bytes

[1|10:?a]

Explanation

[       ]  FOR <x> = ...NEXT; note that <x> becomes the symbol a-z, based on availability
 1|10      <x> goes from 1 to 10, an increment could be specified with ||
     :     End of FOR-loop specification
      ?a   Print the value of a

EDIT: A non-competing entry saves me one byte two bytes:

[q|z:?a

QBIC now supports variable params for FOR, and q through z are initialised in QBIC to represent 1-10, so saved a byte on the 10. Woohoo! Also, QBIC now detects any unclosed language constructs (IF/DO/FOR) at the end of a program and auto-closes 'm.

EDIT2: QBIC now has a variable number of parameters for its FOR loops. It by default assumes a loop from 1 to 10. Providing 1 parameter (y) makes it run from 1 to y, Providing 2 parameters (x and y) loops from x to y. Providing 3 parameters changes the interval: FOR a = x TO y STEP z. Using the defaults gives us this 4-byter:

[|?a
\$\endgroup\$
2
\$\begingroup\$

Dyvil, 20 bytes

for(x<-1..10)print x

21 bytes

for x<-1..10{print x}

Two ways to do the same thing: for-each loop over the range 1..10 with type-inferred loop variable x. The loop body calls the built-in print(int) function with the argument x.


22 bytes

print{for x<-1..10{x}}

This version calls the built-in print(AutoPrinter.()->void) method. An AutoPrinter instance is implicitly available to the {for ...} block, which is passed as an anonymous function. The AutoPrinter class has an applyStatement(any) method, which is called when an expression like x appears in a statement context. The implementation forwards to print(any), which prints the expression.


24 bytes

(1..10).forEach(print _)

Functional approach; creates an IntRange object and calls its forEach(int -> void) (higher order) method. The argument is an anonymous lambda function that passes the argument to the aforementioned print(int) method. In this case, print _ is syntactic sugar for the lambda expression x => print x. Again, the types are inferred by the compiler, so that it internally becomes (int x) => print x.


Output in all cases:

1
2
3
4
5
6
7
8
9
10
\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 9 bytes

seq(I,I,1,10
\$\endgroup\$
2
\$\begingroup\$

x86 assembly (32-bit, Linux), 40 bytes

Prints out the numbers from 1 to 10, separated by newlines. The last number is not followed by a newline.

0: 31 db 43 89 d8 b4 0a 50 89 da 42 89 e1 c6 01 30
1: 00 01 50 8d 04 12 cd 80 58 40 3f 73 ee 66 c7 01
2: 31 30 30 e4 cd 80 48 cd 80

Ungolfed:

00000000 <_start>:
   0:   31 db                   xor    %ebx,%ebx
   2:   43                      inc    %ebx
   3:   89 d8                   mov    %ebx,%eax
   5:   b4 0a                   mov    $0xa,%ah
   7:   50                      push   %eax
   8:   89 da                   mov    %ebx,%edx
   a:   42                      inc    %edx

0000000b <loop>:                                                                                     
   b:   89 e1                   mov    %esp,%ecx                                                     
   d:   c6 01 30                movb   $0x30,(%ecx)                                                  
  10:   00 01                   add    %al,(%ecx)                                                    
  12:   50                      push   %eax                                                          
  13:   8d 04 12                lea    (%edx,%edx,1),%eax                                            
  16:   cd 80                   int    $0x80                                                         
  18:   58                      pop    %eax                                                          
  19:   40                      inc    %eax                                                          
  1a:   3f                      aas                                                                  
  1b:   73 ee                   jae    b <loop>                                                      

0000001d <end>:                                                                                      
  1d:   66 c7 01 31 30          movw   $0x3031,(%ecx)                                                
  22:   30 e4                   xor    %ah,%ah
  24:   cd 80                   int    $0x80
  26:   48                      dec    %eax
  27:   cd 80                   int    $0x80

The most notable part of the optimization is the aas instruction, which checks if %al is 10 or above, and, if it is, sets the carry flag, and decrements %al by 6. It is meant to be used to adjust for subtraction—you might find the corresponding aaa, which adds 6 instead, used in a real program here. I have instead made use of it because subtracting 6 gets me 4, which is exactly the number I need for another write syscall—saving me another lea instruction.

\$\endgroup\$
2
\$\begingroup\$

Commercial, 649 bytes

A now 1 dollar off!
B now 1 dollar off!
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B
A has been selling out worldwide!
A: made by the makers of B

Try it online!

I tried to do this in a loop, but Jumps in Commercial are weird and I couldn't get it to work properly

\$\endgroup\$
2
\$\begingroup\$

Fortran 95, 32 bytes

It was the only language I know that was not used yet :P

program o
print*,(i,i=1,10)
end
\$\endgroup\$
2
\$\begingroup\$

Kotlin, 16 bytes

print((1..9)+10)

Beautified

print((1..9)+10)

Test

fun f() =
print((1..9)+10)

fun main(args: Array<String>) {
    f()
}

TIO

TryItOnline

\$\endgroup\$
2
\$\begingroup\$

brainfuck, 53 bytes

++++++++++[->+++++>+>+<<<]>-->-[-<+.>>.<]<--------.-.

Try it online!

Explanation :

++++++++++              # set initial counter to ten
[->+++++>+>+<<<]        # creates ascii cell, second counter, and newline
>-->-                   # fixes ascii and counter cell values
[-<+.>>.<]              # prints digits one through nine
<--------.-.            # prints the ten

Output:

1
2
3
4
5
6
7
8
9
10
\$\endgroup\$
  • \$\begingroup\$ It's not really different enough from the previous answer for me to upvote, but I appreciate the explanation. Also, this answer is much shorter \$\endgroup\$ – Jo King Feb 21 '18 at 3:49
  • 1
    \$\begingroup\$ And welcome to PPCG! \$\endgroup\$ – Jo King Feb 21 '18 at 3:56
2
\$\begingroup\$

Sinclair ZX80 (4/8K ROM) or Sinclair ZX81/ZX Spectrum (27 tokenized BASIC bytes for the ZX80, 38 tokenized BASIC bytes for the ZX81)

 1 FOR I=1 TO 10
 2 PRINT I
 3 NEXT I

Or (direct mode, probably works on all 8-bit variants of BASIC):

 PRINT 1,2,3,4,5,6,7,8,9,10

Sinclair ZX Spectrum (and other BASIC variants except the ZX80/ZX81):

 1 FOR I=1 TO 10:PRINT I:NEXT I

Simple, no?

\$\endgroup\$
2
\$\begingroup\$

Keg, 12 bytes

1(91+|:. ,1+

Output is separated by spaces

How it works

1   Pushes 1
(   Begins for loop
91+|   For loop will run 10 times
:.     Output the top element of the stack
 ,     Output space
1+     Add one to the top element of the stack
End bracket gets added automaticly

Try it Online!

\$\endgroup\$
  • \$\begingroup\$ I am working on supporting \ in loop conditions as in this (1 byte less) \$\endgroup\$ – A _ Aug 10 at 13:54
  • \$\begingroup\$ Oh yeah, I did try that and was wondering why that wasn't working, but I just compromised with 91+ \$\endgroup\$ – EdgyNerd Aug 10 at 13:57
  • \$\begingroup\$ That isn't working because you can't escape a character in a for loop repetition indicator. If you read the implementation, you will find that the escaping character is simply skipped. (I just created a PR for it. JonoCode9374 will probably merge it.) \$\endgroup\$ – A _ Aug 10 at 14:00
  • \$\begingroup\$ Whoops, there is an issue in my source code that makes it in fact 2 bytes less. (A 1-to-10-printing program written using 10 bytes!) \$\endgroup\$ – A _ Aug 10 at 14:05
  • \$\begingroup\$ Should we create a Keg chatroom to discuss new features and golfing? \$\endgroup\$ – EdgyNerd Aug 10 at 14:31
1
\$\begingroup\$

Bubblegum, 17 bytes

00000000: 026f eafc 0f98 211e 5d50 d0aa 25bc 6f2a  .o....!.]P..%.o*
00000010: d1                                       .

Outputs 1 2 3 4 5 6 7 8 9 10. The program is compressed using BB96, whereas other schemes are longer due to compression-format headers.

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

PHP, 20 bytes

1 2 3 4 5 6 7 8 9 10

Standard output is implicit outside of PHP tags when reading from a file or standard input.

I'm assuming that an empty separator does not meet the 'arbitrary separator' constraint.

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

Pyke, 2 bytes

TS

Try it here!

one_range(10)
\$\endgroup\$
1
\$\begingroup\$

ListSharp, 37 bytes

[FOREACH NUMB IN 1 TO 10 AS i]
SHOW=i

Pretty straight forward

as a sidenote => is this cheating?

SHOW="10,9,8,7,6,5,4,3,2,1"
\$\endgroup\$
  • 1
    \$\begingroup\$ Hardcoding the output is a standard loophole. \$\endgroup\$ – Neil Jul 21 '16 at 19:04
  • \$\begingroup\$ "Often, this vulnerability is an indicator of a dumb question" \$\endgroup\$ – downrep_nation Jul 21 '16 at 19:27
  • 1
    \$\begingroup\$ @Neil, except kolmogorov-complexity. \$\endgroup\$ – Qwertiy Jul 22 '16 at 19:34
1
\$\begingroup\$

Racket, 15 bytes

(cdr(range 11))

Racket's interpreters implicitly print return values of expressions to standard output, which is nice.

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

Lua, 26 bytes

for i=1,10 do print(i) end

Prints them in seperate lines.

\$\endgroup\$
  • 1
    \$\begingroup\$ Duplicate of Katenkyo's answer posted ~9 hours earlier. \$\endgroup\$ – manatwork Jul 22 '16 at 8:37
1
\$\begingroup\$

Excel VBA, 47 Bytes

Sub t()
For i = 1 To 10: MsgBox i: Next
End Sub
\$\endgroup\$
  • \$\begingroup\$ You can lose 9 bytes by condensing your whitespace down and removing the () following Sub to Sub t For i=1To 10:MsgBox i:Next End Sub \$\endgroup\$ – Taylor Scott Mar 25 '17 at 20:51
1
\$\begingroup\$

Scala, 14 bytes

print(1 to 10)

output

Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
\$\endgroup\$
1
\$\begingroup\$

Racket, 12 bytes

(range 1 11)

Prints '(1 2 3 4 5 6 7 8 9 10).

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

Element, 13 bytes

10'[1+2:`\
`]

Try it online

This outputs one number per line with a trailing newline. This can be shorted to 12 bytes if I replace the newline with a letter (like the letter x). It works by creating a FOR loop, which repeatedly increments the top of the stack, duplicates it, and outputs it.

\$\endgroup\$
  • \$\begingroup\$ Nice! You're in your element! :D \$\endgroup\$ – mbomb007 Jul 21 '16 at 21:16

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.