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
    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\$
    – Jo King
    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\$
    – xnor
    Commented Jul 4, 2018 at 2:56

111 Answers 111

1
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ If you don't mind ending in an error, you can get rid of scene X for 293 bytes \$\endgroup\$
    – Jo King
    Commented Sep 22, 2018 at 22:01
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – maxb
    Commented Sep 24, 2018 at 10:56
1
\$\begingroup\$

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)

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

MBASIC, 102 94 bytes

1 INPUT N:FOR I=0 TO N-1:IF I>0 THEN PRINT MID$(STR$(I),2);
2 PRINT MID$(STR$(I+1),2);" ";:NEXT

Output:

? 3
1 12 23

? 10
1 12 23 34 45 56 67 78 89 910

? 16
1 12 23 34 45 56 67 78 89 910 1011 1112 1213 1314 1415 1516

Could have been much cleaner, but the STR$(n) number-to-string conversion function returns with a leading space that had to be dealt with.

Turns out the variable for NEXT and the trailing PRINT are not needed, saving 8 bytes.

\$\endgroup\$
1
\$\begingroup\$

><>, 15 13 bytes

ln1-:?!;aoln:

Try it online!

We take in the input as a command line argument.

Thanks to @jo king for 2 byte loss (noticing the input loop value could be used as stack length.

Explanation (simple):

ln1-:?!;aoln:
ln              : Add the length of the stack to the stack and print.
   1-:?!;       : Take 1 off the input loop and check if zero, if 0, end.
         ao     : Print new line
           ln:  : Add the length of the stack to the stack, print and duplicate the input loop.
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 13 bytes. Also note that you don't have to count flags as bytes anymore \$\endgroup\$
    – Jo King
    Commented Oct 17, 2018 at 5:35
  • \$\begingroup\$ @JoKing I know I haven't been super active recently but when was that change? Also that is a nice change to mine, didn't even think about using the input as the length. \$\endgroup\$ Commented Oct 19, 2018 at 8:30
  • 1
    \$\begingroup\$ Herr's the meta discussion. \$\endgroup\$
    – Jo King
    Commented Oct 19, 2018 at 9:46
  • \$\begingroup\$ @JoKing Perfect, Thank you. \$\endgroup\$ Commented Oct 19, 2018 at 11:39
1
\$\begingroup\$

Nim, 70 bytes

import sugar,sequtils,math
s(x=>toSeq(1..x).map y=>(y-1)*10^len($y)+y)

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

jq, 30 27 characters

1,(range(1;.)|"\(.)\(.+1)")

Sample run:

bash-5.1$ jq -r '1,(range(1;.)|"\(.)\(.+1)")' <<< 5
1
12
23
34
45

Try it online!

\$\endgroup\$
1
\$\begingroup\$

J-uby, 26 25 bytes

-1 byte thanks to Razetime

:*|:*&(-[I,:+&1]|:join|Z)

Attempt This Online!

Explanation

:* |              # Range 0..n-1, then
:* & (            # map with ...
  -[I, :+ & 1] |  #   [m, m+1] (I is identity), then
  :join |         #   join, then
  Z               #   convert to int (for special case "01")
)
\$\endgroup\$
1
  • 2
    \$\begingroup\$ :join in place of ~:*&"" should be -1 \$\endgroup\$
    – Razetime
    Commented Oct 10, 2022 at 3:18
1
\$\begingroup\$

Vyxal, 5 bytes

ʀ¨pJ⌊

Try it Online!

Explained

ʀ¨pJ⌊
ʀ     # Inclusive zero range
 ¨p   # each overlapping pair
   J  # merge
    ⌊ # floor
\$\endgroup\$
1
\$\begingroup\$

Nim, 46 bytes

proc(x:any)=
 echo 1;for i in(2..x):echo i-1,i

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Rockstar, 76 bytes

listen to N
X's 0
while N-X
let S be X
let X be+1
let S be+""+X
cast S
say S

Try it here (Code will need to be pasted in)

listen to N       :Read input string into variable N
X's 0             :Initialise X as 0
while N-X         :While X is less than N
let S be X        :  Assign X to S
let X be+1        :  Increment X
let S be+""+X     :  Append X to S
cast S            :  Cast S to an integer
say S             :  Output S
\$\endgroup\$
1
\$\begingroup\$

Julia, 28 bytes

!n=["1";2:n.|>i->"$(i-1)$i"]

Try it online!

based on the answer by Sundar R

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

Tcl, 59 bytes

proc C n {puts [incr i]
time {puts $i[incr i]} [expr $n-1]}

Try it online!

PS: I will golf it more later!

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

Haskell, 57 55 bytes

f n=read<$>zipWith((.show).(++).show)[0..][1..n]::[Int]

A function which takes a number and returns a list.

EDIT: -2 Bytes thanks to Cat Wizard

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ My answer has been outdone by @Doorknob, go check out his. \$\endgroup\$ Commented Jul 4, 2018 at 1:00
  • \$\begingroup\$ You can save a byte by using [0..n] and [1..n] instead. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 4, 2018 at 1:45
0
\$\begingroup\$

CJam, 11 bytes

{{_)s+si}%}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ {_)s+si}%p <s>saves one byte</s> (nevermind, it's not allowed since it's not a function) \$\endgroup\$
    – maxb
    Commented Jul 4, 2018 at 11:32
0
\$\begingroup\$

Python 2, 43 bytes

lambda i:[1]+[`x`+`x+1`for x in range(1,i)]

Try it online!

First element is an integer, the rest are strings.

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

Perl 5, 29 bytes

say"@{[1,map$_-1 .$_,2..<>]}"
\$\endgroup\$
0
\$\begingroup\$

RAD, 28 27 bytes

(⊢(⊢+⊣×10*1+∘⌊10⍟⊢)1+⊢)¨0,⍳

Repository

Must be called like: ((⊢(⊢+⊣×10*1+∘⌊10⍟⊢)1+⊢)¨0,⍳) <value>, as I broke assignment of functions when I implemented shy results.

Note: this should also work in Dyalog APL, and due to that: -1 byte thanks to @Adám!

The can either be the APL symbol or the greek one if run in RAD, but must be the APL version ifran in Dyalog APL

\$\endgroup\$
2
  • \$\begingroup\$ +(⌊10⍟⊢)+∘⌊10⍟⊢ \$\endgroup\$
    – Adám
    Commented Jul 4, 2018 at 6:24
  • \$\begingroup\$ Thanks! Outgolfed in my own language ... somewhat, but this is also APL code, so not really. \$\endgroup\$
    – Adalynn
    Commented Jul 4, 2018 at 13:21
0
\$\begingroup\$

C# (Visual C# Compiler), 54 bytes

Uses space as seperator character.

n=>{var r="";for(;n-->1;)r=" "+n+(n+1)+r;return"1"+r;}

Try it online!

Ungolfed full code:

class P
{
    static void Main()
    {
        System.Func<int, string> f =
            n =>
            {
                var r = "";        //Declare variable for the result
                for (; n--> 1;)    //Loop until n is 1

                    r =            //Set the result to:
                        " " +      //Seperator +
                        n +        //Value of n as string
                        (n + 1) +  //Value of n + 1 as string
                        r;         //Previous content of the result

                return "1" + r;    //Return 1 (first item) + the result.
            }
            ;

        System.Console.WriteLine(f(1));
        System.Console.WriteLine(f(2));
        System.Console.WriteLine(f(3));
        System.Console.WriteLine(f(10));
        System.Console.WriteLine(f(100));
    }
}
\$\endgroup\$
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\$
    – raznagul
    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\$
    – Jo King
    Commented Jul 23, 2018 at 1:38

Your Answer

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

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