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

13
\$\begingroup\$

Jelly, 3 bytes

ŻVƝ

A monadic link accepting an integer which yields a list of integers

Try it online!

How?

ŻVƝ - Link: integer       e.g. 59
Ż   - zero-range               [0,1,2,3,4,5,6, ... ,58,59]
  Ɲ - apply to each pair: i.e: [0,1] or [5,6]  or  [58,59]
 V  -   evaluate* jelly code   1     or 56     or  5859
    -                       -> [1,12,23,45,56, ... 5859]

* When given a list V actually joins the Python string values and evaluates that
  ...so e.g.: [58,59] -> ['58','59'] -> '5859' -> 5859
\$\endgroup\$
0
10
\$\begingroup\$

Python 3, 39 bytes

f=lambda n:1//n or f'{f(n-1)} {n-1}{n}'

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Never thought f-strings can be used for golfing! Nice idea. \$\endgroup\$
    – Chromium
    Commented Jul 6, 2018 at 10:17
10
\$\begingroup\$

R, 32 bytes

strtoi(paste0((x=1:scan())-1,x))

Try it online!

Outgolfed by MickyT, so go upvote that answer!

\$\endgroup\$
7
  • \$\begingroup\$ There’s been an edit to allow strings... no need for strtoi! \$\endgroup\$
    – JayCe
    Commented Jul 4, 2018 at 14:10
  • 2
    \$\begingroup\$ @JayCe it's necessary to strip the leading 0 from the first output. \$\endgroup\$
    – Giuseppe
    Commented Jul 4, 2018 at 16:08
  • \$\begingroup\$ couldn't you remove the leading zero by ending with [-1]rather than using strtoi or does that fail in some edge case or other? \$\endgroup\$
    – JDL
    Commented Jul 6, 2018 at 12:53
  • \$\begingroup\$ @JDL strtoi is being used to convert from "01" to 1 because paste0 will return c("01","12","23","34",...) and we aren't allowed to return "01". \$\endgroup\$
    – Giuseppe
    Commented Jul 6, 2018 at 13:09
  • 1
    \$\begingroup\$ @CriminallyVulgar unfortunately that will fail for input of 1 \$\endgroup\$
    – Giuseppe
    Commented Aug 9, 2018 at 11:09
8
\$\begingroup\$

Haskell, 38 37 bytes

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

Try it online!

Thanks to Cat Wizard for a byte!

\$\endgroup\$
1
  • \$\begingroup\$ As far as golfing goes you can also use <$> as a substitute for map, that can be infixed. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 4, 2018 at 1:05
7
\$\begingroup\$

Cubix, 19 bytes

I.1.W)>OSo;u.uO;@!-

Try it online!

This wraps onto the cube as follows

    I .
    1 .
W ) > O S o ; u
. u O ; @ ! - .
    . .
    . .

Watch It Run

Got a little room to play with yet, but at the moment

  • W redirect to the top face heading down
  • I1> set up the stack with the input and 1 then redirect into the main loop
  • OSo;u output the top of stack, add space to stack, output, remove and uturn
  • -!@;Ou) subtract TOS from input, if 0 halt else pop result, output TOS, uturn and increment TOS. Back into the main loop.
\$\endgroup\$
7
\$\begingroup\$

Perl 6, 19 18 bytes

{(^$_ Z~1..$_)X+0}

Try it online!

Anonymous code block that zips the range 0 to n-1 with 1 to n using the concatenation operator, then adds 0 to every element to force it to a number and remove leading 0s.

\$\endgroup\$
7
\$\begingroup\$

R, 30 29 bytes

An extra byte thanks to @Giuseppe

10^nchar(n<-1:scan())*(n-1)+n

Try it online!

A mostly mathematical solution, except for using nchar() rather than floor(log10()). I was really surprised that it came in shorter than the string version.

\$\endgroup\$
2
  • \$\begingroup\$ 29 bytes! nice work on this, I never would have thought of it! \$\endgroup\$
    – Giuseppe
    Commented Jul 5, 2018 at 21:36
  • \$\begingroup\$ @Giuseppe thanks for the extra byte. \$\endgroup\$
    – MickyT
    Commented Jul 5, 2018 at 21:49
4
\$\begingroup\$

APL (Dyalog), 13 12 bytes

1 byte saved thanks to @FrownyFrog

(⍎⍕,∘⍕1∘+)¨⍳

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Wow, our answers tied \$\endgroup\$
    – Adalynn
    Commented Jul 4, 2018 at 0:59
  • \$\begingroup\$ @Zacharý yours is more APLish though :) \$\endgroup\$
    – Uriel
    Commented Jul 4, 2018 at 1:05
  • \$\begingroup\$ Save 1: (⍎⍕,∘⍕1∘+)¨⍳ \$\endgroup\$
    – FrownyFrog
    Commented Jul 4, 2018 at 1:31
4
\$\begingroup\$

JavaScript (Node.js), 25 bytes

f=n=>--n?f(n)+','+n+-~n:1

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ f=n=>--n?[f(n),n]+-~n:1 \$\endgroup\$
    – l4m2
    Commented Jul 23, 2018 at 13:22
4
\$\begingroup\$

Brachylog, 6 bytes

⟦s₂ᶠcᵐ

Try it online!

Explanation

⟦         Range: [0, …, Input]
 s₂ᶠ      Find all substrings of length 2
    cᵐ    Map concatenate
\$\endgroup\$
4
\$\begingroup\$

Python 2, 42 41 bytes

f=lambda n:n-1and f(n-1)+[`n-1`+`n`]or[1]

Try it online!

Recursive function that returns a mixed list of strings and integers

\$\endgroup\$
5
  • \$\begingroup\$ Did you intend to edit out the original description "Anonymous function that returns a list"? \$\endgroup\$ Commented Jul 4, 2018 at 5:44
  • \$\begingroup\$ @EsolangingFruit Oopsie, fixed! Thanks \$\endgroup\$
    – Jo King
    Commented Jul 4, 2018 at 5:54
  • \$\begingroup\$ Maybe I am missing something but this doesn't seem to have '12' as the second element. \$\endgroup\$
    – ElPedro
    Commented Jul 4, 2018 at 8:17
  • 2
    \$\begingroup\$ @ElPedro That can be fixed by saving a byte with n and \$\endgroup\$
    – Mr. Xcoder
    Commented Jul 4, 2018 at 8:36
  • 1
    \$\begingroup\$ It's not often that a fix saves bytes :-) \$\endgroup\$
    – ElPedro
    Commented Jul 4, 2018 at 8:42
4
\$\begingroup\$

Haskell, 34 bytes

f n="1":[show=<<[i-1,i]|i<-[2..n]]

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Blossom, 88 bytes

rule e<int x>[1(x)]=>[1(x-1),2(str(x)+str(x+1))];rule c[1(0)]=>[];e!c

Blossom is a graph programming language I'm working on. It can only take graphs as inputs, so this programme expects a graph comprising a single node with its label an integer. It returns a graph of connected edges to form the closest to an array I can get, and the resultant graph is printed to output.

An unminified version of the code is this:

rule expand <int x>
    [ 1 (x) ]
 => [ 1 (x-1), 2(str(x)+str(x+1)) ]
where x > 0;

rule clean
    [ 1 (0) ]
 => [];

expand! clean

It defines two rules: one called expand, which (while there is a node with an integer-valued label in the current graph) creates another node with its increment concatenated, and lowers the value. This rule also has the condition that x is greater than 0.

The ! executes this rule for as long as it can be applied on the graph, so in this case it will execute until x is 0. And then the clean rule removes this 0 node.

Blossom was not made for golfing, but it doesn't do too badly, I don't think., considering what it is. There currently isn't really an easy way for people to test blossom code (and the interpreter I'm working on at the moment is not quite finished and a little buggy), but this isn't exactly a competing entry!

\$\endgroup\$
3
\$\begingroup\$

Shakespeare, 703 bytes

Q.Ajax,.Ford,.Act I:.Scene I:.[enter Ajax and Ford]Ford:Open mind!Scene V:.Ajax:You is the sum of thyself the sum of myself the sum of a big bad fat old red pig a big bad fat old lie!Ford:Open mind!Is you nicer zero?Ajax:If so, you is twice the sum of the sum of twice thyself twice thyself thyself!If so,Let us Scene V!Ford:You a cat!Open heart!Scene X:.Ajax:You is the sum of thyself a pig!Is you worse than a cat?If so,let us Scene C.Remember thyself.You is the sum of the sum of a big old red cute rich cat a big old red cute joy a big old pig!Speak mind!You is a big old red cute rich cat!Speak mind!Recall!Ford:Open heart!You is the sum of thyself a joy!Open heart!Let us Scene X.Scene C:.[exeunt]

try it here

ungolfed version

127421th Night.
Ajax, likes to read the stars.
Ford, someone Ajax can always count on.
Act I:.
Scene I: Ajax reads a star.
[enter Ajax and Ford]
Ford: Open your mind! 
Scene V: Ford counts what ajax has learned.
Ajax: you are the sum of thyself and the sum of myself and the sum of a big bad fat old red pig and a big bad fat old lie!
Ford: Open Your mind! Are you nicer than zero?
Ajax: If so, you are twice the sum of the sum of twice thyself and twice thyself and thyself! 
If so, Let us Scene V!
Ford: You are a cat! Open your heart!

Scene X: Ajax and Ford recall the nights.
Ajax: You are the sum of thyself and a pig! Are you worse than a cat? If so, Let us Scene C.
Remember thyself. 
You are the sum of the sum of a big old red cute rich cat and a big old red cute joy and a big old pig! 
Speak you mind!
You are a big old red cute rich cat! Speak your mind! Recall your finest hour!
Ford: Open your heart! You are the sum of thyself and a joy! Open your heart! Let us Scene X.
Scene C: Fin.
[exeunt]
\$\endgroup\$
3
\$\begingroup\$

Groovy, 35 bytes

{(0..<it)*.with{""+it+++it as int}}

Try it online!

I came up last minute with the idea of using *.with instead of .collect. I have no idea what it+++it parses to but whether it's it++ + it or it + ++it they both do the same thing. I tried to think of a way of getting rid of the < in ..< by turning it into 1..it and decrementing but I don't think it would get any shorter.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG; nice first post! Regarding the parsing of a+++b, this test suggests it is parsed from left to right, meaning (a++)+b. \$\endgroup\$ Commented Jul 5, 2018 at 14:30
3
\$\begingroup\$

C (gcc), 44 43 bytes

f(i){i--&&printf(" %2$d%d"+5*!f(i),i+1,i);}

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ @DLosc The %m$ format specifier "denotes the position in the argument list of the desired argument, indexed starting from 1" (printf(3) man page). It's pretty handy as long as your C library supports it! \$\endgroup\$
    – ErikF
    Commented Jul 8, 2018 at 5:20
  • \$\begingroup\$ Thanks... but I'm still confused why %d%d (and switching the order of the arguments) doesn't work. (I tried it, but don't know why it produces no output.) \$\endgroup\$
    – DLosc
    Commented Jul 8, 2018 at 22:11
  • \$\begingroup\$ @DLosc If you change the format string, make sure to change the offset after it (e.g. " %d%d" should have +3*!f(i); otherwise, the +5 offset points to the NUL at the end of the string.) \$\endgroup\$
    – ErikF
    Commented Jul 8, 2018 at 23:02
  • \$\begingroup\$ Oh, okay--I finally get it: the "#2, then #1" is necessary because in the base case, the shortened format string becomes just "#1" and so you need the first printf argument to be i+1, not i. Very interesting. \$\endgroup\$
    – DLosc
    Commented Jul 9, 2018 at 14:48
  • \$\begingroup\$ 41 bytes abusing undefined behaviour in the form of unsequenced modification and access to i. \$\endgroup\$
    – c--
    Commented Oct 9, 2022 at 20:28
3
\$\begingroup\$

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.

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

Pyth, 9 8 6 bytes

ms+`dh

Try it online!

Explanation:

       - implicit output
m      - map function with argument d:
  +    -  concatenate
    d  -  argument d
   `   -  to string
     h -  into implicit d + 1
       - into Q (implicit input)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcom to PPCG! :) \$\endgroup\$
    – Shaggy
    Commented Jul 6, 2018 at 11:41
  • \$\begingroup\$ @Shaggy Thank you, this is my first time doing this. \$\endgroup\$
    – u-ndefined
    Commented Jul 6, 2018 at 13:39
3
\$\begingroup\$

Fig, \$8\log_{256}(96)\approx\$ 6.585 bytes

Mrx'_Jx}

Try it online!

Mrx'_Jx}
Mrx'     # For every number in the range [0, input)
       } # Input + 1
     Jx  # Prepend the input to that
    _    # Parse as number to remove leading 0s
\$\endgroup\$
4
  • \$\begingroup\$ This doesn't work for 1 - leading zeros aren't allowed. (Also, the challenge does say to output the first n terms...) \$\endgroup\$
    – naffetS
    Commented Oct 9, 2022 at 19:00
  • \$\begingroup\$ @Steffan but it is tagged sequence, so therefore the i/o defaults are allowed? \$\endgroup\$
    – Seggan
    Commented Oct 9, 2022 at 19:23
  • \$\begingroup\$ the i/o defaults are not allowed if the question overrides them. they are defaults, not requirements \$\endgroup\$
    – naffetS
    Commented Oct 9, 2022 at 19:24
  • \$\begingroup\$ @Steffan oof. done \$\endgroup\$
    – Seggan
    Commented Oct 9, 2022 at 19:28
2
\$\begingroup\$

Jelly, 4 bytes

ḶżRV

Try it online!

How it works

ḶżRV  Main link. Argument: n

Ḷ     Unlength; yield [0, ..., n-1].
  R   Range; yield [1, ... n].
 ż    Zipwith; yield [[0, 1], ..., [n-1, n]].
   V  Eval; cast each array to string and evaluate, yielding integers.
\$\endgroup\$
2
\$\begingroup\$

Python 2, 44 bytes

for i in range(input()):print`i`*(i>0)+`i+1`

Try it online!

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

05AB1E, 6 bytes

>GNJ,N

Try it online!

Explanation

>G       # for N in [1 ... input]
  N      # push N
   J     # join stack
    ,    # print
     N   # push N (for next iteration)

LεD<ìï would work for same byte count but with list output

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Classic), 9 bytes

1,2,/⍕¨∘⍳

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice job, I can't believe I didn't think of that. \$\endgroup\$
    – Adalynn
    Commented Jul 4, 2018 at 13:19
2
\$\begingroup\$

Haskell, 37 bytes

f x=[[y-1|y>1]++[y]>>=show|y<-[1..x]]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt -m, 6 5 bytes

ó2 ¬n

Try it online!

As always, know the flags.

Unpacked & How it works

-m       Convert to range and map...

Uó2 q n
Uó2      Construct [U, U+1]
    q    Join
      n  Convert to number

         Implicit output (Array is printed as comma-delimited values)
\$\endgroup\$
4
  • \$\begingroup\$ 5 bytes. Don't know why ó doesn't work here without the 2. \$\endgroup\$
    – Shaggy
    Commented Jul 4, 2018 at 10:39
  • \$\begingroup\$ I have 2 other 5 byte solutions (both using the same method) that don't use a flag, if anyone else wants to take a stab at them. \$\endgroup\$
    – Shaggy
    Commented Jul 4, 2018 at 11:04
  • \$\begingroup\$ I think 5 + 2 = 7 bytes because of flag? \$\endgroup\$
    – u-ndefined
    Commented Jul 7, 2018 at 6:18
  • \$\begingroup\$ @u_ndefined Flags are no longer counted towards bytes, and treated as different implementation. \$\endgroup\$
    – Bubbler
    Commented Jul 8, 2018 at 7:11
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 103 71 64 56 bytes


Golfed Try it online!

i=>{for(int x=0;x<i;)Write($"{(x>0?$",{x}":"")}{++x}");}

Ungolfed

i => {
    for( int x = 0; x < i; )
        Write( $"{( x > 0 ? $",{x}" : "")}{ ++x }" );
}

Full code

Action<Int32> a = i => {
    for( int x = 0; x < i; )
        Write( $"{( x > 0 ? $",{x}" : "")}{ ++x }" );
    };

Int32[]
    testCases = new Int32[] {
        1,
        2,
        3,
        10,
    };

foreach( Int32[] testCase in testCases ) {
    WriteLine( $" Input: {testCase}\nOutput:" );
    a(testCase);
    WriteLine("\n");
}

Older versions:

  • v1.2, 64 bytes

    i=>{for(int x=0;x<i;)Write($"{(x>0?$",{x}":"")}{++x}");}
    
  • v1.1, 71 bytes

    i=>{for(int x=0;x<i;)System.Console.Write($"{(x>0?$",{x}":"")}{++x}");}
    
  • v1.0, 103 bytes

    i=>{for(int x=0;x<i;)System.Console.Write($"{(x>0?",":"")}{x++*System.Math.Pow(10,$"{x}".Length)+x}");}
    

Releases

  • v1.3 - - 8 bytes - Removed Console thanks again to raznagul
  • v1.2 - - 7 bytes - Removed System thanks to raznagul
  • v1.1 - -32 bytes
  • v1.0 - 103 bytes - Initial solution.

Notes

  • None
\$\endgroup\$
3
  • 1
    \$\begingroup\$ The C# Interactive Compiler has static imports for System.Console. So you can save 15 bytes by removing it. \$\endgroup\$
    – raznagul
    Commented Jul 4, 2018 at 13:34
  • \$\begingroup\$ Right! Habit of having to use them \$\endgroup\$
    – auhmaan
    Commented Jul 4, 2018 at 13:50
  • \$\begingroup\$ You can also remove Console.: TIO \$\endgroup\$
    – raznagul
    Commented Jul 4, 2018 at 13:58
2
\$\begingroup\$

J, 14 bytes

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 2,&.":/\i.@>: for 13 bytes. Try it online! \$\endgroup\$
    – Jonah
    Commented Jul 5, 2018 at 4:41
2
\$\begingroup\$

ABAP, 101 bytes

Not really a golfing language, but I'm having a lot of fun with it

WHILE x<w.
CLEAR z.
IF x=1.
WRITE x.
ELSE.
CONCATENATE y x INTO z.
WRITE z.
ENDIF.
y=x.
x=x+1.
ENDDO.

W is the input term, X is the counter from 1, Y is X-1 from the second pass onward, Z is concatenated string.

\$\endgroup\$
2
\$\begingroup\$

Powershell, 27 26 bytes

1.."$args"|%{"$p$_";$p=$_}

-1 byte: thanks AdmBorkBork

Test script:

$f = {
1.."$args"|%{"$p$_";$p=$_}
}

&$f 1
""
&$f 2
""
&$f 3
""
&$f 10
""
&$f 46
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can save a byte doing 1.."$args" instead. \$\endgroup\$ Commented Jul 5, 2018 at 13:41
2
\$\begingroup\$

Python 2, 41 bytes

lambda l:[`n`[:n]+`n+1`for n in range(l)]

Try it online!

\$\endgroup\$

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.