44
\$\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\$
  • 1
    \$\begingroup\$ Can it be 0 indexed? \$\endgroup\$ – Jo King Jul 4 '18 at 1:09
  • 3
    \$\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 '18 at 1:40
  • 3
    \$\begingroup\$ After 7 days, I will accept the shortest answer which meets all these criteria. Why is there a need for the challenge to end? \$\endgroup\$ – Erik the Outgolfer Jul 4 '18 at 9:13
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – user202729 Jul 4 '18 at 9:21
  • 2
    \$\begingroup\$ @Dennis Ok, i'll remove the date from the challenge; maybe I'll accept when no more new answers are coming. \$\endgroup\$ – Amphibological Jul 4 '18 at 13:41

97 Answers 97

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\$
  • 1
    \$\begingroup\$ 13 bytes. Also note that you don't have to count flags as bytes anymore \$\endgroup\$ – Jo King Oct 17 '18 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\$ – Teal pelican Oct 19 '18 at 8:30
  • 1
    \$\begingroup\$ Herr's the meta discussion. \$\endgroup\$ – Jo King Oct 19 '18 at 9:46
  • \$\begingroup\$ @JoKing Perfect, Thank you. \$\endgroup\$ – Teal pelican Oct 19 '18 at 11:39
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\$

Jelly, 8 6 bytes

ṭ’DFḌ)

Try it online!

ṭ’DFḌ)    Monadic link with argument n
     )    For each int in the range 1..n
 ’        n-1
ṭ         [n-1, n]
  D       Digits of n-1 and n
   F      Flatten into one list
    Ḍ     Convert back to an integer.
\$\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\$
  • \$\begingroup\$ My answer has been outdone by @Doorknob, go check out his. \$\endgroup\$ – Amphibological Jul 4 '18 at 1:00
  • \$\begingroup\$ You can save a byte by using [0..n] and [1..n] instead. \$\endgroup\$ – Wheat Wizard Jul 4 '18 at 1:45
0
\$\begingroup\$

CJam, 11 bytes

{{_)s+si}%}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ {_)s+si}%p <s>saves one byte</s> (nevermind, it's not allowed since it's not a function) \$\endgroup\$ – maxb Jul 4 '18 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\$

Julia 0.6, 30 bytes

n->["1";["$i$(i+1)"for i=1:n]]

Try it online!

Alternative version with numeric output:

34 bytes

n->[10^ndigits(i)*~-i+i for i=1:n]

Try it online!

\$\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\$
  • \$\begingroup\$ +(⌊10⍟⊢)+∘⌊10⍟⊢ \$\endgroup\$ – Adám Jul 4 '18 at 6:24
  • \$\begingroup\$ Thanks! Outgolfed in my own language ... somewhat, but this is also APL code, so not really. \$\endgroup\$ – Zacharý Jul 4 '18 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\$
  • \$\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 Jul 5 '18 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
    \$\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\$ – Jonathan Frech Jul 8 '18 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\$
  • \$\begingroup\$ Changing this to an anonymous lambda function returning a list of numbers gets it down to 42 bytes \$\endgroup\$ – Jo King Jul 23 '18 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\$
  • \$\begingroup\$ You can do [i] instead of [#t+1] and remove the brackets around .... 57 bytes \$\endgroup\$ – Jo King Jul 29 '18 at 8:38
  • \$\begingroup\$ Or it's shorter to have a full program. 40 bytes \$\endgroup\$ – Jo King Jul 29 '18 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 '18 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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