48
\$\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\$
7
  • 1
    \$\begingroup\$ Can it be 0 indexed? \$\endgroup\$
    – Jo King
    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\$ 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\$
    – Jo King
    Jul 4, 2018 at 1:40
  • \$\begingroup\$ @Jo King Thanks, I'm glad you liked it. \$\endgroup\$ Jul 4, 2018 at 1:41
  • \$\begingroup\$ Do the outputs have to be in order? Can we mix strings and numbers? \$\endgroup\$
    – xnor
    Jul 4, 2018 at 2:56

111 Answers 111

1 2 3
4
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\$
    – Jo King
    Jul 29, 2018 at 8:38
  • \$\begingroup\$ Or it's shorter to have a full program. 40 bytes \$\endgroup\$
    – Jo King
    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\$
    – Visckmart
    Jul 29, 2018 at 16:37
0
\$\begingroup\$

Ruby, 34 33 bytes

->n{$><<i||=p(1);p(i+=1)<n&&redo}

Try it online!

Pseudocode:

loop
  if i is undefined
    set i to 1
    print 1
    print newline
  end if
  print i
  increment i by 1
  print i
  print newline
  if i >= n
    break and return
  end if
end loop
\$\endgroup\$
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\$

    Go, 122 bytes, using string operations

    import(."strconv";."fmt")
    func f(n int)(o[]int){for i:=0;i<n;i++{k,_:=Atoi(Sprintf("%d%d",i,i+1))
    o=append(o,k)}
    return o}
    

    Attempt This Online!

    Go, 125 bytes, using formula

    import."math"
    func f(n int)(o[]int){for i:=0;i<n;i++{o=append(o,i*int(Pow10(1+int(Floor(Log10(float64(i)+1)))))+i+1)}
    return}
    

    Attempt This Online!

    Uses the formula \$a(n) = n\cdot10^{1 + \lfloor\log_{10}(n+1))\rfloor} + n + 1\$ for \$n \ge 0\$.

    \$\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\$ May 23 at 19:00
    • \$\begingroup\$ (naffetS's answer seems to get 3.375 bytes, though...) \$\endgroup\$ May 23 at 19:06
    1 2 3
    4

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.