# 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

:X             e# Copy n into variable X
{     }%    e# Map over the array [0 1 2 ... n-1]
_          e# Duplicate
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

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 '19 at 7:16

# dc, 17 bytes

[znddn1-d0<M]dsMx

Try it online!