49
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Can it be 0 indexed? \$\endgroup\$ Commented Jul 4, 2018 at 1:09
  • 1
    \$\begingroup\$ @Jo King No. 1 should refer to the first iteration of the sequence as per the challenge spec. \$\endgroup\$ Commented Jul 4, 2018 at 1:14
  • 5
    \$\begingroup\$ 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 \$\endgroup\$ Commented Jul 4, 2018 at 1:40
  • \$\begingroup\$ @Jo King Thanks, I'm glad you liked it. \$\endgroup\$ Commented Jul 4, 2018 at 1:41
  • \$\begingroup\$ Do the outputs have to be in order? Can we mix strings and numbers? \$\endgroup\$ Commented Jul 4, 2018 at 2:56

124 Answers 124

0
\$\begingroup\$

C# (Visual C# Compiler), 46 bytes

If you are allowed to have some using static directives in the top whose bytes do not count:

n=>1+Concat(Range(1,n-1).Select(x=>","+x+++x)) 

Try it online!

Will explode if input n is 0.

It is interesting that, while the C# rules say ","+x+++x means ( "," + (x++) ) + x, if it had meant ( "," + x ) + (++x) instead, the program would still work!

\$\endgroup\$
1
  • \$\begingroup\$ The using Statements should be included in the byte count. See this meta questions. Only using statements that are automatically included by the environment do not need to be counted. (Eg. TIO automatically adds using static System.Console for Visual C# Interactive Compiler. \$\endgroup\$ Commented Jul 5, 2018 at 8:48
0
\$\begingroup\$

Clojure, 54 bytes

#(reduce(fn[a n](conj a(str n(inc n))))[1](range 1 %)) 

Try it online!

Just a reduction over a range between 1 (inclusive) and n (exclusive).

(defn concat-n-n+1 [n] (reduce (fn [acc m] (conj acc (str m (inc m)))) [1] (range 1 n))) 

The first element of the list is a number, the rest are strings. This seems to be allowed by the spec.

\$\endgroup\$
0
\$\begingroup\$

K (oK), 12 bytes

Solution:

,/'$1,2':1+! 

Try it online!

Examples:

,/'$1,2':1+!1 ,,"1" ,/'$1,2':1+!2 (,"1" "12") ,/'$1,2':1+!3 (,"1" "12" "23") ,/'$1,2':1+!10 (,"1" "12" "23" "34" "45" "56" "67" "78" "89" "910") 

Explanation:

,/'$1,2':1+! / the solution ! / range 0..n 1+ / add 1 2': / sliding window of 2 1, / prepend 1 $ / convert to strings ,/' / flatten each 
\$\endgroup\$
0
\$\begingroup\$

C Sharp 51 bytes

n=>new int[n].Select((_,o)=>int.Parse($"{o}{o+1}")) 
\$\endgroup\$
0
\$\begingroup\$

Attache, 15 bytes

{N!_'-~_}=>Iota 

Try it online!

Explanation

{N!_'-~_}=>Iota { }=> map the inside function: Iota (over each number k from 0 to n-1) ' array concatenate: _ k -~_ k+1 N! convert [k, k + 1] to an integer (concatenates and returns a number) 
\$\endgroup\$
0
\$\begingroup\$

Rutger, 108 bytes

x=$Input; f=For[$x]; f=f[@i]; f=f[{p=Subtract[$i];r=Concat[Str[p[1]]];Print[Integer[r[Str[$i]]]];}]; Do[$f]; 

Try it online!

Ungolfed

input = $Input; for = For[$input]; for = for[@index]; for = for[{ dec = Subtract[$index]; ret = Concat[Str[dec[1]]]; Print[Integer[ret[Str[$index]]]]; }]; Do[$for]; 

The basic concept of Rutger is the lack of multi-adic commands: every command takes a single argument, and if the command needs multiple argument, each new call returns a curried function.

First, we take evaluated input and store it in \$x\$. We then create a For object, a tri-adic command. The first two calls create a variable \$f\$, that will loop \$x\$ times, using the iteration variable \$i\$, starting at \$i := 1\$. The third call indicates the code to be run:

{p=Subtract[$i];r=Concat[Str[p[1]]];Print[Integer[r[Str[$i]]]];} 

This creates a block of code containing 3 statements. Our first two are variable assignments. First, we create a curried function Subtract[$i], which prepares to subtract a value from \$i\$. This curried function is saved in the \$p\$ variable. Next, we create a second curried variable, \$r\$, which starts by calling p[1], subtracting \$1\$ from \$i\$. We then convert \$i-1\$ to a string and pass it as the first argument to the Concat function. Finally, we convert \$i\$ to a string, concatenate it to \$str(i - 1)\$ and convert it to an integer to reomve the leading \$0\$ when \$i = 1\$. This is then printed.

The last line, Do[$f];, runs the for loop.

\$\endgroup\$
0
\$\begingroup\$

RAD, 72 bytes

0,(⊢+10×⊣)⍁¨∊¨(⌽∘⌊(⊢|⍨10*(⍳(1+∘⌊10⍟⊢)))÷10*1-⍨(⍳(1+∘⌊10⍟⊢)))¨¨(⊢,1+⊢)¨⍳⎕ 

Link to repository

I wanted to try to see if I could do it in a "non-mathematical" way by splitting a number up into its digits.

\$\endgroup\$
0
\$\begingroup\$

C, 39 bytes

x(n){n&&x(n-1);printf("%d%d, ",n,n+1);} 

eliminated the ternary conditional and shaved off 3 bytes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Leaving the main function out is allowed per meta consensus, since it often is shorter than a full program in C submissions often only implement a function. If you want to leave your submission as a whole program, I suggest removing the two spaces in "%d%d, " and char **n. n==0?:x(n-1); can also be n&&x(n-1);. \$\endgroup\$ Commented Jul 8, 2018 at 21:30
0
\$\begingroup\$

ARBLE, 23 20 bytes

Saved a handful of bytes by using better named variables.

range(0,n)//(x..y|0) 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2.7, 52 bytes

I tried my best. Not yet familiar with this golfing thing.

def a(n): for e in range(n): print int(`e`+`e+1`) 

The int() removes the leading zero in the first output, as specified in the rules.

\$\endgroup\$
1
  • \$\begingroup\$ Changing this to an anonymous lambda function returning a list of numbers gets it down to 42 bytes \$\endgroup\$ Commented Jul 23, 2018 at 1:38
0
\$\begingroup\$

Twig, 72 bytes

Twig is very verbose, causing some issues when trying to reduce the length.

{%macro f(a)%}{%for i in 1..a%}{{o~i}} {%set o=i%}{%endfor%}{%endmacro%} 

This requires that "strict variables" is disabled (default).


How to use?

Simply import the macro and call it:

{% import "fn.twig" as fn %} {{ fn.f(<number>) }} 

You can test it on https://twigfiddle.com/lah1a5

\$\endgroup\$
0
\$\begingroup\$

Haskell, 35 bytes

f n="1":map(show.(-1+)<>show)[2..n] 

Try it online! 1

Explanation / Ungolfed

The operator (<>) is the addition of Semigroups, in case of the Semigroup a -> b (where b needs to be a Semigroup) it is defined as:

(f <> g) = \x-> f x <> g x 

And in case of the Semigroup String it is the same as concatenation, so the code becomes:

f n = "1" : map (\x-> show (x-1) ++ show x) [2..n] 

1 (imports (<>) since it's not part of the Prelude in GHC 8.2.2)

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 22 bytes

:seq(A,A,1,Ans :Ans+(Ans-1)10^(1+int(log(Ans 
\$\endgroup\$
0
\$\begingroup\$

Bash, 56 bytes

echo 1;for i in $(seq $(($1-1)));do echo $i$((i+1));done 

A pretty naive approach.

\$\endgroup\$
0
\$\begingroup\$

Lua, 62 bytes

loadstring't={1}for i=2,(...)do t[#t+1]=(i-1)..i end return t' 

Try it online!


Explanation

This is an anonymous function.

t={1} -- initializes the return table, with the number 1 already in it for i = 2, (...) do -- loop from 2 to the number of the input -- (this is actual code, ... gets the arguments of the program/function t[#t+1] = (i-1)..i -- append to the table i-1 concatenated with i end return t -- returns the table 
\$\endgroup\$
3
  • \$\begingroup\$ You can do [i] instead of [#t+1] and remove the brackets around .... 57 bytes \$\endgroup\$ Commented Jul 29, 2018 at 8:38
  • \$\begingroup\$ Or it's shorter to have a full program. 40 bytes \$\endgroup\$ Commented Jul 29, 2018 at 8:42
  • \$\begingroup\$ About the first comment: yeah, I didn't pay attention :/ About the second: can I?! Hahah I thought the output had to be in the same line, as shown in the "question", that's why I did all of that table thing. \$\endgroup\$ Commented Jul 29, 2018 at 16:37
0
\$\begingroup\$

Swift 4, 43 bytes

print(1);(1..<n).map{print("\($0)\($0+1)")} 

Try it online!

n is the input of the program

\$\endgroup\$
0
\$\begingroup\$

Noether, 15 bytes

I(iWi1+W+WP?!i) 

Try it online!

Explanation

I( ) - Loop until the top of the stack equals the input iW - Push i and convert it to a string i1+W - Add one to i and convert to string + - Concatenate two strings WP - Convert string to a number and print it ? - Print a newline !i - Increment i 
\$\endgroup\$
0
\$\begingroup\$

VBA (Excel), 31 bytes

using immediate window and Cell [A1] as input

for x=1to[a1]:?int(x-1 &x):next

\$\endgroup\$
0
\$\begingroup\$

Python 2, 42 bytes

I know there are plenty of shorter answers already.

a=0 exec"print`a`[:a]+`a+1`;a+=1;"*input() 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pepe, 68 bytes

RrEEEEEREeErREEREEEEEREErEEEEEeEErEEEereEEreeErEEEEEeeEReerREEREeRee 

Try it online!

Previous code has errors when giving input 0, so this is fixed!

Explanation:

# Preparation RrEEEEE # Stack r: [0, 1] REeE # Stack R: [input] # Loop rREE # create label input (r flag: skip until REe) REEEEE # increment input (loop preparation) REE # create label input rEEEEEeEE # join all rEEEe # move r pointer to last reEE # output as int reeE # output newline "\n" rEEEEEeeE # increment all Ree # repeat if input != last content of stack rREE # create label input + 1 (r flag: skip until REe) REe # stop skipping commands Ree # if not 0, go to loop 
\$\endgroup\$
0
\$\begingroup\$

Microscript II, 16 bytes

1vsN-s{lPps1+v}* 

Output is newline-separated.

\$\endgroup\$
0
\$\begingroup\$

Gol><>, 11 bytes

IFLL?nLPN|; 

Try it online!

Explanation:

IFLL?nLPN|; I //Take a number as input F //Loop as many times as the input specified LL // Push the loop counter twice to the stack (same as L:) ?n // Check wether the count is zero, if not print the counter as the first digit LP // Push the loop counter and add 1 N // Output the next digits of the number with nl |; //Exit code 
\$\endgroup\$
0
0
\$\begingroup\$

Dart, 46 45 bytes

f(n)=>List.generate(n,(e)=>e<1?1:'$e${e+1}'); 

  • -1 byte by replacing e==0 by e<1
  • Try it online!

    \$\endgroup\$
    0
    \$\begingroup\$

    Kotlin, 43 41 bytes

    {(1..it).mapIndexed{i,v->"$i$v".toInt()}} 

    Try it online!

    \$\endgroup\$
    0
    \$\begingroup\$

    Knight, 29 bytes

    ;=n+0P;=i 0W>n iO+0++""i=i+1i 

    Try it online!

    \$\endgroup\$
    0
    \$\begingroup\$

    Gema, 42 characters

    *=@repeat{*;${n;}@set{n;@add{${n;};1}}$n } 

    Sample run:

    bash-5.1$ gema '*=@repeat{*;${n;}@set{n;@add{${n;};1}}$n }' <<< 5 1 12 23 34 45 

    Try it online!

    \$\endgroup\$
    0
    \$\begingroup\$

    Ly, 12 bytes

    Rrp[:u`u' o] 

    Try it online!

    This is pretty brute force, but short enough to warrant a post I think...

    R - generate an inclusive rangefrom "0" to the STDIN number rp - reverse the stack, delete the "0" [ ] - for each number on the stack... :u - duplicate number, print it `u - increment the number, print it ' o - print a space 

    AWK, 26 bytes

    a=$1{for(;a;a--)$a=a a+1}1 

    Try it online!

    Turns out that a version in AWK is pretty short too, so here's that one...

    a=$1 - stash the number we want { } - code always runs if STDIN>0 for(;a;a--) - loop for N, N-1, N-2, ... 1 $a=a a+1 - set positional var to N appended w/ N+1 1 - print all the positional vars we just set 

    This really just abuses the fact that AWK will reset the positional variable count if you set them to a value. And it uses a "truthy" condition with no associated code block to print them all.

    \$\endgroup\$
    0
    \$\begingroup\$

    SAS

    Input is passed by:

    %let n=123; 

    4GL - 38

    data; do i=1to &n;x=cats(i-1,i)+0;put x;end; run; 

    The data; and run; are obligatory for every 4GL data step, so the code effectively doing the job is in line 2.

    Macrolanguage - 44

    %macro m(n,x);%do i=1%to&n;%put&x&i;%let x=&i;%end; %mend; 

    The %macro and %mend; are obligatory for every Macroprogram, so the code effectively doing the job is in line 2.

    To run macro execute:

    %m(&n) 
    \$\endgroup\$
    0
    \$\begingroup\$

    Vyxal, 29 bitsv2, 3.625 bytes

    ʀzṅ⌊ 

    Try it Online!

    Explained

    ɾzṅ ɾ # range [1, input] z # overlapping pairs (equivalent to 2l) ṅ # concatenate each pair into a single string 
    \$\endgroup\$
    2
    • 1
      \$\begingroup\$ This appears to consistently omit the first '1' required for each output sequence... \$\endgroup\$ Commented May 23, 2023 at 19:00
    • \$\begingroup\$ (naffetS's answer seems to get 3.375 bytes, though...) \$\endgroup\$ Commented May 23, 2023 at 19:06
    0
    \$\begingroup\$

    SAKO, 85 bytes

    PODPROGRAM:F(N) *)DRUKUJ(9,0):(I-1)×10*ENT(((LN(I))/LN(10))+1)+I POWTORZ:I=1(1)N WROC 

    I use here a quite simple approach:
    We take a number I, multiply it by 10*(length of I+1), add I+1 to it, and print the result. It's repeated until I+1 = N.

    I just use here I-1 and I instead for some byte saving.

    Full programme version, 87 bytes

    CZYTAJ:N *1)DRUKUJ(9,0):(I-1)×10*ENT(((LN(I))/LN(10))+1)+I POWTORZ:I=1(1)N STOP1 KONIEC 
    \$\endgroup\$

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.