56
\$\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\$
17
  • 10
    \$\begingroup\$ Related (duplicate?) \$\endgroup\$
    – Luis Mendo
    Jul 21, 2016 at 9:07
  • 17
    \$\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\$ Jul 21, 2016 at 9:54
  • 10
    \$\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\$ Jul 21, 2016 at 10:01
  • 19
    \$\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, 2016 at 14:09
  • 11
    \$\begingroup\$ I can't believe every single of the (currently) 71 answers assumes the base should be decimal… \$\endgroup\$ Jul 22, 2016 at 15:05

251 Answers 251

1 2
3
4 5
9
3
\$\begingroup\$

Piet, 40 Codels

Original (10x4): Original Image

Scaled: Scaled x20

Code runs in a tight loop where the output is printed, followed by 32 ASCII. Previously printed number is compared to 9, and the output is used for both incrementing the counter and redirecting the code execution flow. After 10 increments, the code will get stuck in an infinite loop, performing invalid ops.

Try it online!

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

Lua, 25 Bytes

for i=1,10 do print(i)end
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 6 5 bytes

Saved one byte thanks to @mat

10yb.

Try it online!

Explanation

10y        Get the list [0:1:2:3:4:5:6:7:8:9:10]
   b.      Output is that list minus the first element
\$\endgroup\$
2
  • \$\begingroup\$ 5 bytes: 10yb. \$\endgroup\$
    – mat
    Jul 21, 2016 at 11:57
  • \$\begingroup\$ @mat Thanks, I didn't thought of that, even though it's even simpler! \$\endgroup\$
    – Fatalize
    Jul 21, 2016 at 12:00
2
\$\begingroup\$

Perl 5.10, 14 13 bytes

Almost the same as Perl 6:

say for 1..10

I could've done something like say"@{[1..10]}" which is the exact same thing as in Perl 6, but it is too long. :p

Each number is outputted on a separate line.

Thanks to manatwork for saving 1 byte!

Try it here!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ No need for parenthesis around statement modifier's condition. \$\endgroup\$
    – manatwork
    Jul 21, 2016 at 14:10
  • \$\begingroup\$ @manatwork Indeed, thanks! \$\endgroup\$ Jul 21, 2016 at 14:11
  • 2
    \$\begingroup\$ OP says separators are ok but not required. I'd just use say 1..10 \$\endgroup\$ Jul 21, 2016 at 17:47
2
\$\begingroup\$

Brainfuck, 66 Bytes

Sadly, Someone found a 58 Bytes solution, but as we both have different way of solving this, I'll post mine anyway.

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

Output: 1/2/3/4/5/6/7/8/9/10

Here's what the tape looks like at each step, the * shows where the pointer lies.

+++[>+++>++<<-] tape (0*:9:6)cell-2: counter for printing;cell-3: used for ascii-value of 1
>>+[>+++++++<-] tape (0:9:0*:49) 49 is the ascii value of 1
>[>+>+<<-<+>]   tape (0:9:49:0*:49:49) duplicate it 3 times
<--<            tape (0:9*:47:0:49:49) one will be used as a delimiter "/"
[>>>.+<<.<-]    tape (0:0*:47:0:57:49) outputs the numbers 1..9 separated by "/"
>>>>.-.         tape (0:0:47:0:49:48) outputs 10
\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 29 bytes

(dotimes(n 10)(print(+ n 1)))
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Perhaps this would save 1 byte? (dotimes(n 10)(print(1+ n))) \$\endgroup\$
    – gsl
    Jul 23, 2016 at 8:11
2
\$\begingroup\$

Convex, 3 bytes

A´`

Try it online!

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

Swift 3 (Jun 20, 2016), 20 bytes

print(Array(1...10))

Try it online!

Just doing print(1...10) (un)surprisingly prints 1...10 to STDOUT, so we need to unwrap the Range object into an Array. A Set would be shorter, but does not guarantee order (and in practice doesn't, either).

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

Julia 0.2, 11 bytes

print(1:10)

Pretty self explanatory. 1:10 is a range, and print expands it.

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

Cheddar, 7 bytes

->1|>10

Prett simple. |> is range operator. [0, 10] is:

->|>10
\$\endgroup\$
2
  • \$\begingroup\$ What does -> do? Also pretty* \$\endgroup\$
    – Cyoce
    Sep 2, 2016 at 1:15
  • \$\begingroup\$ @Cyoce -> is a function with no arguments. Like ()=> in JavaScript but in Cheddar if you have no args you can drop the parens \$\endgroup\$
    – Downgoat
    Sep 2, 2016 at 1:49
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\$
1
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\$

Emmet (HTML) - 6 bytes

You'll have to excuse me, I'm new to code golf but I think I understand the concept.

{$}*10

Output:

12345678910

Alternatively, if it's required for the numbers to be seperate, adding a p infront of the braces will put it into <p> tags, like so:

<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>

It also requires a tab to make it "go", I've left that out of the byte count.

Please let me know if I've stuffed up somewhere. Thanks!

\$\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\$
2
  • 1
    \$\begingroup\$ That output is not for that code. range() with a single parameter generates starting from 0. \$\endgroup\$
    – manatwork
    Nov 14, 2016 at 10:53
  • \$\begingroup\$ @manatwork Oh, my bad. Updating it :) \$\endgroup\$
    – ABcDexter
    Nov 14, 2016 at 11:09
2
\$\begingroup\$

TrumpScript, 45 bytes

Say "1 2 3 4 5 6 7 8 9 10"!
America is great.

Don't even try using a loop... it's much much longer.

Edit: to clarify, TrumpScript uses only numbers greater than 1,000,000, so counting from 1 to 10 would require a lot of variables.

\$\endgroup\$
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\$
3
  • \$\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, 2016 at 4:39
  • \$\begingroup\$ Right, that is a lot better! Down to 9 chars now. \$\endgroup\$
    – snail_
    Nov 16, 2016 at 19:10
  • \$\begingroup\$ My solution would have been a-:!k@b+:. for 10 bytes \$\endgroup\$ Jun 19, 2017 at 17:19
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\$

Python 3 (REPL): 17 Bytes

list(range(1,11))

Outputs a list of ints!

[1,2,3,4,5,6,7,8,9,10]

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Provide a reason for down voting please. This meets the requirements of the challenge in less bytes than the other Python 3 example on here. I would also love to have commented on that solution, but i need rep to do anything on here... \$\endgroup\$
    – MildCorma
    Jan 26, 2017 at 15:36
  • 3
    \$\begingroup\$ I did not downvote, but if you actually run this as a python program, it won't output anything; this only works in the REPL. I don't see that as a reason to downvote. though. \$\endgroup\$
    – hyper-neutrino
    Jan 26, 2017 at 15:39
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\$

PHP, 23 bytes

while($i<10)echo++$i,_;

prints a trailing delimiter, run with -nr.

or

<?=join(_,range(1,10));

(already posted by BusinessCat a year ago)

or

for(;$i++<10;)echo$i,_;

(by Ismael Miguel in a comment to BusinessCat´s answer)


21 bytes without the delimiters: while($i<10)echo++$i; or <?=join(range(1,10));

21 bytes with native array output: print_r(range(1,10)); (run with -r)

\$\endgroup\$
2
  • \$\begingroup\$ But the first one was already posted almost an year ago by Business Cat in his PHP solution, where you also participated in the discussion… [confused smiley] \$\endgroup\$
    – manatwork
    Jul 7, 2017 at 15:22
  • \$\begingroup\$ @manatwork True ... Didn´t find that when I browsed the answers. I added references. \$\endgroup\$
    – Titus
    Jul 7, 2017 at 15:34
2
\$\begingroup\$

Brain-Flak, 30 bytes

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

Try it online!

\$\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\$
1 2
3
4 5
9

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.