# Interleave numbers from 1 to n, with the same numbers reversed

### A simple one:

Take a positive integer n less than 1000, and output the integers from 1 to n interleaved with the integers from n to 1. You must concatenate the numbers so that they appear without any delimiters between them.

### Test cases:

n = 1
11

n = 4
14233241

n = 26
12622532442352262172081991810171116121513141413151216111710189198207216225234243252261

n = 100
110029939849759669579489399210911190128913881487158616851784188319822081218022792378247725762675277428732972307131703269336834673566366537643863396240614160425943584457455646554754485349525051515052495348544755465645574458435942604161406239633864376536663567346833693270317130722973287427752676257724782379228021812082198318841785168615871488138912901191109299389479569659749839921001


This is so the shortest submission in bytes in each language wins. Explanations are encouraged.

# Mathematica, 36 32 bytes

Row@Riffle[x=Range@#,Reverse@x]&


thanks Martin

• #+1-x is more efficient than Reverse@x. ------- Note: Row does not even make it a string, just a formatting. It looks identical to a string on some platforms, but it show as multiplication sign between on Wolfram sandbox. However it takes less bytes than ToString/@(""<>...) or ""<>ToString/@.... – user202729 Jun 10 '17 at 15:32

# CJam, 13 12 bytes

1 byte removed thanks to Martin Ender

ri:X{_)X@-}%


Try it online!

### Explanation

ri               e# Read integer n
:X             e# Copy n into variable X
{     }%    e# Map over the array [0 1 2 ... n-1]
_          e# Duplicate
)         e# Add 1
X        e# Push n
@       e# Rotate
-      e# Subtract


## Perl 5, 28 bytes

27 bytes code + 1 byte for -a.

map{print$_,"@F"+1-$_}1..$_  Try it online! # Braingolf v0.7, 18 bytes [non-competing] VR.MUvU&,R{vMR}>&_  Try it online! ### Explanation: VR.MUvU&,R{vMR}>&_ Implicit input of n to stack VR Create new stack then return to main .M Duplicate n and move duplicate to new stack U Replace stack with 1-n, where n is last item on stack vU Replace 2nd stack with 1-n &, Reverse second stack R Return to main { } Map loop, runs for each item in the stack vMR Move to next stack, move last item to main stack > Cleanup after loop &_ Print all items in stack with no delimiter  • Did you just add the entire interpreter in the header in TIO? I guess that's why it's non-competing? – Stewie Griffin May 30 '17 at 19:02 • Yes I did, the language isn't on TIO yet, but that doesn't make it non-competing, it's nc because braingolf v0.7, which adds the {} loop, was released after the challenge was posted. – Skidsdev May 30 '17 at 21:52 # Jelly, 6 bytes RżU$FV


Try it online!

Explanation:

         Example: n = 6
R        Create a range from 1 to n             [1, 2, 3, 4, 5, 6]
U$and a reversed copy of that same range [6, 5, 4, 3, 2, 1] ż and interleave them [ [1, 6], [2, 5, ... ] F Flatten the list [1, 6, 2, 5, 3, 4 ...] V And Eval: Jelly code consisting of only numbers would simply print those numbers,  # Retina, 20 bytes .+$*__
\B
$.$.'
_



Try it online! Includes test cases. Explanation: Each pair of numbers sums to n+1, so we convert n to unary and add 1. Then, we match between each pair of _s, counting the number of _s to the left and right. This generates the pairs of numbers. Finally we delete the _s now that they've served their purpose.

## QBIC, 18 bytes

[:|Z=Z+!a$+!b-a+1$


Explanation

[:|       FOR a = 1 to n
Z=Z+      Add to Z$!b$+      a cast of the loop counter as string
!b-a+1$and a cast of (n+1) minus the loop counter to string Z$ is printed implicitly at the end of QBIC


# Perl 5, 25 21 + 1 = 26 22 bytes

Takes input from stdin, without a trailing newline.

Runs with the -n flag:

print++$x.$_--while$_  4 bytes saved thanks to Dada. • It doesn't work for numbers greater than 9. That's because the precedence of . is higher than the one of +. Adding parenthesis around $_+1 would solve this. Also, you can remove the parenthesis around $_--. But even shorter, you can take the input without final newline (with echo -n for instance), and then you can do print++$x.$_--while$_. – Dada Jun 1 '17 at 8:01

# ><>, 16 12+3 = 19 15 bytes

l:n}:n1-:?!;


Input is expected on the stack, so +3 bytes for the -v flag.

Thanks to @TealPelican for pointing out a very clever way to save 4 bytes by using the size of the stack itself - on the first iteration it'll be 1, then 2, then 3... That way, the first number in each pair manages itself, no manual incrementing required!

Previous version:

1:n1+$:n1-:?!;$!


Try it online!

• Surprisingly I came up with something fairly similar before checking the answers for ><> but you can save a few bytes by replacing the; 1+$with } and then removing the final;$! at the end to come up with this; l:n}:n1-:?!; – Teal pelican Jun 2 '17 at 15:25
• ln:n1-::?!; – Jo King Nov 6 '18 at 21:40

# Powershell, 37 bytes

param($n)-join(1..$n|%{$_,($n-_+1)})  # C (gcc), 43 bytes a;f(n){for(a=1;n;a++)printf("%d%d",a,n--);}  Try it online! • You can save a byte by putting the ++ on the a in the printf. – Ørjan Johansen Jun 10 '17 at 15:00 # Python 3, 51 bytes lambda n:''.join("%d%d"%(x+1,n-x)for x in range(n))  Try it online! Port of Mego's Python 2 answer. # C# (.NET Core), 53 bytes a=>{for(int i=0;++i<=a;)Console.Write(i+""+(a-i+1));}  Try it online! Uses an Action delegate to pull in the input and not require a return. Ungolfed: a => { for(int i = 0; ++i <= a;) // initialize i and increment until a Console.Write(i + "" + (a - i + 1)); // output i and "inverse" of i // empty string required to set as string // parentheses required because C# is dumb and can't figure out what a minus sign is supposed to do without them }  # Japt -mP, 8 6 bytes °Us+N´  Try it  :For each U in the range [0,input) °U : Prefix increment U s : Convert to a string + : Append N´ : Postfix decrement the (singleton) array of inputs :Implicitly join and output  # Japt-P, 11 8 bytes Saved 2 byte thanks to @Shaggy õ í1õU)c  Try it online! ## Explanation: õ í1õU)c õ Range [1...Input] í Pair with: 1õU Range [Input...1] )c Flatten -P Join into a string  • Use the -P flag to save a byte. Link – Shaggy Jun 6 '17 at 16:11 • @Shaggy Thanks! When I wrote it, I tried this and assumed it wasn't joining due to a bug. – Oliver Jun 6 '17 at 16:26 • 8 bytes – Shaggy Nov 6 '18 at 17:48 # Forth (gforth), 41 bytes : f 1+ dup 1 do i 1 .r 1- dup 1 .r loop ;  Try it online! ### Explanation Loops from 1 to n and outputs the index as well as n-index in a right-aligned space of size 1 (forces no space after output) ### Code Explanation : f \ start new word definition 1+ dup 1 \ set up arguments to loop from 1 to n do \ start loop i 1 .r \ output the index in a right-aligned space of size 1 1- dup \ subtract 1 from current value of n and duplicate 1 .r \ output new n in right-aligned space of size 1 loop \ end loop ; \ end word definition  # Kotlin, 52 50 bytes fun g(e:Int)=(1..e).forEach{print("it${e-it+1}")}  Try it online Older version, the one above also prints it, the one below produces a string. fun y(n:Int)=(1..n).joinToString(""){"$it${n-it+1}"}  Try it online! # APL (Dyalog Unicode), 10 bytesSBCS (∊⍪,⌽)⍕¨∘⍳  Try it online! Explanation: (∊⍪,⌽)⍕¨∘⍳ ⍝ Monadic function train ⍕¨∘⍳ ⍝ Generate integers from 1 to input, convert each to string ⍝ Call this vector "X" ⍪ ⍝ "Table" X, making a single-column matrix ,⌽ ⍝ Reverse X and concatenate it with the above table ⍝ This results in a two-column matrix with integers from ⍝ 1 to N and N to 1 side-by-side ∊ ⍝ "Enlist" the above - left-to-right, top-to-bottom, ⍝ recursively concatenate all items in the matrix  • Don't forget to claim your bounty! – Adám Mar 9 at 21:05 • here's a shorter one: ∊⍕¨⌽,⌸⌽⍳⎕ – ngn Mar 14 at 12:00 ## Clojure, 52 bytes #(apply str(mapcat(fn[i][(inc i)(- % i)])(range %)))  Concatenates pair-wise integers (head and tail of the seq). # AWK, 34 30 bytes {for(i=1;i<=$1;i++)printf i$1-i+1} {for(;i<$1;i++)printf i+1$1-i}  Try it online! • could shave 4 with {for(;i<$1;i++)printf i+1$1-i} – marcosm May 30 '17 at 13:50 Python 2.7, 39 bytes x=''.join(i+1+n-ifor i in range(n))  • I think you must either use input or make this a lambda. Assuming the variable exists in the workspace is not acceptable I'm afraid. – Stewie Griffin May 30 '17 at 15:45 ## Pyke, 9 bytes SD_],sms  Try it here! ## R, 4339 37 bytes This is an anonymous function. function(n)cat(rbind(1:n,n:1),sep="")  It's 35 bytes if I use pryr::f() like in the other answer: pryr::f(cat(rbind(1:n,n:1),sep=""))  but needs to be installed (Enter install.packages("pryr") in the R console). • You could save some bytes by not defining s and just using 1:n directly. – user2390246 May 30 '17 at 19:54 • you can also save a couple bytes by removing the curly braces from your first answer – Giuseppe May 30 '17 at 19:58 ## Python 2, 67 bytes A simple one, just for the collection :) n=input()+1 i=1 s="" while i<n: s+=i+n-i i+=1 print s  • you can use while i<n:s+=i+n-i;i+=1 to golf off some bytes – Leaky Nun Jun 10 '17 at 14:48 # Hexagony, 15 bytes {?\"!\{$.(.@).!


Embiggened:

   { ? \
" ! \ {
$. ( . @ ) . ! . . . .  Try it online! ## Mathematica, 52 bytes As a traditional function: f[x_] := StringJoin[ToString /@ Riffle[Range[x], Reverse[Range[x]]]]  As an anonymous function (x = Range[#]; StringJoin[ToString /@ Riffle[x, Reverse[x]]]) &  As a code block that accepts the input in the variable n x = Range[n]; StringJoin[ToString /@ Riffle[x, Reverse[x]]]  The code is pretty simple -- generate the list of integers with Range[], interleave this list (using Riffle[]) with a copy of the list that has been reversed, then convert all the integers to strings and concatenate them with StringJoin[]. Could have done it in 30 bytes if we were allowed to leave the answer in the form of a list (thus, {1,4,2,3,3,2,4,1} rather than "14233241"). Also, in the hypothetical Mthmtca with short command names it would have been something like 21 bytes. Performance is tolerable for n <= 1,000,000. I didn't test past that. • This should be a function that takes an integer argument, rather than having a 4 hardcoded. – Greg Martin May 30 '17 at 21:38 • This isn't golfed either, which makes it invalid – cat Jun 1 '17 at 1:34 • @cat if you think you can write it more concisely, please do so. If you can't, why do you think it's not golfed? – Michael Stern Jun 1 '17 at 3:29 • Yeah, f[x_]:=StringJoin[ToString/@Riffle[Range[x],Reverse[Range[x]]]], I think I counted the close braces right from my phone. Either way I know for a fact that MMA doesn't rely on whitespace, and I can say that as a communiry member – cat Jun 1 '17 at 10:53 • None of these are 52 bytes, even with whitespace stripped. I get 56 bytes for the middle one (the last one not being valid). – Ørjan Johansen Jun 1 '17 at 17:05 ## LOGO, 48 bytes to f :n for[i 1 :n][(type :i :n+1-:i)]show " end  Define a function f that when invoke with parameter = number n, print the result string. # braingasm, 9 bytes ;[>+:<:-]  Simple: Read a number from stdin to the current cell; While the current cell is not zero, go to next cell (initially 0), increase it, output its value, go back, output that value and decrease it. # Kotlin, 44 bytes {n->(1..n).joinToString(""){"$it${n-it+1}"}} {n-> // n is Int input (1..n) // range from 1 to end .joinToString("") // join items with no separator { // transform by "$it\${n-it+1}"    // current number and number from end of range
}}


Lambda that takes an Int and returns a String.

Try it online!

• 42 (btw it's basically identical to the other kotlin answer) – ASCII-only Mar 15 at 7:16

# dc, 17 bytes

[znddn1-d0<M]dsMx


Try it online!