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

2
\$\begingroup\$

PHP, 33 32 bytes

while($argv[1]--)echo" $i".++$i;

Try it Online

Old version

for(;$i<$argv[1];)echo" $i".++$i;     // 33 bytes
\$\endgroup\$
0
2
\$\begingroup\$

Javascript, 43 44,46,49,53 bytes

n=>[...Array(n)].map((_,a)=>~~(a+(a+1+"")))

Previous versions :

n=>[...Array(n)].map((_,a)=>~~(a+(""+ ++a)))
n=>[...Array(n)].map((_,a)=>0- -(a+(""+ ++a)))
n=>[...Array(n).keys()].map(a=>0- -(a+(""+ ++a)))
n=>[...Array(n).keys()].map(a=>parseInt(a+(""+ ++a)))

Saved 3 bytes thanks to @Shaggy's solution (which is better than mine) to remove .keys()

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 39 bytes \$\endgroup\$
    – Shaggy
    Commented Jul 6, 2018 at 13:08
2
\$\begingroup\$

Husk, 11 9 bytes

-2 thanks to @BMO!

mSöd+d←dḣ

Try it online!

Explanation

m          map(                                              )
 S                                               <*>
  ö            (           .).    .         .
   d            fromDecimal
    +                         (++)
     d                             toDecimal
      ←                                      (+1)
       d                                            toDecimal
                                                              .
        ḣ                                                      (\n->[1..n])

mSöd+d←dḣ  map((fromDecimal.).(++).toDecimal.(+1)<*>toDecimal).(\n->[1..n])
\$\endgroup\$
2
\$\begingroup\$

yup, 48 bytes

0eee0ee-0ee-*:0e#[:@]0e-{]:[:]-:#0e0~--#]:@]0e-}

Try it online!

Explanation

yup only knows of a few commands. To modify data, I'll be using 0 (nilad pushing that number), e (natural exponentiation), and - (subtraction).

For example, the snippet 0e is equal to 1, since \$e^0=1\$.

This program is divided into two parts: initialization and iteration.

Initialization

0eee0ee-0ee-*:0e#[:@]0e-

0eee pushes \$e^e\approx15.1543\$ and 0ee pushes \$e\approx2.7183\$. Thus, the expression 0eee0ee-0ee- pushes:

$$e^e-e-e\approx9.7177$$

This encodes 0x10 (the linefeed), since numbers are rounded to the nearest integer before being outputted. This is our numeric separator.

The next part is to initialize the stack with data. *: pushes the input twice, and 0e# outputs a 1. [:@] outputs a linefeed without popping it from the stack, and 0e- decrements the input. This handles the edge case of outputting 1 instead of 01. Rather than handle this with a conditional, hardcoding the first entry is shorter.

Iteration

{]:[:]-:#0e0~--#]:@]0e-}

{...} loops while the TOS is defined and positive. In this case, it will stop when it hits zero. Each iteration starts off with a stack like this:

[9.7177, input, iterator]

iterator starts at input - 1. ]:[:]- calculates input - iterator and gives us our true iterator value. Then, :#0e0~--# first outputs the true iterator then the same number plus 1, where 0e0~-- encodes \$-(-n-e^0)=-(-n-1)=n+1\$. Then, ]:@] restores the stack to its initial shape and 0e- subtracts 1 from the iterator, continuing our loop. This repeats until iterator reaches 0, at which point the loop stops and the program terminates.

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

Python 3, 44 bytes

lambda n:[f"{j or''}{j+1}"for j in range(n)]

Try it online!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to the site! You can save bytes by removing the space between your string and the for. Also since this is an anonymous function there is no need for f=, so you can save 2 bytes by removing it. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 9, 2018 at 4:28
  • \$\begingroup\$ @CatWizard Thanks! But what do you mean by anonymous function? is there a way to call an anonymous function? \$\endgroup\$
    – JathOsh
    Commented Jul 9, 2018 at 5:08
  • 2
    \$\begingroup\$ An anonymous function is any function that is not associated with a name. In python the lambda keyword makes an anonymous function. Anonymous functions can be set to variables as you did, they can also be called directly without assignment (that's what makes them anonymous) by wrapping them in parentheses e.g. (lambda x:x+1)(6). For TIO you can put f=\ in the "header" section and call the function with f. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 9, 2018 at 5:12
  • \$\begingroup\$ @CatWizard Thanks that is super helpful \$\endgroup\$
    – JathOsh
    Commented Jul 9, 2018 at 5:29
  • 1
    \$\begingroup\$ How about having the condition for 1 inside the format string? 44 bytes \$\endgroup\$
    – Jo King
    Commented Jul 9, 2018 at 6:28
2
\$\begingroup\$

Vyxal, 4 bytes

ƛ‹p⌊

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ >:( beat me by 1 byte \$\endgroup\$
    – DialFrost
    Commented Oct 11, 2022 at 2:55
2
\$\begingroup\$

Scratch, 107 bytes

enter image description here

define(y
delete all of[x v
set[i v]to(0
repeat(y
add((join(i)((i)+(1)))-())to[x v
change[i v]by(1
end
say(x

I would've used for each, but scratchblocks doesn't support it.

\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

cQuents, 11 bytes

=1::($-1)~$

Try it online!

Way too long for a language that this is supposed to be easy in, guess I should have implemented unary subtract-one and add-one operators.

Explanation

=1            First term in sequence is 1
  ::          Mode: for input n, output first n terms in sequence
              For each term in the sequence:
    ($-1)      Subtract 1 from the current index
         ~$    Concatenate that to the current index
\$\endgroup\$
1
  • \$\begingroup\$ Note current version uses & instead of ::, saving 1 byte \$\endgroup\$
    – Stephen
    Commented Feb 1, 2019 at 4:53
1
\$\begingroup\$

APL (Dyalog Classic), 13 bytes

2{⍎∊⍕¨⍺⍵}/0,⍳

Try it online!

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

Canvas, 6 bytes

ŗ²;+┤]

Try it here!

Explanation:

{     ]  map over 1..input
 ŗ         convert to string (required as `+` needs 1 arg to be a string to not add)
  ²;       place a 0-indexed version of the index below TOS
    +      join the two
     ┤     cast to number

ŗ²×┤] (or even ²×┤]) would work if I didn't try to push the absolute most out of characters and didn't make × - reverse add - do not that when not receiving 2 strings.

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

Red, 56 bytes

func[n][prin"1 "repeat i n - 1[prin rejoin[i i + 1" "]]]

Try it online!

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

Rust, 56 bytes

|n|(0..n).map(|n|format!("{}{}",n,n+1).parse().unwrap())

Try it online!

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

Java, 48 bytes

String f(int n){return--n>0?f(n)+","+n+-~n:"1";}

Port of tsh's JavaScript answer. Try it online here.

Ungolfed:

String f(int n) { // recursive function taking an integer as argument and returning a String
    return --n > 0 ? f(n) // decrement n and recurse if n is still positive after
                   + "," + n // return the result of the recursive call concatenated with n and ...
                   + (- ~n) // ... n+1; writing it as -~n gives it precedence over the concatenation
                   : "1"; // if n is now 0 on the other hand, return "1"
}
\$\endgroup\$
1
\$\begingroup\$

MATL, 9 bytes

:"@qV@VhU

Try it online!

              % implicit input n
:             % range, push 1..n
"             % for loop:
 @            % push for loop index
 q            % decrement
 V            % convert to string (num2str)
 @            % push for loop index
 V            % convert to string (num2str)
 h            % horizontally concatenate
 U            % convert to number (str2num)
              % implicit end of for loop
              % implicit end of program, display stack contents
\$\endgroup\$
1
\$\begingroup\$

Panacea, 6 bytes

re
D>j

Explanation:

r        Range [0.. input]
 e       Map each element with the following line
D        Duplicate
 >       Increment
  j      Join digits; multiply by ten and add
         Since this works with integers, it removes the preceding 0.
\$\endgroup\$
1
\$\begingroup\$

Japt, 5 bytes

õÈsiY

Try it


Explanation

          :Implicit input of integer U
õ         :Range [1,U]
 È        :Map each integer at index Y
  s       :  Convert to string
   iY     :  Prepend index
          :  Implicitly convert back to integer
          :Implicit output of resulting array

Alternative

Ç°s+Z

Try it

          :Implicit input of integer U
Ç         :Map each integer Z in the range [0,U)
 °        :  Postfix increment
  s       :  Convert to string
   +Z     :  Append (the now incremented) Z
          :  Implicitly convert back to integer
          :Implicit output of resulting array
\$\endgroup\$
1
  • \$\begingroup\$ an OEIS sequence answer with oeis in it \$\endgroup\$
    – Razetime
    Commented Oct 2, 2020 at 16:46
1
\$\begingroup\$

J, 20 bytes

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

Try it online!

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

Forth (gforth), 33 bytes

: f dup 1 ?do i . i 1 .r loop . ;

Try it online!

Explanation

Loops through all numbers in range and outputs the number twice, the first with a space appended, and the second without.

This results in the first output being appended to the previous output. To fix the last case, the input is outputted so that it can be appended to the last number in the loop.

Code Explanation

: f                   \ start a new word definition
  dup 1               \ duplicate the input and put a 1 on the stack
  ?do                 \ begin counted loop from 1 to n (does nothing if 1 == n)
    i .               \ output loop index with a space appended
    i 1 .r            \ output loop index right-aligned with minimum 1 character length
  loop                \ end the loop
  .                   \ output the input we duplicated earlier
;                     \ end the word definition
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 24 21 bytes

@DomHastings came up with a way to save 3 bytes

say$i++.$i|0for 1..<>

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Nice! Much shorter than my first approach using a while and `` $\ ``! Using yours as a base though, you can save a couple of bytes using eval and x<>: Try it online! or even Try it online! \$\endgroup\$ Commented Jul 4, 2018 at 11:44
  • \$\begingroup\$ Wait, combining the two gives 21: Try it online! \$\endgroup\$ Commented Jul 4, 2018 at 11:46
1
\$\begingroup\$

Excel VBA, 29 bytes

An anonymous VBE immediate window function that takes input from cell [A1] and outputs to the console.

?1:For i=2To[A1]:?i-1 &i:Next
\$\endgroup\$
4
  • \$\begingroup\$ I've always wanted to use Excel VBA for one of these. I have SO much to learn! \$\endgroup\$ Commented Jul 6, 2018 at 15:16
  • \$\begingroup\$ @seadoggie01 Its a great way to get more familiar with the language - and fortunately, VBA has a very well made up Tips Page that can help you to get started :) \$\endgroup\$ Commented Jul 6, 2018 at 16:17
  • \$\begingroup\$ @seadoggie01 oh, and if you are interested in VBA answers, I have a couple that I am really quite proud of, such as Let's Draw Mona Lisa, Embiggen your Input, and Visualize Visual Eyes \$\endgroup\$ Commented Jul 6, 2018 at 16:20
  • 1
    \$\begingroup\$ Ha! The first thing I found after this question was the Tips page :) \$\endgroup\$ Commented Jul 6, 2018 at 16:29
1
\$\begingroup\$

Yabasic, 37 bytes

An anonymous function that takes input from STDIN and outputs to STDOUT.

Input""n
?1
For i=2TO n?i-1,"",i
Next

Try it online!

-14 bytes thanks to @ErikF

\$\endgroup\$
2
  • \$\begingroup\$ You can get it down to 31 bytes by not counting the input assignment and making use of , between output fields in PRINT to combine them: Try it online! \$\endgroup\$
    – ErikF
    Commented Jul 6, 2018 at 8:42
  • \$\begingroup\$ @ErikF, input must be taken explicitly per community rules and cannot be taken from a predefined variable. That said I had never seen the ?n,"",m trick - that is really quite clever. \$\endgroup\$ Commented Jul 6, 2018 at 10:56
1
\$\begingroup\$

dc, 34 bytes

sj1psi[lid1+dsiZAr^*li+pzlj>M]dsMx

Try it online!

Completely different (and less golfy, unfortunately) approach than Sophia's dc answer. I juggle a lot of register activity here that I wish I could cut down, seems the likeliest way to golf a couple of bytes. Expects input as the sole stack entry.

sj1psi stores the input into register j, prints the initial 1, and stores 1 in register i. Macro M duplicates i, increments it, and then multiplies it by 10 to the power of however many digits the incremented value has (ZAr^*). Fetches the newly incremented version, and adds the two together. Compares register j to the number of entries on the stack, and runs until they match.

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

dc, 29 bytes

[d1-d0<F]dsFx+p[npz1<G]sGz1<G

Try it online!

This is a good showing for dc because its printing capabilities are so limited, but line up perfectly with the challenge spec. Input is from the stack (and must be the only thing on the stack), output is to stdout. There's some duplication near the end I can't figure out how to get rid of without breaking the case n=1

Explanation

F will be the macro [d1-d0<F]
d        copy the top of the stack
 1-      decrement
   d0<F  repeat (tail recursion) until a 0 is on top

G will be the macro [npz1<G]
n        print the top (pop, but no newline)
 p       print the new top (newline, but no pop)
  z1<G   repeat (tail recursion) until only one number is on the stack

Full program (example input: 4)
[d1-d0<F]dsFx      Store F and run it: new stack is 0 1 2 3 4
+                  Drop the zero (can't just recurse to 1, or n=1 breaks)
p                  Print (no pop) the '1': stack is 1 2 3 4
[npz1<G]sG        Store G
z1<G               Run G if there's more than one number on the stack
\$\endgroup\$
1
\$\begingroup\$

Momema, 36 bytes

z0w+1=*0-8*0w00+1*0-8*0-9 10z=+_M-*0

Try it online! Requires the -i interpreter flag.

Explanation

                                                     #  i = 0
d   0            #  d0:  nop                         #  do {
i   (+ 1 =*0)    #  i0:  jump to i(![0])             #    if i {
-8  *0           #       print [0]                   #      print i
i   0            #  i1:  nop                         #    }
0   (+ 1 *0)     #       [0] = [0] + 1               #    i += 1
-8  *0           #       print [0]                   #    print i
-9  10           #       print chr(10)               #    print \n
d   =(+ _M -*0)  #  d1:  jump to d(!!(input - [0]))  #  } while (input - i != 0)

The Momema reference interpreter provides an "interactive mode", enabled by -i, which is intended to be used on the command line.

One of the features it allows is the ability to add holes, denoted by _, which effectively allow one to substitute an integer read from STDIN into an expression. This is already shorter than *-8 (read from memory-mapped I/O location -8), which does the same thing without interactive mode and without displaying a prompt to STDERR.

Crucially, holes can also be named with a sequence of capital letters after the _. Evaluation of named holes is memoized. Input will be read the first time a named hole is evaluated but subsequent evaluations of a hole with the same name will reuse the input number. This means that we can use _M to stand in for "the input", but input will only be read on the first iteration of the loop.

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

D, 93 bytes

import std.math;_[]f(_)(_ n){_[]k;foreach(i;0..n)k~=++i+10^^(cast(_)i--.log10+1)*i;return k;}

Try it online!

I couldn't find a nice (short) solution that evades an import, and this was the shortest of the bunch.

\$\endgroup\$
3
  • \$\begingroup\$ You can save two bytes by making auto be _[] \$\endgroup\$
    – Adalynn
    Commented Jul 8, 2018 at 16:15
  • \$\begingroup\$ @Zacharý Good point! \$\endgroup\$ Commented Jul 8, 2018 at 17:21
  • \$\begingroup\$ WOuldn't it just be easier to do std.conv trickery? \$\endgroup\$
    – Adalynn
    Commented Jul 11, 2018 at 2:04
1
\$\begingroup\$

MATLAB 45 63 bytes

@(j)eval('for a=0:j-1;disp([num2str(a)*a/a,num2str(a+1)]);end')

Somewhat ugly *a/a in order to avoid printing leading zero for the first element

Fixed by wrapping into disp and anonymous function

\$\endgroup\$
4
  • \$\begingroup\$ You can't assume the input is predefined as a variable unfortunately. You have to do j=input(''); or create a function. Anyway, +1, as I'm assuming you'll fix it. Anonymous function with eval is often shortest when loops are involved. \$\endgroup\$ Commented Jul 8, 2018 at 17:47
  • \$\begingroup\$ @StewieGriffin thanks for advice, i am still learning ways around golfing \$\endgroup\$ Commented Jul 8, 2018 at 18:43
  • \$\begingroup\$ @StewieGriffin by anonymous function + eval you mean something like f = @(j) eval('for ..... end') ? \$\endgroup\$ Commented Jul 8, 2018 at 18:53
  • 1
    \$\begingroup\$ Yes :-) you don't need to include f= in the byte count, since the function can be used unnamed like: ans(6). \$\endgroup\$ Commented Jul 8, 2018 at 19:57
1
\$\begingroup\$

DC, 33 bytes

si[lidZAr^spd1-dsilp*+li0<d]dsdxf

Explanation

si[lidZAr^spd1-dsilp*+li0<d]dsdxf  Whole program.
si                                 Save the value on the stack to the i register.
  [                        ]dsdx   Create a macro, duplicate it, store it in the d register, and execute it.
   li                              Put the value from the i register on the stack.
     dZ                            Duplicate the top value, and change it to the number of digits it has.
       Ar                          Push 10 on the stack, and reverse the top two values.
         ^sp                       Do 10^x, and store it in the p register.
            d1-                    Duplicate the top value, and subtract 1 from it.
               dsi                 Duplicate the top value, and store it in the i register.
                  lp*+             Put the value from the p register on the stack, and multiply it by the the value we just put in the i register. Then, add the top two values together.
                      li0<d        Put the value from the i register and then 0 on the stack. If 0 < li, then we run the d macro again.
                                f  Print the stack.

Input is the number of numbers to be generated, on the stack.

Output is printed.

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – DLosc
    Commented Jul 12, 2018 at 20:16
  • \$\begingroup\$ Thanks! I've been lurking for a while, figured I'd finally give it a shot! \$\endgroup\$
    – FlexEast
    Commented Jul 12, 2018 at 20:17
1
\$\begingroup\$

QBasic, 60 53 bytes

INPUT n
?1
FOR i=2TO n
?STR$(i-1)MID$(STR$(i),2)
NEXT

A math-based solution, because converting numbers to strings in QBasic is a lot more complicated than it ought to be. Nope, strings are still shorter, you just have to special-case the first item. The problem with STR$(i) is that it adds a space to the start of positive numbers, so we take all but the first character by using MID$. Other than that, it's pretty straightforward.

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

Kotlin, 48 bytes

{"1, "+(2..it).map{"$it${it+1}"}.joinToString()}

Yes I know, I could save 1 more byte by removing the blank behind the first comma, but it looks nicer with it :-)

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.