# Concatenating n with n + 1

## Introduction

OEIS sequence A127421 is the sequence of numbers whose decimal expansion is a concatenation of 2 consecutive increasing non-negative numbers. Put simply, every number in the sequence is formed by putting together n with n+1 for some non-negative, integer value of n. The first several terms are:

1, 12, 23, 34, 45, 56, 67, 78, 89, 910, 1011, 1112, 1213, 1314, 1415, 1516, 1617, 1718, 1819, 1920, 2021, 2122, 2223, 2324, 2425, 2526, 2627, 2728, 2829, 2930, 3031, 3132, 3233, 3334, 3435, 3536, 3637, 3738, 3839, 3940, 4041, 4142, 4243, 4344, 4445, 4546, …

## Challenge

Given a single positive integer n, print the first n entries of OEIS sequence A127421 in increasing order.

• Input and output can be in any acceptable format. Strings or numbers are fine for output.
• Leading zeroes are not permitted.
• Either a full program or function is permitted.
• For the purposes of this challenge, n will be positive and under 100.
• Standard loopholes are disallowed by default.
• This question is code golf, so lowest byte-count wins.
• Here is some sample input and output:

1 => 1
2 => 1, 12
3 => 1, 12, 23
10 => 1, 12, 23, 34, 45, 56, 67, 78, 89, 910


If you have any questions, don't hesitate to ask. Good luck.

P.S this is my first challenge, so hopefully this all makes sense.

EDIT: Removed output restriction to allow numbers or strings.

• Can it be 0 indexed? – Jo King Jul 4 '18 at 1:09
• No-one's said it yet, but welcome to PPCG! Nice first question, not too hard, yet not completely trivial either, and there's a number of different approaches – Jo King Jul 4 '18 at 1:40
• After 7 days, I will accept the shortest answer which meets all these criteria. Why is there a need for the challenge to end? – Erik the Outgolfer Jul 4 '18 at 9:13
• Nowadays we tend to not accept an answer, because it discourages further posting of answers. I suppose you take old challenges as a model (which is also discouraged) See things-to-avoid-when-writing-challenges – user202729 Jul 4 '18 at 9:21
• @Dennis Ok, i'll remove the date from the challenge; maybe I'll accept when no more new answers are coming. – Amphibological Jul 4 '18 at 13:41

# Husk, 11 9 bytes

-2 thanks to @BMO!

mSöd+d←dḣ


Try it online!

## Explanation

m          map(                                              )
S                                               <*>
ö            (           .).    .         .
d            fromDecimal
+                         (++)
d                             toDecimal
←                                      (+1)
d                                            toDecimal
.
ḣ                                                      (\n->[1..n])

mSöd+d←dḣ  map((fromDecimal.).(++).toDecimal.(+1)<*>toDecimal).(\n->[1..n])


# Python 3, 55 48 47 43 bytes

f=lambda n:n-1and f(n-1)+[f"{n-1}{n}"]or[1]


Try it online!

Recursive function that takes an integer and returns a mixed list of strings and numbers.

# Python 3, 44 bytes

lambda n:[f"{j or''}{j+1}"for j in range(n)]


Try it online!

• Welcome to the site! You can save bytes by removing the space between your string and the for. Also since this is an anonymous function there is no need for f=, so you can save 2 bytes by removing it. – Wheat Wizard Jul 9 '18 at 4:28
• @CatWizard Thanks! But what do you mean by anonymous function? is there a way to call an anonymous function? – JathOsh Jul 9 '18 at 5:08
• An anonymous function is any function that is not associated with a name. In python the lambda keyword makes an anonymous function. Anonymous functions can be set to variables as you did, they can also be called directly without assignment (that's what makes them anonymous) by wrapping them in parentheses e.g. (lambda x:x+1)(6). For TIO you can put f=\  in the "header" section and call the function with f. – Wheat Wizard Jul 9 '18 at 5:12
• @CatWizard Thanks that is super helpful – JathOsh Jul 9 '18 at 5:29
• How about having the condition for 1 inside the format string? 44 bytes – Jo King Jul 9 '18 at 6:28

# cQuents, 11 bytes

=1::($-1)~$


Try it online!

Way too long for a language that this is supposed to be easy in, guess I should have implemented unary subtract-one and add-one operators.

# Explanation

=1            First term in sequence is 1
::          Mode: for input n, output first n terms in sequence
For each term in the sequence:
($-1) Subtract 1 from the current index ~$    Concatenate that to the current index

• Note current version uses & instead of ::, saving 1 byte – Stephen Feb 1 at 4:53

# APL (Dyalog Classic), 13 bytes

2{⍎∊⍕¨⍺⍵}/0,⍳


Try it online!

# Canvas, 6 bytes

ŗ²；＋┤］


Try it here!

Explanation:

{     ]  map over 1..input
ŗ         convert to string (required as ＋ needs 1 arg to be a string to not add)
²;       place a 0-indexed version of the index below TOS
+      join the two
┤     cast to number


ŗ²×┤］ (or even ²×┤］) would work if I didn't try to push the absolute most out of characters and didn't make × - reverse add - do not that when not receiving 2 strings.

# Red, 56 bytes

func[n][prin"1 "repeat i n - 1[prin rejoin[i i + 1" "]]]


Try it online!

# Rust, 56 bytes

|n|(0..n).map(|n|format!("{}{}",n,n+1).parse().unwrap())


Try it online!

# Java, 48 bytes

String f(int n){return--n>0?f(n)+","+n+-~n:"1";}


Port of tsh's JavaScript answer. Try it online here.

Ungolfed:

String f(int n) { // recursive function taking an integer as argument and returning a String
return --n > 0 ? f(n) // decrement n and recurse if n is still positive after
+ "," + n // return the result of the recursive call concatenated with n and ...
+ (- ~n) // ... n+1; writing it as -~n gives it precedence over the concatenation
: "1"; // if n is now 0 on the other hand, return "1"
}


# MATL, 9 bytes

:"@qV@VhU


Try it online!

              % implicit input n
:             % range, push 1..n
"             % for loop:
@            % push for loop index
q            % decrement
V            % convert to string (num2str)
@            % push for loop index
V            % convert to string (num2str)
h            % horizontally concatenate
U            % convert to number (str2num)
% implicit end of for loop
% implicit end of program, display stack contents


# Panacea, 6 bytes

re
D>j


Explanation:

r        Range [0.. input]
e       Map each element with the following line
D        Duplicate
>       Increment
j      Join digits; multiply by ten and add
Since this works with integers, it removes the preceding 0.


# Japt, 5 bytes

õÈsiY


Try it

## Explanation

          :Implicit input of integer U
õ         :Range [1,U]
È        :Map each integer at index Y
s       :  Convert to string
iY     :  Prepend index
:  Implicitly convert back to integer
:Implicit output of resulting array


## Alternative

Ç°s+Z


Try it

          :Implicit input of integer U
Ç         :Map each integer Z in the range [0,U)
°        :  Postfix increment
s       :  Convert to string
+Z     :  Append (the now incremented) Z
:  Implicitly convert back to integer
:Implicit output of resulting array


# J, 20 bytes

".(,&":)/@(,>:)"0@i.


Try it online!

# Forth (gforth), 33 bytes

: f dup 1 ?do i . i 1 .r loop . ;


Try it online!

### Explanation

Loops through all numbers in range and outputs the number twice, the first with a space appended, and the second without.

This results in the first output being appended to the previous output. To fix the last case, the input is outputted so that it can be appended to the last number in the loop.

### Code Explanation

: f                   \ start a new word definition
dup 1               \ duplicate the input and put a 1 on the stack
?do                 \ begin counted loop from 1 to n (does nothing if 1 == n)
i .               \ output loop index with a space appended
i 1 .r            \ output loop index right-aligned with minimum 1 character length
loop                \ end the loop
.                   \ output the input we duplicated earlier
;                     \ end the word definition


# Perl 5, 24 21 bytes

@DomHastings came up with a way to save 3 bytes

say$i++.$i|0for 1..<>


Try it online!

• Nice! Much shorter than my first approach using a while and  $\ ! Using yours as a base though, you can save a couple of bytes using eval and x<>: Try it online! or even Try it online! – Dom Hastings Jul 4 '18 at 11:44 • Wait, combining the two gives 21: Try it online! – Dom Hastings Jul 4 '18 at 11:46 # Excel VBA, 29 bytes An anonymous VBE immediate window function that takes input from cell [A1] and outputs to the console. ?1:For i=2To[A1]:?i-1 &i:Next  • I've always wanted to use Excel VBA for one of these. I have SO much to learn! – seadoggie01 Jul 6 '18 at 15:16 • @seadoggie01 Its a great way to get more familiar with the language - and fortunately, VBA has a very well made up Tips Page that can help you to get started :) – Taylor Scott Jul 6 '18 at 16:17 • @seadoggie01 oh, and if you are interested in VBA answers, I have a couple that I am really quite proud of, such as Let's Draw Mona Lisa, Embiggen your Input, and Visualize Visual Eyes – Taylor Scott Jul 6 '18 at 16:20 • Ha! The first thing I found after this question was the Tips page :) – seadoggie01 Jul 6 '18 at 16:29 # Yabasic, 37 bytes An anonymous function that takes input from STDIN and outputs to STDOUT. Input""n ?1 For i=2TO n?i-1,"",i Next  Try it online! -14 bytes thanks to @ErikF • You can get it down to 31 bytes by not counting the input assignment and making use of , between output fields in PRINT to combine them: Try it online! – ErikF Jul 6 '18 at 8:42 • @ErikF, input must be taken explicitly per community rules and cannot be taken from a predefined variable. That said I had never seen the ?n,"",m trick - that is really quite clever. – Taylor Scott Jul 6 '18 at 10:56 # dc, 34 bytes sj1psi[lid1+dsiZAr^*li+pzlj>M]dsMx  Try it online! Completely different (and less golfy, unfortunately) approach than Sophia's dc answer. I juggle a lot of register activity here that I wish I could cut down, seems the likeliest way to golf a couple of bytes. Expects input as the sole stack entry. sj1psi stores the input into register j, prints the initial 1, and stores 1 in register i. Macro M duplicates i, increments it, and then multiplies it by 10 to the power of however many digits the incremented value has (ZAr^*). Fetches the newly incremented version, and adds the two together. Compares register j to the number of entries on the stack, and runs until they match. # dc, 29 bytes [d1-d0<F]dsFx+p[npz1<G]sGz1<G  Try it online! This is a good showing for dc because its printing capabilities are so limited, but line up perfectly with the challenge spec. Input is from the stack (and must be the only thing on the stack), output is to stdout. There's some duplication near the end I can't figure out how to get rid of without breaking the case n=1 # Explanation F will be the macro [d1-d0<F] d copy the top of the stack 1- decrement d0<F repeat (tail recursion) until a 0 is on top G will be the macro [npz1<G] n print the top (pop, but no newline) p print the new top (newline, but no pop) z1<G repeat (tail recursion) until only one number is on the stack Full program (example input: 4) [d1-d0<F]dsFx Store F and run it: new stack is 0 1 2 3 4 + Drop the zero (can't just recurse to 1, or n=1 breaks) p Print (no pop) the '1': stack is 1 2 3 4 [npz1<G]sG Store G z1<G Run G if there's more than one number on the stack  # Momema, 36 bytes z0w+1=*0-8*0w00+1*0-8*0-9 10z=+_M-*0  Try it online! Requires the -i interpreter flag. ## Explanation  # i = 0 d 0 # d0: nop # do { i (+ 1 =*0) # i0: jump to i(![0]) # if i { -8 *0 # print [0] # print i i 0 # i1: nop # } 0 (+ 1 *0) # [0] = [0] + 1 # i += 1 -8 *0 # print [0] # print i -9 10 # print chr(10) # print \n d =(+ _M -*0) # d1: jump to d(!!(input - [0])) # } while (input - i != 0)  The Momema reference interpreter provides an "interactive mode", enabled by -i, which is intended to be used on the command line. One of the features it allows is the ability to add holes, denoted by _, which effectively allow one to substitute an integer read from STDIN into an expression. This is already shorter than *-8 (read from memory-mapped I/O location -8), which does the same thing without interactive mode and without displaying a prompt to STDERR. Crucially, holes can also be named with a sequence of capital letters after the _. Evaluation of named holes is memoized. Input will be read the first time a named hole is evaluated but subsequent evaluations of a hole with the same name will reuse the input number. This means that we can use _M to stand in for "the input", but input will only be read on the first iteration of the loop. # yup, 48 bytes 0eee0ee-0ee-*:0e#[:@]0e-{]:[:]-:#0e0~--#]:@]0e-}  Try it online! ## Explanation yup only knows of a few commands. To modify data, I'll be using 0 (nilad pushing that number), e (natural exponentiation), and - (subtraction). For example, the snippet 0e is equal to 1, since $e^0=1$. This program is divided into two parts: initialization and iteration. ### Initialization 0eee0ee-0ee-*:0e#[:@]0e-  0eee pushes $e^e\approx15.1543$ and 0ee pushes $e\approx2.7183$. Thus, the expression 0eee0ee-0ee- pushes: $$e^e-e-e\approx9.7177$$ This encodes 0x10 (the linefeed), since numbers are rounded to the nearest integer before being outputted. This is our numeric separator. The next part is to initialize the stack with data. *: pushes the input twice, and 0e# outputs a 1. [:@] outputs a linefeed without popping it from the stack, and 0e- decrements the input. This handles the edge case of outputting 1 instead of 01. Rather than handle this with a conditional, hardcoding the first entry is shorter. ## Iteration {]:[:]-:#0e0~--#]:@]0e-}  {...} loops while the TOS is defined and positive. In this case, it will stop when it hits zero. Each iteration starts off with a stack like this: [9.7177, input, iterator]  iterator starts at input - 1. ]:[:]- calculates input - iterator and gives us our true iterator value. Then, :#0e0~--# first outputs the true iterator then the same number plus 1, where 0e0~-- encodes $-(-n-e^0)=-(-n-1)=n+1$. Then, ]:@] restores the stack to its initial shape and 0e- subtracts 1 from the iterator, continuing our loop. This repeats until iterator reaches 0, at which point the loop stops and the program terminates. # D, 93 bytes import std.math;_[]f(_)(_ n){_[]k;foreach(i;0..n)k~=++i+10^^(cast(_)i--.log10+1)*i;return k;}  Try it online! I couldn't find a nice (short) solution that evades an import, and this was the shortest of the bunch. • You can save two bytes by making auto  be _[] – Zacharý Jul 8 '18 at 16:15 • @Zacharý Good point! – Conor O'Brien Jul 8 '18 at 17:21 • WOuldn't it just be easier to do std.conv trickery? – Zacharý Jul 11 '18 at 2:04 ## MATLAB 45 63 bytes @(j)eval('for a=0:j-1;disp([num2str(a)*a/a,num2str(a+1)]);end')  Somewhat ugly *a/a in order to avoid printing leading zero for the first element Fixed by wrapping into disp and anonymous function • You can't assume the input is predefined as a variable unfortunately. You have to do j=input(''); or create a function. Anyway, +1, as I'm assuming you'll fix it. Anonymous function with eval is often shortest when loops are involved. – Stewie Griffin Jul 8 '18 at 17:47 • @StewieGriffin thanks for advice, i am still learning ways around golfing – Oct18 is day of silence on SE Jul 8 '18 at 18:43 • @StewieGriffin by anonymous function + eval you mean something like f = @(j) eval('for ..... end') ? – Oct18 is day of silence on SE Jul 8 '18 at 18:53 • Yes :-) you don't need to include f= in the byte count, since the function can be used unnamed like: ans(6). – Stewie Griffin Jul 8 '18 at 19:57 # DC, 33 bytes si[lidZAr^spd1-dsilp*+li0<d]dsdxf  Explanation si[lidZAr^spd1-dsilp*+li0<d]dsdxf Whole program. si Save the value on the stack to the i register. [ ]dsdx Create a macro, duplicate it, store it in the d register, and execute it. li Put the value from the i register on the stack. dZ Duplicate the top value, and change it to the number of digits it has. Ar Push 10 on the stack, and reverse the top two values. ^sp Do 10^x, and store it in the p register. d1- Duplicate the top value, and subtract 1 from it. dsi Duplicate the top value, and store it in the i register. lp*+ Put the value from the p register on the stack, and multiply it by the the value we just put in the i register. Then, add the top two values together. li0<d Put the value from the i register and then 0 on the stack. If 0 < li, then we run the d macro again. f Print the stack.  Input is the number of numbers to be generated, on the stack. Output is printed. • Welcome to PPCG! – DLosc Jul 12 '18 at 20:16 • Thanks! I've been lurking for a while, figured I'd finally give it a shot! – FlexEast Jul 12 '18 at 20:17 # QBasic, 60 53 bytes INPUT n ?1 FOR i=2TO n ?STR$(i-1)MID$(STR$(i),2)
NEXT


A math-based solution, because converting numbers to strings in QBasic is a lot more complicated than it ought to be. Nope, strings are still shorter, you just have to special-case the first item. The problem with STR$(i) is that it adds a space to the start of positive numbers, so we take all but the first character by using MID$. Other than that, it's pretty straightforward.

# Kotlin, 48 bytes

{"1, "+(2..it).map{"$it${it+1}"}.joinToString()}


Yes I know, I could save 1 more byte by removing the blank behind the first comma, but it looks nicer with it :-)

Try it online!

# Shakespeare Programming Language, 310 bytes

,.Ajax,.Ford,.Page,.Act I:.Scene I:.[Enter Ajax and Ford]Ford:Listen tothy.Ajax:You cat.Open heart.[Exit Ajax][Enter Page]Scene V:.Ford:Am I worse Ajax?If notlet usScene X.You be twice the sum ofa cat a big big cat.Speak thy.Page:Open heart.You be the sum ofyou a cat.Open heart.Let usScene V.Scene X:.[Exeunt]


Try it online!

• If you don't mind ending in an error, you can get rid of scene X for 293 bytes – Jo King Sep 22 '18 at 22:01

# Runic Enchantments, 33 bytes

/01iR1-:0)?;{1+:
\$1<\}$q}:+1{$'  Try it online! Uncompressing the entry sequence and moving the reflectors to the other side: >1$01iR1-:0)?;{1+:\
\}$q}:+1{$ '/


Entry sequence (>1$01i) is fairly straight forward. Push and print 1, push 0, push 1, read input and push it to the top of the stack. At R we enter the program's main loop (unrolled with directional control characters removed): 1-:0)?;{1+:'${1+:}q$}  At this point the stack is [0,1,i] where i is the input value. The loop subtracts 1 from the input value (1-), compares it to greater than 0 (if true, skip terminator, else terminate; :0)?;). Then a series of stack manipulations ({1+:{1+:}) and increments to result in [2,(i-1),1,1,2] as well as printing a space ('$). q then concats the top two items on the stack, which is then printed (giving 12 in the output stream).

Finally the stack is rotated once more, leaving [1,2,(i-1)] as the input to the next loop iteration.

## Bonus challenge: using two IPs? 40 bytes

>1$0iR1-:0)?;1{+:' \ > F1iU }$~?=am$?=9m/  Try it online! As there's no way to clone the input to a second instruction pointer (I have thought about stack cloning, so this may be possible in the future, but the spec for it would be difficult to implement), we have to read it from the input stream twice. Flow results in the second pointer being a step behind the first (avoiding merging) and the Fizzle lets us distinguish the two IPs, letting one print a space and the other discards it. I can't figure out a shorter way of performing this check. However if it allowable to print two spaces as a separator it can be reduced to this (30 bytes): >1$0iR1-:0)?;1\
> F1iU}$:+{$ '/

input: 4 4
output: 1  12  23  34


But this is an admittedly dubious answer due to how it has to take input, but 3 bytes shorter than the single IP answer, which is interesting.

Try it online!

### Update: Stack transfer

Getting the two pointers to enter the T command in the right execution order is a huge pain. The remaining two spaces in this program can't be removed, as it messes with the timing, but it avoids having to supply the input value twice. Prints 2 spaces between each entry in the sequence (35 bytes).

>1$0y TR1-:0)?;1\ >1i:1/U}$:+{$'/  Try it online! # MathGolf, 8 6 5 4 bytes {└§p  Try it online! Outputs a newline separated series. ### Explanation: {└§p { Start a loop over the range(0, input) └ Push the top of stack (implicitly the index of the loop) + 1 § Concatenate the two (this removes leading 0s) p And print the value  • Really nice solution! Splitting the printing into two different loop iterations was really clever! I saw that you used the "push n+1 without popping" operator too, it worked really well for this task. – maxb Sep 24 '18 at 10:56 # K (ngn/k), 13 bytes {.,/$x+!2}'!:


Try it online!

! generate the list 0 1 ... n-1

{ } is a function with argument x

' applied to each

!2 is 0 1

x+!2 is x, x+1

\$ format as strings

,/ concatenate

. evaluate (convert to number)

• Strings or numbers are fine for output. – So . should not be needed. – Mr. Xcoder Jul 4 '18 at 5:14
• @Mr.Xcoder thanks - the challenge has changed since I answered it – ngn Jul 4 '18 at 6:22
• @Mr.Xcoder unfortunately, if I remove . there's a leading 0 in "01" – ngn Jul 4 '18 at 6:29