The Rien Number

38

4

The Champernowne constant is a number that is constructed by concatenating the first n numbers, with n tending to infinity. It looks something like this:

0.123456789101112131415161718192021222324252627282930...

Now, I will describe to you the Rien number. It can be thought of as a minimization of the Champernowne constant as an integer. I will refer to the Rien number with the first n digits as Ri(n). This is how to formulate it:

  1. The first n natural numbers (the sequence {1,2,3,...}) are concatenated.
  2. This result is then sorted, according to the digit value. So 1..12 would look like 011111223456789.
  3. Since the Rien number cannot have leading zeroes, we move all 0s so that they are significant, whilst keeping the number minimized, resulting in, say, 101111223456789. This is Ri(n), in this case, Ri(12).

Here are some results for Ri(n):

n    Ri(n)
1    1
2    12
3    123
7    1234567
9    123456789
10   10123456789
15   101111111223344556789
34   10001111111111111222222222222223333333334444555666777888999
42   100001111111111111122222222222222233333333333333444444455556666777788889999
45   100001111111111111122222222222222233333333333333344444444444555556666777788889999
55   10000011111111111111122222222222222223333333333333333444444444444444455555555555566666777778888899999
100  100000000000111111111111111111112222222222222222222233333333333333333333444444444444444444445555555555555555555566666666666666666666777777777777777777778888888888888888888899999999999999999999
999  100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Objective Given a number 1 ≤ n < 10000 as input (via arguments, STDIN, or hardcoding if your language does not support conventional input), output/return Ri(n).

This is a , so the shortest code in bytes wins. You may use a language that was made after this contest, as long as it was not made for answering this challenge. (Of course, you can use it, if it provides an interesting solution, but mark your answer as non-competing.)

Reference implementation

I tested this in IE, so there really shouldn't be a problem. If there is a problem, there's an easy solution: get a sane browser.

function min(n) {
  var seq = [];
  for(var i = 1; i <= n; i++) seq.push(i);
  seq = seq.join("").split("").map(Number);
  var to;
  if(seq.indexOf(1) >= 0) to = seq.splice(seq.indexOf(1), 1);
  seq.sort(function(a, b) {
    return a - b;
  });
  if(to) seq = to.concat(seq);
  return seq.join("");
}
t.onchange = t.onkeyup = function() {
  h.innerHTML = min(this.value)
}
* {
  font-family: Consolas, monospace;
}
input {
  border: 2px dotted #aaaaaa;
  border-radius: 5px;
  margin: 10px;
}
<input id="t" type="number">
<div id="h">

Leaderboard

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=68685,OVERRIDE_USER=44713;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>

Conor O'Brien

Posted 2016-01-06T19:34:27.637

Reputation: 36 228

22I always thought 0 is the rien number. – flawr – 2016-01-06T19:36:15.650

Not sure if I'm missing something, but we can just move one of the 1s in front of the 0s, yeah? – FryAmTheEggman – 2016-01-06T19:40:22.213

@FryAmTheEggman You are correct. – Conor O'Brien – 2016-01-06T19:40:40.193

@MartinBüttner \o/ you found it. – Conor O'Brien – 2016-01-06T20:02:16.427

@CᴏɴᴏʀO'Bʀɪᴇɴ Nope that one was different. That one only allowed permutations of the entire numbers, not of their individual digits. – Martin Ender – 2016-01-06T20:05:21.483

@MartinBüttner *You found my inspiration. Maybe I should submit on OEIS on this? – Conor O'Brien – 2016-01-06T20:06:03.037

@CᴏɴᴏʀO'Bʀɪᴇɴ Sure why not. – Martin Ender – 2016-01-06T20:08:28.017

Shouldn't the output for input 10 be 10123456789? – Zach Gates – 2016-01-06T21:01:56.883

@Zach gates yes, my mistake – Conor O'Brien – 2016-01-06T21:03:31.843

@flawr I agree. :D – HyperNeutrino – 2016-04-03T22:19:47.253

Answers

12

Pyth, 8

+1SjktSQ

Makes a list [1, .. , input] then removes the leading one, joins and sorts, then prepends a 1.

Test Suite

FryAmTheEggman

Posted 2016-01-06T19:34:27.637

Reputation: 16 206

13

Perl, 44 42 41 33 31 bytes

Yaaay, first post ever !

Thanks to primo for the 2 bytes save.

print 1,sort"@{[2..<>]}"=~/\d/g

As others did, removing 1 and prepending it manually does the work.

Try it online

Paul Picard

Posted 2016-01-06T19:34:27.637

Reputation: 863

2

Welcome to PPCG, and congrats on your first post (though it seems strange having me welcome you, since you joined before I did ...). Two things -- I count 43 bytes ... second, your "Try it online" link looks to point to an older/different revision of your code. Manually copy-pasting your code into Ideone works, but not your link.

– AdmBorkBork – 2016-01-06T20:49:53.960

Thank you for you comment ! I used TextWrangler for the byte count, guess I went 1 too far... (plus a space wasn't needed, so that reduces the whole bytecount to 42). The Ideone should be fixed now. – Paul Picard – 2016-01-06T20:52:55.987

Oh, didn't know about that. Also works on my Perl on my Mac (5.18) Thanks ! – Paul Picard – 2016-01-06T20:54:51.717

5Crossed out 44 is still regular 44 ;( – AdmBorkBork – 2016-01-06T20:56:18.903

Haha, I didn't know about that meme ! I read a lot of challenges and answers before posting, but I never noticed that :p – Paul Picard – 2016-01-06T20:58:47.070

2Saved two bytes by getting rid of the split/join: print 1,sort"@{[2..<>]}"=~/\d/g – primo – 2016-01-07T11:32:43.533

Didn't know you could use the regexp to split the number. Nice find ! – Paul Picard – 2016-01-07T20:16:32.013

11

Japt, 14 12 bytes

1+2o°U ¬¬n ¬

Try it online!

How it works

1+2o°U ¬¬n ¬  // Implicit: U = input integer
  2o°U        // Generate the range of integers from 2 to U, inclusive.
       ¬¬     // Join, then split into chars.
         n    // Sort.
1+         ¬  // Join again, and add a 1 to the beginning.
              // Implicit: output last expression

ETHproductions

Posted 2016-01-06T19:34:27.637

Reputation: 47 880

4o_o you golfed it in the 5 minute grace period? [tag:fastest-gun-in-the-west] – Conor O'Brien – 2016-01-06T19:41:39.127

@CᴏɴᴏʀO'Bʀɪᴇɴ I'm guessing you don't want to post new code golfs after that :D – Pierre Arlaud – 2016-01-07T09:30:07.910

9

Retina, 78 bytes

Time to show off some new Retina features (it's still not very competitive, but before today this would probably have been closer to 300 bytes).

.+
$0$*1
\B
 $`
(1)+
$#1
^1| 

.
 1$0$*1
+r`(1+\2) (1+)\b
$2 $1
 1(1)*
$#1
^
1

Try it online.

Explanation

While it's possible to convert between decimal and unary quite conveniently now, this is still quite long because I have to convert back and forth several times because some operations are more doable in decimal than in unary and vice-versa.

.+
$0$*1

Let's start by converting the input to unary. This works by matching the input and then using $*1 which repeats 1 that many times (this repetition feature is new as of today).

\B
 $`

Next, we generate a range from 1 to N in unary. I've explained why this works in my FizzBuzz answer.

(1)+
$#1

We convert each number in the range back to decimal so we can work with the decimal digits. This is done by matching each of the unary numbers such that each 1 generates a separate capture. Then we replace that with the number of captures of group one, using the new capture count syntax $#1.

^1| 

This removes the leading 1 as well as all spaces from the string so we're left with only the digits (except for a single 1).

.
 1$0$*1

We convert back to unary and add 1 to each digit (to ensure that even 0 is a non-empty). We also insert a space in front of each digit to ensure that they are separated.

+r`(1+\2) (1+)\b
$2 $1

We repeatedly match a small number preceded by a larger number and swap them. That's bubble sort in Retina. :)

 1(1)*
$#1

Aaaand back to decimal.

^
1

Finally, we insert a single 1 at the front to account for the one we've removed earlier.

Martin Ender

Posted 2016-01-06T19:34:27.637

Reputation: 184 808

1What new features do ye speak of? – Conor O'Brien – 2016-01-06T19:58:14.540

1

@CᴏɴᴏʀO'Bʀɪᴇɴ I'll add an explanation later. This one uses new substitution syntax for counting captures and repeating characters which can be used for reasonably short decimal/unary conversion. Here is the full changelog: https://github.com/mbuettner/retina/blob/master/CHANGELOG.md

– Martin Ender – 2016-01-06T19:59:05.573

@MartinBüttner nice. Does this mean Retina can no longer claim this exemption?

– Digital Trauma – 2016-01-06T20:17:23.263

@DigitalTrauma I think it's still the most natural form of input. Also I never understood how that kind of language-ism ever got so much support on this site. – Martin Ender – 2016-01-06T20:18:35.097

6

Haskell, 44 bytes

import Data.List
f n='1':sort(show=<<[2..n])

Unfortunately sort is in Data.List, that's 17 bytes!

nimi

Posted 2016-01-06T19:34:27.637

Reputation: 34 639

6

JavaScript (ES6), 65 62 54 52 bytes

Saved 3 bytes thanks to edc65

x=>eval("for(b='';x>1;)1+[...b+=x--].sort().join``")

Builds a string of all numbers from 2 to x, then splits, sorts, joins, and adds a 1 to the beginning. This may still be golfable; suggestions welcome!

ETHproductions

Posted 2016-01-06T19:34:27.637

Reputation: 47 880

I don't have ES6 on hand, but can't you just use (new) Array(x-1).map((_,y)=>y+2)? – Conor O'Brien – 2016-01-06T19:57:00.180

@CᴏɴᴏʀO'Bʀɪᴇɴ map skip empty array elements, so you should use Array(n).fill().map(... (see ES6 tips) – edc65 – 2016-01-06T20:55:42.157

Too late to post mine, but a hint for you: n=>1+[...[...Array(n-1)].map(_=>++n,n=1).join``].sort().join`` (1 byte shorter, it's the split) – edc65 – 2016-01-06T20:56:44.447

@edc65 and Cᴏɴᴏʀ O'Bʀɪᴇɴ Thanks for the tips! I wondered why using .split() felt so odd... – ETHproductions – 2016-01-06T21:44:43.700

@edc65 Huh, is my solution the same length as yours: n=>1+[...[...Array(n+1).keys()].slice(2).join\`].sort().join``` – Neil – 2016-01-07T12:59:08.273

5

CJam, 9

1qi),2>s$

Try it online

Alternatively:

qi,:)(\s$

FryAmTheEggman

Posted 2016-01-06T19:34:27.637

Reputation: 16 206

5

Mathematica, 52 bytes

"1"<>ToString/@Sort[Join@@IntegerDigits[2~Range~#]]&

Once again, string processing happened...

LegionMammal978

Posted 2016-01-06T19:34:27.637

Reputation: 15 731

I think IntegerDigits threads over lists so you don't need map it. – Martin Ender – 2016-01-06T22:44:59.013

Oh right, precedence. – Martin Ender – 2016-01-07T11:24:32.053

4

APL (17)

'1',∆[⍋∆←1↓∊⍕¨⍳⎕]

Explanation:

'1',∆[⍋∆←1↓∊⍕¨⍳⎕]
-----------------
               ⎕   read a number from the keyboard
               ⍳    get the natural numbers up to and including that number
             ⍕¨    get the string representation for each number
           ∊       flatten the array (giving a string of digits)
         1↓        remove the first digit (which is always 1)
       ∆←          store the result in ∆
      ⍋            get a permutation to sort ∆ upwards
    ∆[           ] rearrange ∆ so that it is sorted
'1',               add a 1 to the front

Test:

      '1',∆[⍋∆←1↓∊⍕¨⍳⎕]
⎕:
      1
1
      '1',∆[⍋∆←1↓∊⍕¨⍳⎕]
⎕:
      10
10123456789
      '1',∆[⍋∆←1↓∊⍕¨⍳⎕]
⎕:
      55
10000011111111111111122222222222222223333333333333333444444444444444455555555555566666777778888899999

marinus

Posted 2016-01-06T19:34:27.637

Reputation: 30 224

Nice! Much better than mine. :D I didn't realize that would flatten the array and give you a string. That's good to know. – Alex A. – 2016-01-07T20:50:47.250

3

JavaScript ES6, 49 46 Bytes

Thanks to edc65 for 2 bytes

F=
x=>1+[...(g=_=>x>1?x--+g``:_)``].sort().join``

;console.log(F(15))

l4m2

Posted 2016-01-06T19:34:27.637

Reputation: 5 985

1I got something similar but 1 byte shorter. Try using a recursive function with no argument, just decreasing the original parameter x – edc65 – 2018-03-19T08:19:13.187

3

Bash + GNU utilities, 58

seq $1|sed 's/./&\n/g'|sort|tr -d \\n|sed 's/\(0*\)1/1\1/'

Try it online.

Digital Trauma

Posted 2016-01-06T19:34:27.637

Reputation: 64 644

1I had a similar approach, but a different handling of the "1 at beginning" part (52 bytes). With yours, you can shave off : sort (1 digit long, no need for -n). – Olivier Dulac – 2016-01-08T14:02:16.037

3

Python 2, 60 bytes

_="".join;print"1"+_(sorted(_(map(str,range(2,input()+1)))))

Blue

Posted 2016-01-06T19:34:27.637

Reputation: 26 661

3

ClojureScript, 48 bytes

#(apply str"1"(sort(apply str(range 2(inc %)))))

Same as all of the others, pretty much. REPL available here.

MattPutnam

Posted 2016-01-06T19:34:27.637

Reputation: 521

3

Smalltalk, 76 bytes

As usual in Smalltalk, conceptually very terse, but textually very verbose...

f:l^'1',((2to:l)fold:[:p :q|p asString,q asString])asByteArray sort asString

Add this as a class method for String and call like this, e.g. for 20, String f: 20

user15259

Posted 2016-01-06T19:34:27.637

Reputation:

3

Ruby, 48 bytes

An anonymous function. Basically just Rubyfied some of the other answers here..

->n{n>1?(?1+[*2..n].join.chars.sort*'').to_i: n}

daniero

Posted 2016-01-06T19:34:27.637

Reputation: 17 193

3

Brachylog, 76 41 bytes

1 .;{,1:.e?}?:1fcbZlL,ZoOlM,10^(L-M)=:Oc.

Takes a number as input.

This solution works with the few changes I made to the built-in Findall predicate f. OP is apparently OK with using languages older than the answer so I think this is fine (the changes I made were intented a long time ago, I just motivated myself to do it because of this challenge).

Explanation

1 .                                            § If the input is 1, unify output with 1

   ;                                           § Else

    {      }?:1f                               § Output a list of all inputs which satisfy
                                               § the predicate in brackets with the input
                                               § of the main predicate (ie the input number)
                                               § as output

     ,1:.e?                                    § True if the input is an integer between 1
                                               § and . (the output)

                cbZ                            § Concatenate everything into a single number,
                                               § remove the first digit (1) and call it Z

                   lL,ZoOlM,                   § L is the length of Z, M is the length of O
                                               § O being Z sorted (which removes the leading
                                               § 0s)

                            10^(L-M)=:Oc.      § Concatenate 10^(L-M) at the beginning of O
                                               § and unify it with the output

Fatalize

Posted 2016-01-06T19:34:27.637

Reputation: 32 976

3

Bash, 35 34 bytes

printf %d 1`seq 2 $1|fold -1|sort`

This builds on the answers of @DigitalTrauma and @OlivierDulac. Try it online with Ideone.

How it works

  • seq 2 $1 prints all integers from 2 to the one specified on the command line.

  • fold -1 wraps all lines with width 1, i.e., places each character on its own line.

    -1 seems to be an undocumented feature.

  • sort sorts the characters by their numeric value.

  • printf %d 1`...`​ prepends a 1 to the first line and prints each line as an integer (%d), with no separation.

    This takes advantage of Bash's curious printf implementation, which repeats the format string over and over, until all arguments are consumed.

Dennis

Posted 2016-01-06T19:34:27.637

Reputation: 196 637

+1, I like that one better than ours :) – Olivier Dulac – 2016-01-14T18:32:00.833

2

Julia, 33 bytes

n->"1"*join(sort([join(2:n)...]))

This is a lambda function that accepts an integer and returns a string. To call it, assign it to a variable.

We construct the range 2:n, which will be empty for n < 2, join it into a string, splat the string into an array of characters, sort them, join it into a string, and prepend 1.

Alex A.

Posted 2016-01-06T19:34:27.637

Reputation: 23 761

2

PowerShell, 61 59 bytes

param($a)(("1"+-join([char[]]-join(2..$a)|sort)),1)[$a-eq1]

Takes input param($a) and then uses it to index into an array with [$a-eq1]. If true, we index the second element and output 1. Otherwise, we concatenate "1" with the joined array created by 1) defining a new range 2..$a that has been itself joined together, 2) casting that as a char-array, and 3) sending it through the Sort-Object cmdlet, all of which is then output.

Edit1 -- Saved 2 bytes by moving the inner -join operator.

AdmBorkBork

Posted 2016-01-06T19:34:27.637

Reputation: 41 581

2

APL, 21 bytes

{' '~⍨⍕1,x[⍋x←⍕1↓⍳⍵]}

This is an unnamed monadic function that accepts an integer on the right and returns a string. To call it, assign it to a variable.

Explanation:

            x←⍕1↓⍳⍵]}   ⍝ Get the numbers 1:input, drop 1, convert to string
         x[⍋            ⍝ Sort
      ⍕1,               ⍝ Tack 1 onto the front, flatten to string
{' '~⍨                  ⍝ Remove the spaces (side effect of ⍕ on an array)

Try it online

Alex A.

Posted 2016-01-06T19:34:27.637

Reputation: 23 761

2

Python 2, 60 bytes

P=input();print"1"+"".join(sorted(`range(2,P+1)`)[P*2-4:-2])

Indexing for the win. :-)


Python 2, 60 bytes

P=input();print"1"+"".join(sorted(`range(-P,-1)`))[P*3-5:-2]

Just an alternative.

Zach Gates

Posted 2016-01-06T19:34:27.637

Reputation: 6 152

2

Milky Way 1.6.4, 22 bytes (not competing)

^^'LH=^^JB", "-Q"1";+!

I had to add a sorting opcode for this challenge.


Explanation

^^                      ` pop the TOS
  '                     ` read input from the command line
   LH                   ` push a reversed Pythonic range(TOS+1)
     =^^J               ` remove the top 2 items from the TOS
         B              ` push the string literal of the TOS
          ", "-         ` remove ", " from the TOS
               Q        ` sort the TOS
                "1";+   ` add "1" to the beginning of the TOS
                     !  ` output the TOS

Zach Gates

Posted 2016-01-06T19:34:27.637

Reputation: 6 152

2

MATL, 17 bytes

Uses current version (7.0.0) of language/compiler.

49[]i:"@YUh]6L)Sh

Inspired by FryTheEgggman's answer.

EDIT (July 29, 2016): You can try it online with some modifications to conform to changes in the language.

Example

>> matl
 > 49[]i:"@YUh]6L)Sh
 >
> 12
101111223456789

Explanation

49        % push '1'
[]        % push empty array
i:        % input "N" and build vector [1, 2, ... N]
"         % for each number in this vector
   @      % push that number
   YU     % convert to string
   h      % concatenate horizontally
]         % end
6L)       % remove first element
S         % sort
h         % concatenate horizontally

Luis Mendo

Posted 2016-01-06T19:34:27.637

Reputation: 87 464

2

Seriously, 10 bytes

,u2xεjS'1+

Hex Dump:

2c753278ee6a5327312b

Try It Online

Explanation:

,            Read input
 u2x         Push range from 2..n
    εj       Join into string
      S      Sort
       '1+   Prepend a "1"

quintopia

Posted 2016-01-06T19:34:27.637

Reputation: 3 899

2

Bash and GNU tools, 58 52 bytes

echo 1$(seq 2 $1|sed -e 's/./&\n/g'|sort|tr -d \\n)

Olivier Dulac

Posted 2016-01-06T19:34:27.637

Reputation: 209

similar approach than @Digital-trauma , but different way to add the 1, and no need for -n as numbers are 1 digit long. (I saw his answer after I did my first try) – Olivier Dulac – 2016-01-08T13:58:39.517

2

Gogh, 9 7 bytes

GJT1-1P

You can run this using:

$ ./gogh noi 'GJT1-1P' <input>

G     “ Push a range (1, TOS]       ”
J     “ Join the TOS                ”
T     “ Sort the TOS                ”
1-    “ Remove the first 1          ”
P     “ Prepend the TOS to the STOS ”

Zach Gates

Posted 2016-01-06T19:34:27.637

Reputation: 6 152

"You may use a language that was made after this contest, as long as it was not made for answering this challenge." I allow such languages to be competitive in my challenges, usually ;) – Conor O'Brien – 2016-03-18T13:41:48.040

@CᴏɴᴏʀO'Bʀɪᴇɴ: Oh, awesome! Thanks for pointing that out :) – Zach Gates – 2016-03-18T19:17:22.907

2

Jelly, 8 bytes

RDFṢ1œ|Ḍ

Try it online!

How it works

RDFṢ1œ|Ḍ  Main link. Argument: n (integer)

R         Range; yield r := [1, ..., n].
 D        Convert each k in r into the array of its digits in base 10.
  F       Flatten the resulting array of lists.
   Ṣ      Sort the resulting flat list of digits.
    1œ|   Perform multiset union with 1 as left argument.
          This moves a single 1 to the beginning of the list.
       Ḍ  Convert the resulting list of base 10 to integer.

Dennis

Posted 2016-01-06T19:34:27.637

Reputation: 196 637

2

Oracle SQL 11.2, 222 211 bytes

SELECT 1||TRIM(xmlagg(xmlelement(e,c)).EXTRACT('//text()'))FROM(SELECT SUBSTR(s,LEVEL+2,1)c FROM(SELECT MAX(sys_connect_by_path(LEVEL,' '))s FROM DUAL CONNECT BY LEVEL<=:1)CONNECT BY LEVEL<=LENGTH(s)ORDER BY 1);

Un-golfed

SELECT 1||TRIM(xmlagg(xmlelement(e,c)).EXTRACT('//text()'))  -- add leading 1, concatenate each char and remove spaces
FROM   (
         SELECT SUBSTR(s,LEVEL+2,1)c                          -- split the string in characters, omiting the first number (1)   
         FROM   (
                  SELECT MAX(sys_connect_by_path(LEVEL,' '))s -- create a string by concatenating numbers
                  FROM   DUAL 
                  CONNECT BY LEVEL<=:1
                )
         CONNECT BY LEVEL<=LENGTH(s)ORDER BY 1
       )

Jeto

Posted 2016-01-06T19:34:27.637

Reputation: 1 601

1

Stacked, 22 bytes

2\|>''join sorted'1'\+

Try it here! Expects input on TOS. Example on the repl:

> 15
(15)
> 2\|>''join sorted'1'\+
('101111111223344556789')

Explanation

2\|>''join sorted'1'\+
2\|>                    range from 2 to n
    ''join              join by empty string
           sorted       sort it
                 '1'\+  and prepend a '1'

A more fun version:

Stacked, 45 bytes

{*x:1[1+:yield]x 1-*}!toarr''join sorted'1'\+

Expects input as TOS. For example (on the repl):

> 15    (* the input *)
(15)
> {*x:1[1+:yield]x 1-*}!toarr''join sorted'1'\+
('101111111223344556789')
> 

Explanation

{*x:1[1+:yield]x 1-*}!toarr''join sorted'1'\+
{*x:1[1+:yield]x 1-*}                            generator function that...
  x:                                               given x
    1                                              pushes 1
     [        ]x 1-*                               and executes the inside (x-1) times:
      1+                                             adds 1 to the TOS (initially 1);
                                                     2 is the first value yielded
        :yield                                       and yields it for the generator
                     !                           initialize this generator with TOS
                      toarr                      exhaust generator into an array
                           ''join                join by empty strings
                                  sorted         sort the values
                                        '1'\+    prepend '1'.

Conor O'Brien

Posted 2016-01-06T19:34:27.637

Reputation: 36 228

1

05AB1E, 6 bytes

Note: This submission uses features that postdate this challenge and is therefore not competitive

Code:

Lß?J{?

Explanation:

L      # Creates the list [1 .. input]
 ß     # Extract the smallest value of the list
  ?    # Print this value (always 1)
   J   # ''.join(list)
    {  # Sort the string
     ? # Print this value

Uses ISO 8859-1 encoding

Adnan

Posted 2016-01-06T19:34:27.637

Reputation: 41 965

How does this work? – lirtosiast – 2016-01-21T20:43:57.070

@ThomasKwa Explanation added – Adnan – 2016-01-21T20:44:29.947

Whoa. This is fantastic! – Conor O'Brien – 2016-01-21T20:59:34.793

1

Mathcad, 86 "bytes"

The function s(n) uses a for loop to build up the Champernowne "integer" by converting each number to its string form and concatenating them together. The string is then converted to its equivalent vector of ASCII codes and sorted. The function then swaps any leading zeros with the first one, finally converting the vector back to a string.

To check the function, I put the test cases into a vector vn, then applied s to vn using the vectorize operator. I then check the results against the given test case values.

enter image description here


Mathcad is mathematical application based on 2D worksheets comprised of "regions" each of which can be text, a mathematical expression, program, plot or scripted component.

A mathematical or programming instruction is picked from a palette toolbar or entered using a keyboard shortcut. For golfing purposes, an operation ("byte") is taken to be the number of keyboard operations necessary to create a name or expression (for example, to set the variable a to 3, we would write a:=3. The definition operator := is a single keypress ":", as are a and 3 giving a total of 3 "bytes". The programming for operator requires typing ctl-shft-# (or a single click on the programming toolbar) so again is equivalent to 1 byte.

Stuart Bruff

Posted 2016-01-06T19:34:27.637

Reputation: 501

1

Reng v.3.2, 36 bytes

1i#x:xe!Ø;$:1+)å(Eh
å'11µ1nran! ¡n~

Try it here!

This pushes 1 to the stack, then stores the input i into x (i#x). Then, a range of all the necessary numbers is created using :xe!Ø;$:1+)å(Eh, being ended by Ø, which goes the first i.e. next line. å takes every number and pushes the digits of that number.

On the next line, å breaks the last number into its digits, ' sorts the stack, and 11µ removes 1 1 from the stack. A 1 is then printed, and the numbers are reversed then outputted through the loop an! ¡n. The program is terminated with ~.

Conor O'Brien

Posted 2016-01-06T19:34:27.637

Reputation: 36 228

Maybe you could add an explanation about this? – Hannes Karppila – 2016-04-03T23:26:26.487

@HannesKarppila I have added an explanation. – Conor O'Brien – 2016-04-03T23:31:25.653

1

Seriously, 10 bytes

,Rp@εjS@$+

Try it online!

Explanation:

,Rp@εjS@$+
,Rp@        push range(1, input()+1), pop first element (1), swap
    εjS     join list on empty string, sort
       @$+  prepend the 1

Mego

Posted 2016-01-06T19:34:27.637

Reputation: 32 998

1

Pyke, 9 bytes, noncompetitive

Stm`sS1R+

Explanation:

S         -      range(1,inp+1)
 t        -     ^[1:]
  m`      -    map(str,^)
    s     -   sum(^)
     S    -  sort(^)
      1R+ - 1+^

Try it here!

Blue

Posted 2016-01-06T19:34:27.637

Reputation: 26 661

0

Attache, 20 bytes

{"1"+Sort!Join[2:_]}

Try it online!

The point-free version is actually 1 byte longer: "1"&`+@Sort@Join@2&`:. This works as most of the other answers do.

Conor O'Brien

Posted 2016-01-06T19:34:27.637

Reputation: 36 228

0

C (gcc), 172 161 bytes

f(n){char s[++n];int d[10]={0},j=1,k;for(;sprintf(s,"%d",j),j++<n;)for(k=0;s[k];)d[s[k++]-48]++;d[printf("1")]--;for(j=0;j<10;j++)for(k=d[j];k--;putchar(j+48));}

Try it online!

Jonathan Frech

Posted 2016-01-06T19:34:27.637

Reputation: 6 681

@ceilingcat thanks. – Jonathan Frech – 2018-06-21T01:38:46.503

0

Lua, 102 Bytes

How can lua be so long while all other entries are < 30 bytes... I'm sure even Java would be shorter!

t={}for i=2,...do(i..''):gsub(".",function(d)t[#t+1]=d end)end
table.sort(t)print(1 ..table.concat(t))

Ungolfed

t={}                           -- initialise t as a table
for i=2,...                    -- iterate from 2 to n
do
  (i..''):gsub(".",function(d) -- iterate over each digits in i
    t[#t+1]=d                  -- insert the digit into t
  end)
end
table.sort(t)                 -- sort t
print(1 ..table.concat(t))    -- prepend a 1 to the content of t separated by ''

I'm forced to put a space in print(1 .., else it would try to evaluate it to a decimal 1.<decimal part>, and throws an error because . isn't a valid decimal part.

Katenkyo

Posted 2016-01-06T19:34:27.637

Reputation: 2 857