44
\$\begingroup\$

Main task

Your task is to print out integers in descending order, starting from 1, and increasing as you keep hitting 1 again, up until the given input is reached, then, print out the rest until you hit 1 again. Example with input 6:

1
21
321
4321
54321
654321
Without newlines (valid output):
121321432154321654321
Side note: this is A004736 in the OEIS. Also, the first example (with newlines) is an invalid output, as specified in the rules.

Input

Your code may take any kind of input (graphical, STDIN) in the form of an integer or number.

Output

Your code should output the above described sequence, up until the input number is reached, then finish to output until it reaches 1 again. The output may be anything, therefore numbers, strings, integers, or graphical output. It is required to print out a single number (no newlines if it's a string). Your output can be in- and outroduced with as many characters as you need (e.g. []).

Since there was some misunderstanding, here's a regex pattern you can try your outputs on.

^(\D*(\d)+\D*)$

Rules

  • The output must be a full number, not split up by anything, not even newlines.
  • The algorithm shouldn't check for the first instance of N appearing in any way (e.g. the 21 in 121321), but rather for the first instance of N as the actual number.
  • A single trailing newline is allowed.
  • The handling for negative input is fully your choice, negative numbers aren't cases you should test.

Test cases

Input: 6
Output: 121321432154321654321

Input: 1 Output: 1

Input: 26 Output: 121321432154321654321765432187654321987654321109876543211110987654321121110987654321131211109876543211413121110987654321151413121110987654321161514131211109876543211716151413121110987654321181716151413121110987654321191817161514131211109876543212019181716151413121110987654321212019181716151413121110987654321222120191817161514131211109876543212322212019181716151413121110987654321242322212019181716151413121110987654321252423222120191817161514131211109876543212625242322212019181716151413121110987654321

Input: 0 Output: 0, Empty, or Error

Input: 21 Output: 121321432154321654321765432187654321987654321109876543211110987654321121110987654321131211109876543211413121110987654321151413121110987654321161514131211109876543211716151413121110987654321181716151413121110987654321191817161514131211109876543212019181716151413121110987654321212019181716151413121110987654321

Thanks @Emigna, I used his algorithm to calculate these test cases.

Winner

The winner has been chosen! It was ErikGolfer's answer with an impressive 5 bytes! Congratulations!

\$\endgroup\$
  • \$\begingroup\$ The output must be a full number ... Do you mean the entire sequence, or only the different substrings (1, 2-1, 3-1 ...)? Your first example doesn't seem to match this statement. \$\endgroup\$ – steenbergh Dec 19 '16 at 14:46
  • 1
    \$\begingroup\$ If the output has to be a single number, how can it be "arrays"? \$\endgroup\$ – smls Dec 19 '16 at 15:50
  • \$\begingroup\$ Would this array be acceptable as output? [1, 21, 321, 4321, 54321, 654321] How about this one? [1,2,1,3,2,1,4,3,2,1,5,4,3,2,1,6,5,4,3,2,1] Or are you just talking about arrays with a single element, like [121321432154321654321] ? \$\endgroup\$ – smls Dec 19 '16 at 16:00
  • 1
    \$\begingroup\$ I'm confused about the output format. Can you give examples of what is acceptable? Array of numbers? String with numbers separated by spaces? \$\endgroup\$ – Luis Mendo Dec 19 '16 at 16:19
  • 1
    \$\begingroup\$ Your regex allow output of mickey321211mouse. Really the \D parts have no reason to be there \$\endgroup\$ – edc65 Dec 20 '16 at 13:45

50 Answers 50

13
\$\begingroup\$

Jelly, 5 bytes

RRUVV

Try it online!

Formula not mine.

I suspect there is too much going on here...

[ANSWER ACCEPTED] I'd have given some 5 rep to Dennis, but this is not Reputation Exchange. Dennis showed me the VV behavior. To my surprise, this is shorter than 05AB1E.

\$\endgroup\$
  • \$\begingroup\$ Congratulations, this code has the least amount of code! \$\endgroup\$ – devRicher Dec 23 '16 at 11:16
19
\$\begingroup\$

05AB1E, 6 bytes

L€LíJJ

Try it online!

Explanation

Example input 4

L       # range [1 ... input]
        # STACK: [1,2,3,4]
 €L     # map: range
        # STACK: [[1],[1,2],[1,2,3],[1,2,3,4]]
   í    # reverse each
        # STACK: [[1],[2,1],[3,2,1],[4,3,2,1]]
    J   # join inner lists
        # STACK: ['1','21','321','4321']
     J  # join list
        # OUTPUT: 1213214321
\$\endgroup\$
13
\$\begingroup\$

JavaScript (ES6), 37 bytes

f=(n,k=1)=>k>n?n--?f(n):'':f(n,k+1)+k

Demo

f=(n,k=1)=>k>n?n--?f(n):'':f(n,k+1)+k

console.log(f(6))

Alternate method for n < 10, 34 bytes (non-competing)

f=(n,s='1')=>--n?s+f(n,++s[0]+s):s

In JavaScript, strings are immutable. Therefore, it's impossible to alter the content of the Nth character of a string s by assigning a new value to s[N].

However, the expression ++s[N] is valid and does evaluate as one would expect, even if the string remains unchanged. For instance:

++"1"[0] // equals 2

And by extension:

s = "21"
++s[0] + s // equals "321"
\$\endgroup\$
  • \$\begingroup\$ Does not seem to work for n > 9 \$\endgroup\$ – edc65 Dec 20 '16 at 13:49
  • \$\begingroup\$ @edc65 You're right, of course. I don't know why I thought it was OK to stop at 9. \$\endgroup\$ – Arnauld Dec 20 '16 at 14:06
12
\$\begingroup\$

V, 29 28 27 23 19 17 16 bytes

8 bytes saved thanks to @DJMcMayhem

3 bytes saved thanks to @nmjcman101

"apÀ­ñÄòy$jpkgJ

Hidden characters:

"apÀ<C-x>ñÄ<C-x>òy$jpkgJ

C-x is Ctrl+x.

Try it online! takes input via command-line arguments

Hexdump:

0000000: 2261 70c0 adf1 c418 f279 246a 706b 674a  "ap......y$jpkgJ

Explanation

"ap            Paste the argument
À<C-x>         Argument minus 1 times (so that we exclude the 0)
ñ ... ò        Loop (for some weird reason the ò closes the ñ)
Ä<C-x>         paste current line above and decrement it

Now it looks like:

1
2
...
n

continued...

ò             recursively do (until a breaking error)
y$             yank this line
  jp           paste it down
    kgJ        go up and join
              implicit ò end

GIF (outdated)

(for arg 6)

gif

\$\endgroup\$
  • \$\begingroup\$ I got a few by changing your loop to a) end implicitly and b) join the lines as it goes (rather than at the end) òy$jpkgJ \$\endgroup\$ – nmjcman101 Dec 19 '16 at 16:28
  • \$\begingroup\$ @nmjcman101 Thanks for helping me save 2 bytes! \$\endgroup\$ – Cows quack Dec 19 '16 at 16:43
  • \$\begingroup\$ This is pretty well golfed. I've been racking my brains for a good 20 minutes, and I can't think of anything shorter. :) \$\endgroup\$ – DJMcMayhem Dec 19 '16 at 19:45
  • \$\begingroup\$ @DJMcMayhem It's because I've had some great help :) \$\endgroup\$ – Cows quack Dec 19 '16 at 19:47
  • \$\begingroup\$ I did it! You can get down to 16 bytes. If you paste the arg, and then duplicate/decrement upwards, you get to remove the H. Then if you use the decrement operator on your À, you won't have the 0 at the top so you can remove the x. Then APPARENTLY a ò will close a ­ñ so you can remove the second ­ñ (which is the byte you save). Link because that made no sense \$\endgroup\$ – nmjcman101 Dec 19 '16 at 20:17
11
\$\begingroup\$

C#, 72 69 65 bytes

n=>{for(int i=0,j;i<n;)for(j=++i;j>0;)System.Console.Write(j--);}

If the output can just be returned as opposed to being Written to the console

C#, 71 68 64 bytes

n=>{var s="";for(int i=0,j;i<n;)for(j=++i;j>0;)s+=j--;return s;}

Thanks to @VisualMelon for saving a lot of bytes

Test it here (Humourously the online compiler breaks at any number above 420)

\$\endgroup\$
  • \$\begingroup\$ That was really fast. \$\endgroup\$ – devRicher Dec 19 '16 at 13:09
  • \$\begingroup\$ @devRicher What can I say, I was waiting for something to be posted :P \$\endgroup\$ – Alfie Goodacre Dec 19 '16 at 13:10
  • 2
    \$\begingroup\$ There is never a reason to use a while loop in C# code golf, a for-loop will always perform as well if not better. In this case, you can include the assignment of j=1 in the for-loop, and save a semi-colon. You can also declare j along with i, to save the int. The i++ can also be moved to the j=i assignment, saving a byte. You should also be able to replace the i<=n with i<n if you make it j=++i instead and start i at 0. \$\endgroup\$ – VisualMelon Dec 19 '16 at 15:02
  • \$\begingroup\$ @VisualMelon edited it, saved 3 bytes! Declaring the ints together actually made no difference to the byte count but it makes the loops look a bit nicer \$\endgroup\$ – Alfie Goodacre Dec 19 '16 at 15:10
  • \$\begingroup\$ @AlfieGoodacre if you declare them together in the for loop, then you'll save 2 more bytes for(int i=0,j;i<n;) ;) There is also no need for the {} around the inner for loop. \$\endgroup\$ – VisualMelon Dec 19 '16 at 15:15
8
\$\begingroup\$

Pure bash, 34

eval eval printf %s \\{{1..$1}..1}

Two levels of brace expansion. With input 6, the first level expands to {1..1} {2..1} {3..1} {4..1} {5..1} {6..1}. This then expands to 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1, which is smushed together to one string with printf %s. eval's are required at both levels of expansion - for the first level so that the $1 parameter is expanded first, and for the second level so that it expands after the first level.

Try it online

\$\endgroup\$
8
\$\begingroup\$

Perl, 21 bytes

Uses -E at no extra cost.

say map$}=$_.$},1..<>

Usage

perl -E 'say map$}=$_.$},1..<>' <<< 6
121321432154321654321
\$\endgroup\$
7
\$\begingroup\$

Pyth, 7 bytes

jks}R1S

A program that takes input of an integer and prints the result.

Try it Online!

How it works

jks}R1S   Program. Input: Q
jks}R1SQ  Implicit input fill
    R     Map
      SQ  over [1, 2, 3, 4, ..., Q] with i:
   } 1     Yield [i, i-1, i-2, i-3, ..., 1]
  s       Merge
jk        Join
          Implicitly print
\$\endgroup\$
  • \$\begingroup\$ Personally I'm very disappointed that jk_hC.:S is longer, but nice work! \$\endgroup\$ – FryAmTheEggman Dec 19 '16 at 19:10
7
\$\begingroup\$

GeoGebra, 67 bytes

1
InputBox[a]
Sum[Join[Sequence[Sequence[Text[j],j,i,1,-1],i,1,a]]]

Each line is entered separately into the input bar. Input is taken from an input box.

Here is a gif of the execution:

Program execution

How it works

Entering 1 implicitly assigns a to 1, and the InputBox command associates an input box with a. Then, for each i in {1, 2, 3, ..., a}, the list {i, i-1, i-2, ..., 1} is created using the Sequence command, and each j in that list is converted to a string using Text. Finally, Join merges all the lists, and Sum concatenates all the elements to one text object, which is displayed.

\$\endgroup\$
  • \$\begingroup\$ @devRicher That seems reasonable. Thanks! \$\endgroup\$ – TheBikingViking Dec 19 '16 at 18:42
7
\$\begingroup\$

Python 2, 51 bytes

r=s=''
n=0
exec'n+=1;s=`n`+s;r+=s;'*input()
print r
\$\endgroup\$
7
\$\begingroup\$

Retina, 26 22 bytes

Byte count assumes ISO 8859-1 encoding.

.+
$*

$`¶
1
$.%'
0?¶

Try it online!

Explanation

.+
$*

Convert input to unary.


$`¶

At each position, insert the prefix up to that point, as well as a linefeed. This creates a unary range from 2 to n+1, one value per line.

1
$.%'

Replace each 1 with the number of characters after it on the same line. This turns something like 11111 into 43210.

0?¶

Remove all linefeeds and the zeros preceding them.

\$\endgroup\$
5
\$\begingroup\$

GameMaker Language, 65 bytes

b=""for(i=1;i<=argument0;i++){c=i while(j)b+=string(c--)}return b
\$\endgroup\$
5
\$\begingroup\$

APL, 10 bytes

∊⍕¨∘⌽∘⍳¨∘⍳

E.g.:

      (∊⍕¨∘⌽∘⍳¨∘⍳)6
121321432154321654321

Explanation:

  • : get the numbers from 1 to N.
  • ⍳¨∘: for each of those, get the numbers from 1 to N.
  • ⌽∘: reverse that list
  • ⍕¨∘: get the character representation of each item (so it does not output the numbers with spaces in between)
  • : flatten the resulting array
\$\endgroup\$
  • \$\begingroup\$ So it's parsing the code backwards? \$\endgroup\$ – devRicher Dec 19 '16 at 15:17
  • \$\begingroup\$ is function composition, I gave the explanation in the order that the functions are actually evaluated \$\endgroup\$ – marinus Dec 19 '16 at 15:18
  • 1
    \$\begingroup\$ Here's the parse tree, for the curious: tryapl.org/… \$\endgroup\$ – marinus Dec 19 '16 at 15:20
5
\$\begingroup\$

Python 2, 71 68 bytes

I bet a recursive solution could be shorter, but I'm having a hard time formulating this into one.

n=input()
i=0
o=""
while i<n:
    i+=1;j=i
    while j:o+=`j`;j-=1
print o

Try it online

\$\endgroup\$
5
\$\begingroup\$

Actually 8 bytes

RR♂RΣRεj

First time posting an answer in Actually so it probably can be golfed.

How it works

Program takes implicit input, implicit print at EOF
R           Takes the input and creates a range (1, input)   
                STACK = [1,2,..,n]
 R          Reverse the top stack item (our range)
                STACK = [n,..,2,1]
  ♂R        For each item in our range, create a range (1, rangeitem)
                STACK = [[1,2,..,n], .., [1,2], [1]]
    Σ       Stitch the items of the list together
                STACK = [n,..,1,2,3,1,2,1]
     R      Reverse the top stack item again (our answer)
                STACK = [1,2,1,3,2,1,..n]
      εj    Create an empty string and append each item from the list to it.
            (turns non string items into strings)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm not sure that there is a shorter solution, but I've proven myself wrong before. In any case, here's a Try It Online link for your answer. \$\endgroup\$ – Sherlock9 Dec 19 '16 at 16:31
  • 1
    \$\begingroup\$ The code R♂R♂RΣεj is the same number of bytes, but it may be easier to write an explanation for. \$\endgroup\$ – Sherlock9 Dec 19 '16 at 16:35
  • \$\begingroup\$ @Sherlock9 your way is a little more elegant, I've added the link and an explanation which I forgot to add on yesterday to try explain it a bit more. \$\endgroup\$ – Teal pelican Dec 20 '16 at 9:28
4
\$\begingroup\$

Brachylog, 8 bytes

yb@[rcw\

Try it online!

Explanation

yb         The list [1, ..., Input]
  @[       Take a prefix of that list
    rc     Reverse it and concatenate into an integer
      w    Write to STDOUT
       \   Backtrack: try another prefix
\$\endgroup\$
4
\$\begingroup\$

Perl 6, 22 bytes

{[~] flat [\R,] 1..$_}

A lambda that returns a string.

(Try it online.)

Explanation:

  • 1..$_: Range of integers... (1 2 3 4)
  • [,] 1..$_: Reduce ("fold") over comma operator... (1 2 3 4)
  • [\,] 1..$_: With intermediate results (triangular reduce)... ((1) (1 2) (1 2 3) (1 2 3 4))
  • [\R,] 1..$_: Apply reversing meta-operator to the comma... ((1) (2 1) (3 2 1) (4 3 2 1))
  • [~] flat ...: Remove list nesting, and fold over string concat operator... 1213214321
\$\endgroup\$
4
\$\begingroup\$

Haskell, 35 bytes

f x=[1..x]>>= \y->[y,y-1..1]>>=show

Usage example: f 6 -> "121321432154321654321".

For all numbers x in 1 ... x make a list x,x-1, ... ,1, turn the numbers into a string and concatenate them into a single string. Again, concatenate those strings into a single string.

\$\endgroup\$
4
\$\begingroup\$

C89, 54 bytes

i,j;f(n){for(i=1;j<=n;j=i++)while(j)printf("%d",j--);}

56 -2 = 54 thanks to ErikGolfer!

\$\endgroup\$
  • \$\begingroup\$ I think you can do (j=i++) instead of (j=i) and remove the last i++ (untested). \$\endgroup\$ – Erik the Outgolfer Dec 19 '16 at 15:00
  • \$\begingroup\$ Here's a shorter recursive version: i,j;f(n){j=++i;while(j)printf("%d",j--);i-n?f(n):0;} (52 bytes) \$\endgroup\$ – Steadybox Dec 19 '16 at 16:33
  • \$\begingroup\$ @Steadybox You can add that as your own answer if you like, but thanks! \$\endgroup\$ – cat Dec 20 '16 at 14:01
  • \$\begingroup\$ @cat Ok, thanks, just did. Wasn't sure if I should since I only edited your solution. \$\endgroup\$ – Steadybox Dec 20 '16 at 22:35
4
\$\begingroup\$

Python 3, 87 92 83 74 bytes

lambda n:"".join(["".join([str(i)for i in range(1,k)][::-1])for k in range(1,n+2)])

Shorter answer using recursion :

f=lambda n:f(n-1)+"".join([str(i)for i in range(1,n+1)][::-1])if n>0else""

Maybe not the shortest one but it's only made with Python's list comprehension !

(Edited to add the print function and remove the \n)

(Edited to remove the print function, and change n+1, k+1 to n,k+2)

\$\endgroup\$
  • \$\begingroup\$ Works with k, n+2 but not with k+2, n, thanks though for the idea :) \$\endgroup\$ – Sygmei Dec 22 '16 at 9:16
  • \$\begingroup\$ The code that you're scoring should be first. Also, you should use Python 2 and then use `i` instead of str(i). And you can use "".join(...) instead of "".join([...]), and range(1,k,-1) to remove the [...][::-1]. \$\endgroup\$ – mbomb007 Dec 22 '16 at 14:52
  • \$\begingroup\$ Also, n>0 can be n. And I meant range(n,0,-1). And use n and f(n-1)+...)or"". \$\endgroup\$ – mbomb007 Dec 22 '16 at 14:58
  • 1
    \$\begingroup\$ 62 bytes. Actually, this may be getting too close to this answer. \$\endgroup\$ – mbomb007 Dec 22 '16 at 15:03
  • \$\begingroup\$ Yes this is getting pretty close, I saw that after doing my second version :( \$\endgroup\$ – Sygmei Dec 22 '16 at 16:05
3
\$\begingroup\$

Pyth, 8 bytes

jks_M._S

Explanation

jks_M._SQ   Implicit input
       SQ   Get the range [1, 2, ..., N]
     ._     Get each prefix
   _M       Reverse each prefix
jks         Join everything as a string
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 6 bytes

LR.sJJ

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 36 bytes

ToString/@(""<>Range[Range@#,1,-1])&

Throws a bunch of warnings which can be safely ignored.

Explanation

Using input 5 as an example:

Range@#

Creates a range {1, 2, 3, 4, 5}.

Range[...,1,-1]

Range is listable so we can give it a list for any of its arguments and it will automatically thread over that argument. So this gives us a bunch of reversed lists:

{{1}, {2, 1}, {3, 2, 1}, {4, 3, 2, 1}, {5, 4, 3, 2, 1}}

Next:

(""<>...)

This joins the nested list with the empty string. Since the nested list doesn't actually contain any strings, it can't really join the values (which is where the warnings are generated), but ""<> has the side-effect of flattening the list. So this gives us

1 <> 2 <> 1 <> 3 <> 2 <> 1 <> 4 <> 3 <> 2 <> 1 <> 5 <> 4 <> 3 <> 2 <> 1

Now comes Mathematica's beautiful feature that Map doesn't care about the structure it's mapping over. You normally apply it to a list, but it works with any head. f /@ h[a, b, c] simply gives you h[f[a], f[b], f[c]]. In our case, the head is StringJoin and the values are the integers.

ToString/@...

So this simply turns the integers into strings. At that point StringJoin[...] knows what to do with them and joins them all into a single string:

"121321432154321"
\$\endgroup\$
  • 1
    \$\begingroup\$ That's just plain nasty. :) \$\endgroup\$ – Greg Martin Dec 20 '16 at 0:59
3
\$\begingroup\$

GolfScript, 14 bytes

~,{),{)}%-1%}%

Try it online!

Usual method of course, but this is GolfScript.

Explanation for this VAST piece of code:

~,{),{)}%-1%}% # Code
               # Initial stack.      ["n"]
~              # Eval ToS.           [n]
 ,             # ToS' lowered range. [[0..n)]
  {),{)}%-1%}  # Block. 1 argument.  [a]
   )           # Increment.          [a+1]
    ,          # Range.              [[0..a)]
     {)}       # Block. 1 argument.  [b]
      )        # Increment.          [b+1]
        %      # Map.                [[1..a]]
         -1    # Integer. -1         [[1..a] -1]
           %   # Each nth element.   [[a..1]]
             % # Map.                [[[1],[2,1],...,[n..1]]]
               # Implicit output.    121...n..1

Note that output is as a single number. Trailing \n.

\$\endgroup\$
3
\$\begingroup\$

R, 38 33 44 bytes

if((n=scan())>0)for(i in 1:n)cat(i:1,sep="")

Takes input to STDIN, and loops from 1 to n, creating the sequence i to 1 for each step and printing it.

Edit: replaced seq(i,1) by i:1 saving 5 bytes and showing why I shouldn't golf during meetings.

\$\endgroup\$
  • \$\begingroup\$ This produces 101 if the input is 0. if((n=scan())>0)for(i in 1:n)cat(i:1,sep="") does the trick. \$\endgroup\$ – Frédéric Dec 19 '16 at 17:44
  • \$\begingroup\$ Damn, assumed non-zero input :( \$\endgroup\$ – JAD Dec 19 '16 at 18:19
  • \$\begingroup\$ if(n<-scan()) should be sufficient. \$\endgroup\$ – Giuseppe Nov 9 '17 at 16:16
3
\$\begingroup\$

MATL, 14 11 bytes

:"@:P]v!VXz

Try it online!

Explanation

:      % Input N implicitly. Push range [1 2 ...N]
"      % For each k in [1 2 ...N]
  @:   %   Push range [1 2 ... k]
  P    %   Reverse
]      % End
v!     % Concatenate all arrays horizontally
V      % Convert to string
Xz     % Remove spaces. Display implicitly
\$\endgroup\$
  • \$\begingroup\$ not split up by anything, don't think the second one is allowed. \$\endgroup\$ – JAD Dec 19 '16 at 16:24
  • 2
    \$\begingroup\$ @JarkoDubbeldam I'll delete that one until OP clarifies \$\endgroup\$ – Luis Mendo Dec 19 '16 at 16:26
  • 1
    \$\begingroup\$ @Jarko The OP clarified. Current solution conforms to spec \$\endgroup\$ – Luis Mendo Dec 19 '16 at 19:37
3
\$\begingroup\$

brainfuck, 17 bytes

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

Explanation

>           keep the first cell at 0
 ,          input of the decimal number into the cell
  [>        start a conditionnal loop and go to the next cell
   [+.>]    while it don't find 0, increment each cells and output the value
    +.      increment the new cell and output
     [<]    go to the first cell
      >-]   decrement the second cell and restart

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Have you read the tour page and the hottest meta questions yet? I advise you to, they're helpful! Also, wrap your code in code formatting! Have you read the formatting help? You should also explain how your code works, if you can! \$\endgroup\$ – devRicher Dec 21 '16 at 18:53
  • \$\begingroup\$ @muddyfish I've just added the explanation \$\endgroup\$ – Milihhard Dec 21 '16 at 19:10
  • \$\begingroup\$ I've used a mobile app. You can see the screen of the result. \$\endgroup\$ – Milihhard Dec 21 '16 at 19:21
  • \$\begingroup\$ No it's fine. But in the link, the ouput is in ascii and not in decimal, so you don't really see the output \$\endgroup\$ – Milihhard Dec 21 '16 at 20:13
  • \$\begingroup\$ @muddyfish Your permalink is a tad confusing. There's an invisible 0x06 in the input, followed by the decimal digit 6. \$\endgroup\$ – Dennis Dec 21 '16 at 20:14
3
\$\begingroup\$

Python, 63 57 59 bytes

A recursive solution that works in both Python 2 and 3. This can probably be golfed further. Golfing suggestions welcome! Try it online!

Edit: -6 bytes thanks to Jonathan Allan. +2 bytes with thanks to mbomb007 for pointing out a problem with my answer.

f=lambda n:n and f(n-1)+"".join(map(str,range(n,0,-1)))or""

Ungolfing

def f(n):
    s = ""
    for i in range(n+1):
        m = map(str, range(n, 0, -1))
        s += "".join(m)
    return s
\$\endgroup\$
  • 2
    \$\begingroup\$ Use a map to save 6 bytes: lambda n:n and f(n-1)+"".join(map(str,range(n,0,-1)))or"" \$\endgroup\$ – Jonathan Allan Dec 19 '16 at 19:26
2
\$\begingroup\$

PHP, 35 34 33 bytes

Saved a byte because I miscounted, thanks Titus! And another!

while($i++<$argv[1])echo$s=$i.$s;

Run from command line with -r.

Pretty simple answer, loops from 1 through our input n, tacking the number onto the beginning of the string and printing it out.

\$\endgroup\$
  • \$\begingroup\$ I count 34. One byte shorter with post-increment. \$\endgroup\$ – Titus Dec 19 '16 at 16:01
  • \$\begingroup\$ 33 bytes: while($i++<$argv[1])echo$s=$i.$s; \$\endgroup\$ – aross Dec 20 '16 at 10:51
1
\$\begingroup\$

CJam, 13 bytes

ri,:){,:)W%}%

interpreter

\$\endgroup\$
  • \$\begingroup\$ You can save 2 bytes: ri{),:)W%}% \$\endgroup\$ – Luis Mendo Dec 19 '16 at 16:25
  • \$\begingroup\$ @LuisMendo Neat, thanks for that! \$\endgroup\$ – Erik the Outgolfer Dec 19 '16 at 17:21

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.