[Question]
  [
## Challenge:
**Input:** A positive integer \$n\$
**Output:**
Create a list in the range \$[1,n]\$, and join it together to a string (i.e. \$n=13\$ would be the string `12345678910111213`).
Now we output a triangle using the prefixes or suffixes of this string, in one of the following four orientations based on the input integer:
* If \$n\equiv 0\pmod 4\$, output it in the triangle shape ◣
* If \$n\equiv 1\pmod 4\$, output it in the triangle shape ◤
* If \$n\equiv 2\pmod 4\$, output it in the triangle shape ◥
* If \$n\equiv 3\pmod 4\$, output it in the triangle shape ◢
**Example:**
Input: \$n=13\$
Because \$13\equiv 1\pmod 4\$, the shape will be ◤. Here three possible valid outputs:
```
12345678910111213    11111111111111111    12345678910111213
1234567891011121     2222222222222222     2345678910111213
123456789101112      333333333333333      345678910111213
12345678910111       44444444444444       45678910111213
1234567891011        5555555555555        5678910111213
123456789101         666666666666         678910111213
12345678910          77777777777          78910111213
1234567891           8888888888           8910111213
123456789            999999999            910111213
12345678             11111111             10111213
1234567              0000000              0111213
123456               111111               111213
12345                11111                11213
1234                 1111                 1213
123                  222                  213
12                   11                   13
1                    3                    3
```
## Challenge rules:
* As you can see at the three valid outputs above, only the correct shape and using all the digits in the **correct order** is important. Apart from that you're free to use prefixes or suffixes; reverses/reflects; diagonal printing; etc. etc. Any of the six possible outputs for each shape is allowed (see the test case below to see *all* valid outputs based on the shape). This allows languages with rotation builtins to use it, but those without can also use an alternative approach of using the prefixes in the correct size from top-to-bottom, or using the prefixes for two of the shapes but suffixes for the other two shapes. Choosing the most appropriate output options for your language is part of the golfing process. :)
* Input is guaranteed to be a positive integer. For \$n=1\$ we simply output `1`.
* Any amount of leading/trailing newlines/spaces are allowed, as long as it prints the correct triangle (without vertical nor horizontal delimiters!) somewhere on the screen.
## General rules:
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins.  
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
* [Standard rules apply](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422) for your answer with [default I/O rules](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/), so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
* [Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* If possible, please add a link with a test for your code (i.e. [TIO](https://tio.run/#)).
* Also, adding an explanation for your answer is highly recommended.
## Test cases:
Input: \$n=5\$  
All possible valid outputs:
```
12345    54321    12345    54321    11111    55555
1234     5432     2345     4321     2222     4444
123      543      345      321      333      333
12       54       45       21       44       22
1        5        5        1        5        1
```
Input: \$n=6\$  
All possible outputs:
```
123456    654321    123456    654321    111111    666666
 12345     65432     23456     54321     22222     55555
  1234      6543      3456      4321      3333      4444
   123       654       456       321       444       333
    12        65        56        21        55        22
     1         6         6         1         6         1
```
Input: \$n=7\$  
All possible outputs:
```
      1          1          7          7          7          1
     12         21         67         76         66         22
    123        321        567        765        555        333
   1234       4321       4567       7654       4444       4444
  12345      54321      34567      76543      33333      55555
 123456     654321     234567     765432     222222     666666
1234567    7654321    1234567    7654321    1111111    7777777
```
Input: \$n=8\$  
All possible outputs:
```
1           1           8           8           8           1
12          21          78          87          77          22
123         321         678         876         666         333
1234        4321        5678        8765        5555        4444
12345       54321       45678       87654       44444       55555
123456      654321      345678      876543      333333      666666
1234567     7654321     2345678     8765432     2222222     7777777
12345678    87654321    12345678    87654321    11111111    88888888
```
Input: \$n=1\$  
Only possible output:
```
1
```
Input: \$n=2\$  
All possible outputs:
```
12    21    12    21    11    22
 1     2     2     1     2     1
```
  
[Answer]
# JavaScript (ES6), ~~93~~ 89 bytes
Returns a matrix of characters.
```
n=>[...(g=n=>n?g(n-1)+n:'')(n)].map((d,y,a)=>a.map(_=>y-(n&2)*y--<0?' ':d)).sort(_=>-n%2)
```
[Try it online!](https://tio.run/##LY7NCsIwEITvPsUe1OxqEmzFH6qpJ6@@gBUNra2WupFUhCI@e63i7Rtm4JvSPm2d@uv9odhl5zY3LZt4r7XGwnTEmwJZBTTmSAhCpoO@2TtiJhtpycT2F48mbhTyMKRRo9R6shEgooxI184/vq3iQUjtah9ICCXMJMwlLCQsJQTTQ0/nzm9tekEGE0PquHbVWVeuwNPO9F/8jhJOuP/KO//P5787r0t3Zexu0Z8SFvRO@ETUfgA "JavaScript (Node.js) – Try It Online")
Alternate pattern (same size):
```
n=>[...(g=n=>n?g(n-1)+n:'')(n)].map((_,y,a)=>a.map(d=>y-(n&2)*y--<0?' ':d)).sort(_=>-n%2)
```
[Try it online!](https://tio.run/##LY7LCsIwEEX3fsUs1MxoEmzFB9XElVt/wIqG1lZFJ5KKUMRvr1XcnQMXzr24p6uycL4/FPv82BSmYWO3WmssTUu8KpFVRENOhCBk2umbuyPuZS0dGet@mhtbK@R@TINaqeVoJUAkOZGufHjg3ljFvZiaxTaSEEuYSJhKmEmYS4jGu44ufFi77IQMxkLmufLXo776Eg8b033xO0k55e6raPu/Xvjugr74M2N7i/6UsqB3ygei5gM "JavaScript (Node.js) – Try It Online")
### Commented
```
n =>                 // n = input
  [...               // split the result of ...
    ( g = n =>       //   ... a call to the recursive function g, taking n
      n ?            //     if n is not equal to 0:
        g(n - 1)     //       append the result of a recursive call with n - 1
        + n          //       append n
      :              //     else:
        ''           //       stop recursion and return an empty string
    )(n)             //   initial call to g
  ].map((d, y, a) => // for each digit d at position y in this array a[]:
    a.map(_ =>       //   for each character in a[]:
      y -            //     we test either y < 0 if (n AND 2) is not set
      (n & 2)        //     or -y < 0 (i.e. y > 0) if (n AND 2) is set
      * y-- < 0      //     and we decrement y afterwards
      ?              //     if the above condition is met:
        ' '          //       append a space
      :              //     else:
        d            //       append d
    )                //   end of inner map()
  )                  // end of outer map()
  .sort(_ => -n % 2) // reverse the rows if n is odd
```
### Shape summary
Below is a summary of the base shape (generated by the nested `map` loops) and the final shape (after the `sort`) for each \$n\bmod 4\$:
```
 n mod 4  | 0     | 1     | 2     | 3
----------+-------+-------+-------+-------
 n & 2    | 0     | 0     | 2     | 2
----------+-------+-------+-------+-------
 test     | y < 0 | y < 0 | y > 0 | y > 0
----------+-------+-------+-------+-------
 base     | #.... | #.... | ##### | #####
 shape    | ##... | ##... | .#### | .####
          | ###.. | ###.. | ..### | ..###
          | ####. | ####. | ...## | ...##
          | ##### | ##### | ....# | ....#
----------+-------+-------+-------+-------
 n % 2    | 0     | 1     | 0     | 1
----------+-------+-------+-------+-------
 reverse? | no    | yes   | no    | yes
----------+-------+-------+-------+-------
 final    | #.... | ##### | ##### | ....#
 shape    | ##... | ####. | .#### | ...##
          | ###.. | ###.. | ..### | ..###
          | ####. | ##... | ...## | .####
          | ##### | #.... | ....# | #####
```
[Answer]
# [Python 2](https://docs.python.org/2/), 94 bytes
```
n=0;s=''
exec"n+=1;s+=`n`;"*input()
K=k=len(s)
while k:k-=1;print s[k^n/-2%-2:].rjust(n%4/2*K)
```
[Try it online!](https://tio.run/##Lc1bCoMwEEDR/1mFBMQXqSa1L2VW4BJKi2AD2sgoSaTt6lOl/b@XM39cP5H03fRQaBhjnrCoLUYRqLfqGGUoapthS23N0oHmxcUJNKhxVBTbBF79MKpAV5qv4WwGcoG96jvlXIZcVredeS7WxRSWuUybxK/E/xHVJgSb/Bu9AAl7KOEARzjBGS4gii8 "Python 2 – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 bytes
Returns an array of lines.
```
õ ¬å+ zU
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVI=&code=9SCs5SsgelU=&input=MTM=)
Saved 2 bytes thanks to [Kevin](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen).
```
õ ¬å+ zU     :Implicit input of integer U
õ            :Range [1,U]
  ¬          :Join to a string
   å+        :Cumulatively reduce by concatenation
      zU     :Rotate clockwise by 90 degrees U times
```
[Answer]
# [Canvas](https://github.com/dzaima/Canvas), 8 [bytes](https://github.com/dzaima/Canvas/blob/master/files/chartable.md)
```
ŗ］∑［］⁸［↷
```
[Try it here!](https://dzaima.github.io/Canvas/?u=JXUwMTU3JXVGRjNEJXUyMjExJXVGRjNCJXVGRjNEJXUyMDc4JXVGRjNCJXUyMUI3,i=MTM_,v=8)
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 94 bytes
```
{[o](|(&reverse xx$_/2+.5),|(*>>.flip xx$_/2+1))([\~](my@a=[~](1..$_).comb)>>.fmt("%{+@a}s"))}
```
[Try it online!](https://tio.run/##NYrRCoIwFEDf/YoxTO7NuGFkBKH4H0vEYoPAMdkilLl@fRnRw4HD4YzSDqeoZ5YpVkUvTAsLZFa@pHWSTVPa7Q85lbhbYFvXpIbH@K8FIojruwU9N30lVimI0g7pbvQNv7N@At/4vOmD44ghKmNZSXRmPqFMrYfr58sK4zwJiYLiiL8YPw "Perl 6 – Try It Online")
Anonymous code block that takes a number and returns a list of lines.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 17 bytes
```
Ｎθ≔⭆θ⊕ιηＧLＬηη⟲⊗θ‖
```
[Try it online!](https://tio.run/##LYu9CsIwEIB3nyLjBeIgbnYSXAq1iD5BGs8kcL006UXw6WMF1@/HBVtcstRaz0uVsc4TFsi6253XNXqGh5TI/moXyEb17ArOyIJPiFobFbbwlujjE8NpMGpA9hIg/NU9iRWES6oTbUvWP4YvQiegu9YOx7Z/0xc "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input `n`.
```
≔⭆θ⊕ιη
```
Create a string by concatenating the numbers `1` to `n`.
```
ＧLＬηη
```
Fill a triangle of that length with the string.
```
⟲⊗θ
```
Rotate the triangle anticlockwise by `n*90` degrees.
```
‖
```
Reflect everything, thus ending up with a triangle that is rotated clockwise by `n*90` degrees.
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 128 bytes
```
n=>{var s="";int i=0,j,p;for(;i<n;s+=++i);for(p=j=s.Length;;)Write("{0,"+(n%4>1?p:0)+"}\n",new string(s[--j],-~n%4>1?j+1:p-j));}
```
[Try it online!](https://tio.run/##TY/LasJAGIX3/1OEAWGGXMiv8ZYx6cqC4KK0hS6si2kcdYIdQya2lJC@Rva9vFheJI266fbjcL5zEuMmRrW3J53MlC6cxVyfXmUuXg5yZopc6V0cb6NWR3H5JnLLRITwLmepyHdSJ@PbY065mmlu7Mi2FbuALEoj4y2l3hV7ztlTrgpJSek7xKa6F8R4k4U@s0n1rImj5bt1NVGzct107bif11BqY5i5KWO8ajl0xVIke3qeISylrYX27qXYPB7nekOZ95AdVEFJV8lYWeQf5UW7VFqeYelXVolVeDYKhzT1d1P/NPVvU3@RVXfIuxO5kVSwXrDuTtD/qPMnokj2ZVW1CH0YQABDGMEYJjAF9AERsA84AAwAh4AjwDHgBHAKff8P "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~95~~ ~~82~~ 79 bytes
```
->n{r=(0...z=/$/=~s=[*1..n]*'').map{|i|" "*n[1]*i+s[0,z-i]};-n&2>0?r:r.reverse}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vushWw0BPT6/KVl9F37au2DZay1BPLy9WS11dUy83saC6JrNGSUFJKy/aMFYrU7s42kCnSjczttZaN0/NyM7AvsiqSK8otSy1qDi19j9Yr4WOoXGsXmpicgZIb1p0JoyTVVNQWlKskFVrDaJr/wMA "Ruby – Try It Online")
3 bytes saved by G B.
[Answer]
# [R](https://www.r-project.org/), ~~152~~ ~~139~~ ~~137~~ 134 bytes
```
function(n,N=nchar(s<-Reduce(paste0,1:n,'')),A=!n%%4%%3)for(i in 1:N)cat(rep('',(M=c(N-i+1,i))[1+!A]*(n%%4>1)),substr(s,1,M[1+A]),'
')
```
Unrolled code :
```
function(n){
  s = Reduce(paste0,1:n,'')      # concat the digits from 1 to n into a string s
  N = nchar(s)                   # store the length of s
  A = !n%%4%%3                   # A = TRUE if n MOD 4 == 1 or 2 
  for(i in 1:N){                 # for 1 to N (length of s)
    M = c(N-i+1,i)               # store N-i+1 and i into a vector
    nSpaces = M[1+!A]*(n%%4>1)   # if n MOD 4 == 1 or 2 then nSpaces = i else nSpaces = N-i+1, 
                                 # but if n MOD 4 == 0 or 1, then force nSpaces = 0
    nDigits = M[1+A]             # if n MOD 4 == 1 or 2 then nDigits = N-i+1 else nDigits = i
    prfx = rep('',)              # create a character vector repeating '' nSpaces times
    sufx = substr(s,1,M[1+A])    # substring s by taking the first nDigits characters
    cat(pr,su,'\n')              # print prfx and sufx using space as separator for the values 
                                 # contatenation (cat function default) and append a newline
  }
```
[Try it online!](https://tio.run/##bY29DoIwFIV3nqIM5N4rl8QGJyImPAAMrsqAtY1dCqEwGZ@9FhMTB894fr4zB1MHszq12NGh46526jHM6I/FWd9XpXEa/KL3LCvHAETc1KnLskOWlWTGGa2wTsiqIzUsOOsJARjbWmFX2FyyJbrIPG36HW6rk4wEv978Ei9YchvDpieGBCj84GRJz0SIjQldBWwZrg4oWgYtfZPijz61V3gD "R – Try It Online")
[Answer]
# APL+WIN, 45 bytes
Prompts for integer
```
m←⊃(⍳⍴s)↑¨⊂s←(⍕⍳n←⎕)~' '⋄⍎(' ⊖⍉⌽'[1+4|n]),'m'
```
[Try it online! Courtesy of Dyalog Classic](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qvz6O2CcZcjzra0/7nApmPupo1HvVuftS7pVjzUdvEQysedTUVA8WBglOB4nkgJX1TNevUFdQfdbc86u3TUFd41DXtUW/no5696tGG2iY1ebGaOuq56v@BZv5P4zI0BgA "APL (Dyalog Classic) – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 108 bytes
```
param($n)0..($y=($x=-join(1..$n)).length-1)|%{' '*(0,0,$_,($z=$y-$_))[$n%4]+-join$x[0..($_,$z,$z,$_)[$n%4]]}
```
[Try it online!](https://tio.run/##LYzdCoIwAIXv9xRjHHOrOVzYTbAnERlCmsWcYkH@5LMvkeBcnfOdr@8@1fBqKucCarOEvhzKlsOLVCmOyXCMJnl2D8@1UlstlKv8/d0kWnyjJabxkacylbCSYzaYElghcvgoK077D2O@q6zEvMf@52INKyH6rJS@bCoGS3nb3WhGDcXGR5kQV0YOqCksYYys4Qc "PowerShell – Try It Online")
A bit rough around the edges but works. Joins the digits 1 to `n` into a string then iterates from 0 to the length of that string-1. Each time, it uses list indexing to swap to the correct spacing method and number range used to slice our new string.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 12 bytes
```
D€Ẏ;\z⁶U$⁸¡Y
```
[Try it online!](https://tio.run/##ASAA3/9qZWxsef//ROKCrOG6jjtceuKBtlUk4oG4wqFZ////OA "Jelly – Try It Online")
[Answer]
# [05AB1E (legacy)](https://github.com/Adriandmen/05AB1E/tree/fb4a2ce2bce6660e1a680a74dd61b72c945e6c3b), ~~14~~ ~~12~~ 10 bytes
Using the legacy verion as the rewrite is extremely slow on this for some reason. 
Saved 2 bytes thanks to *Kevin Cruijssen*
```
LSηsFRζ}J»
```
[Try it online!](https://tio.run/##MzBNTDJM/f/fJ/jc9mK3oHPbar0O7f7/39AUAA "05AB1E (legacy) – Try It Online")
**Explanation**
```
L           # push range [1 ... input]
 S          # split to list of individual digits
  η         # calculate prefixes
   sF  }    # input times do:
     R      # reverse list
      ζ     # and transpose it
        J   # join to list of strings
         »  # and join on newlines
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 10 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
Ç√çZ╟84Γó║
```
[Run and debug it](https://staxlang.xyz/#p=80fb875ac73834e2a2ba&i=1%0A2%0A5%0A6%0A7%0A8%0A13&a=1&m=2)
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), ~~105~~ ~~101~~ 95 bytes
*-4 bytes thanks Arnauld for the [trick with Sort](https://codegolf.stackexchange.com/a/181889/80745).*
```
param($n)($x=1..$n-join'')|% t*y|%{($s+="$_")}|sort -d:($n%4-in1,2)|% *ft($x.Length*($n%4-ge2))
```
[Try it online!](https://tio.run/##JY5NCoMwFIT3OUUILzXP1kCC3QjeoHcQwfhTNBENtEU9e5rW2c58MzO7l1nW3oxjgJaWdAtzvdSTAIsC3qWSEmz2dINNEtw59eln55uA9VoyqBge@@oWT7OmiATPs8Gqm/4F09ZHXj6M7XyfnmZnNGI4CFFaSnWPRYRGxSIqJtfQPO6DgIrniAX7e5d4CqozxsgRvg "PowerShell – Try It Online")
Less golfed:
```
param($n)
$x=1..$n-join''
$x|% toCharArray |% {
    ($s+="$_")
} | sort -Descending:($n%4-in1,2) |% PadLeft ($x.Length*($n%4-ge2))
```
[Answer]
# [R](https://www.r-project.org/), ~~175~~ ~~172~~ 154 bytes
```
function(n)write(c(" ",0:9)[1+(x=utf8ToInt(Reduce(paste0,1:n,""))-47)*!upper.tri(diag(y<-sum(x|1)))["if"(n%%4>1,1:y,y:1),"if"(!n%%4%%3,y:1,1:y)]],1,y,,"")
```
[Try it online!](https://tio.run/##VYyxDoIwFEV3vwKaEN7TYmhEQUR3V@OmDARa08FCShsh8d@rOMl4zs252gmviJywqjayVaDwpaXhUAPxCI3zPd7YCoajNSK7tmdl4MIbW3Poqt7wmLJcUUIQoyTFpW@7juu10RIaWT1gLKLePmF4M0S8ESkIqCBITuybjXTMGdKf9CcbBJtJTROWJWV0pNOzE7DFQ10ZCO8qxIWA3RzTOWb/6D4 "R – Try It Online")
A horrible in-line mess!
*-3 bytes by changing the rotation condition*
*-17 bytes thanks to [digEmAll](https://codegolf.stackexchange.com/users/41725/digemall)'s suggestion, and another byte after golfing that further*
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 137 bytes
```
(t=Table[Table[" ",If[(m=#~Mod~4)>1,Tr[1^s]-k,0]]<>ToString/@s[[;;k]],{k,Length[s=Join@@IntegerDigits/@Range@#]}];If[0<m<3,Reverse@t,t])&
```
[Try it online!](https://tio.run/##JcxBC4IwFADgvyIKobBQsZtOduhiFIR5Gy9Y9ZrDnOAeXSL/@hK6fMdvVNTjqMjcldfcx8Q7dXuh/BsGIWueMh55tJymx7JL6px1s8yvDrYDywCqupsuNBurU@GkLMsBgH0GdkSrqZeOHyZjhWgsocZ5b7Qhl4pWWY0igi@Ua59VY1WwFt84OxTECJKNP68nBakItMwL8P4H "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 116 bytes
```
n=input()
s=''.join(map(str,range(1,n+1)));L=len(s)
p=-~n/2%2;i=~-L*p+1
exec'print s[:i].rjust(n/2%2*L);i+=1-2*p;'*L
```
[Try it online!](https://tio.run/##HU6xjsIwFJt5X9HllKSlnBIEh6gywlTpltsQQylPNMC9Ri@pDhZ@vZeyWLZl2fbP2PVkxrY/oxVCjGQd@SFKBSHpxbV3JH8bL0PkOTd0QannVGilVFXbO5IMCrwtX/RpPkzl7Kusc19owAe2wrOjmIXD1h0XfB1ClO9YXqvKFVaXJveVyOsxDcNf5@6Y/fCAW5hFfiacTSXZdC3xdxckq0Ufs933fsfc85Q6MTa3US9hBWv4gg1oMP8 "Python 2 – Try It Online")
[Answer]
# [Red](http://www.red-lang.org), 155 bytes
```
func[n][b: copy""repeat i n[append b i]repeat i l:
length? b[t:[l - i + 1]set[k m]pick[i t[l t][l i]]n % 4 + 1
print pad/left copy/part b do do m do do k]]
```
[Try it online!](https://tio.run/##PY3NCsMgEITvPsUS6KmUkP7jpe/Qq@zBxLWVGCN2e@jTW9MfYdj5mB2YRCZfySgUVmb7DIMKqHoJwxxfTZMokmZwEJSOkYKBHhzW1EvhKdz4foFesVQeNiVdQ4cPYjXChNENo3LA5cVYjkMMsIL9UhIxucAQtWk9Wf5MtlEnLiNmXjT9fETM37KFbif@eKh0rHSqdK7UVdrmNw "Red – Try It Online")
[Answer]
# perl 5, 117 bytes
```
$p=$_++&2?'/ ':'$/';$s='(.*\d.\n)';$r=$_--&2?$s.'\K$':"^(?=$s)";$_=(join"",1..$_).$/;1while s,$r,'$1=~s/\d'."$p/r",ee
```
[TIO](https://tio.run/##DcrRCoIwFAbge59Cxk9Hc55hZIFj@AC9wsgbBymiYwu669Fbu/zg8y5sfUrwBlPTnC4jqZIGgiKNaKjis53Z7nVmyKVtc0Fksg/QIJ7VaBBroTGZaj2WXQjZMWOqGUp3n9eyuTJKBEnozDcqOxMLeBWEdC6la9EXt@L@O/x7OfaYWr/9AQ)
[Answer]
# [PHP](https://php.net/), ~~116~~ ~~111~~ 109 bytes
```
for($m=$l=strlen($s=join(range(1,$n=$argn)));$l--;)printf('%'.($n&2?$m:-$l).'.'.($n-1&2?$m-$l:$l+1)."s
",$s);
```
[Try it online!](https://tio.run/##NY5Pa8IwGMbv@RQP5Z1N2NtCHNvELHgSPAh6lzGKRKvENCS9jX32rlS9/p6/sY3D1yq2UQjqXe4zLA7Qb4x3xgfjk7FgaMYc30aIU5dcc2wh8XA3GdSkc4DCrwDcse1QhLGk4LvA2G/2P@vd1gxjWNLNkre5T94FSdleu0uQqQlnJzVTsFNGKWXIV5VRMV1Cf5LlS1lLCrP5im7Liryqy3oilZ7YiJbkX7WqiywKpqzM8DjzXBd/wz8 "PHP – Try It Online")
Run with `php -nF` input from `STDIN`.
`$ echo 6|php -nF t.php`
```
123456
 12345
  1234
   123
    12
     1
```
[Answer]
# [Java (JDK)](http://jdk.java.net/), ~~247~~ ~~209~~ ~~188~~ ~~186~~ ~~160~~ 148 bytes
```
i->{String s="";int l=0,w;for(;l<i;s+=++l);for(w=s.length(),l=0;l<w;)System.out.printf("%"+(1>i%4/2?1:w)+"s\n",s.substring(0,1>~-i%4/2?w-l++:++l));}
```
[Try it online!](https://tio.run/##XZBNT8IwGMfvfIqlCUmbdpXha1aHMcaDB08ckUMtHRS7DtpnLGSZX31WiJp4/ef3f3merTzIdLv6GIwD7UupdPIqjetGASQYlUR5sUxAB1Ay6FB01@yG3bI7lrEpyy578QMearNKqmjFc/DGrRdLT7ptTOcNGMvLxikwteMvDp5qF5pK@2RfDCaddWdDEgqEROxLbDFhrShrj4W9NyLQglJLTkJbBG61W8MGExa5CLSCzI8BdMXrBvguRkGJ0RhRnM3M@Opi@pDlLaEovDnEAg/Nezj14QnLZp/pGWlTS2n@XUNEP4jR3/BH7@Ux2sBrWeHfRxAe5zxLtcGnC/4NsA6jWEcNRTkiYs@lUnoH2MR0IkZ9P3wB "Java (JDK) – Try It Online")
`-38 bytes` thanks to @KevinCruijssen  
`-21 bytes` by letting `printf` handle the padding.  
`-2 bytes` by doing substring before replace, allowing us to increment `l` in one location rather than two.  
`-26 bytes` - with `printf` doing the padding, the string full of spaces was no longer necessary, and the digit strings could be generated in a shorter way apparently.  
`-12 bytes` by not messing about with single digits instead of printing substrings of the perfectly servicable digit string we already have.
# Ungolfed
```
input->{
    // Lambda expression with target type of IntConsumer
    String allDigits = "";
    int lineLength, line = 0;
    // Collect a list of all digits in order.
    for (;line < input; allDigits += ++line) {}
    // Save the max length of a line, and create a string of that many spaces.
    for (lineLength=allDigits.length(), line=0; line < lineLength;) {
        System.out.printf(   "%" // construct a format string to handle the padding
                           + (   1 > input%4/2
                               ? 1 // No padding
                               : w // Yes padding
                             )
                           + "s\n"
                         , allDigits.substring( 0
                                              ,   1 > (i-1)%4/2
                                                ? w - l++
                                                : ++l
                                              ) // A string containing only the digit we want.
                         );
    }
}
```
]