New Neighbour Sequence

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












The non-negative integers are bored of always having the same two* neighbours, so they decide to mix things up a little. However, they are also lazy and want to stay as close as possible to their original position.



They come up with the following algorithm:



  • The first element is 0.

  • The $n^th$ element is the smallest number which is not yet present in the sequence and which is not a neighbour of the $(n-1)^th$ element.

This generates the following infinite sequence:



0,2,4,1,3,5,7,9,6,8,10,12,14,11,13,15,17,19,16,18,20,22,24,21,23,25,27,29,26,28 ...


0 is the first element. 1 is the smallest number not yet in the sequence, but it is a neighbour of 0. The next smallest number is 2, so it is the second element of the sequence. Now the remaining numbers are 1,3,4,5,6,..., but as both 1 and 3 are neighbours of 2, 4 is the third member of the sequence. As 1 is not a neighbour of 4, it can finally take its place as fourth element.



The Task



Write a function or program in as few bytes as possible which generates the above sequence.



You may



  • output the sequence infinitely,

  • take an input $n$ and return the $n^th$ element of the sequence, or

  • take an input $n$ and return the first $n$ elements of the sequence.

Both zero- or one-indexing is fine in case you choose one of the two later options.



You don't need to follow the algorithm given above, any method which produces the same sequence is fine.




Inspired by Code golf the best permutation. Turns out this is A277618.
* Zero has literally only one neighbour and doesn't really care.










share|improve this question





















  • Sandbox post (deleted)
    – Laikoni
    1 hour ago














up vote
5
down vote

favorite












The non-negative integers are bored of always having the same two* neighbours, so they decide to mix things up a little. However, they are also lazy and want to stay as close as possible to their original position.



They come up with the following algorithm:



  • The first element is 0.

  • The $n^th$ element is the smallest number which is not yet present in the sequence and which is not a neighbour of the $(n-1)^th$ element.

This generates the following infinite sequence:



0,2,4,1,3,5,7,9,6,8,10,12,14,11,13,15,17,19,16,18,20,22,24,21,23,25,27,29,26,28 ...


0 is the first element. 1 is the smallest number not yet in the sequence, but it is a neighbour of 0. The next smallest number is 2, so it is the second element of the sequence. Now the remaining numbers are 1,3,4,5,6,..., but as both 1 and 3 are neighbours of 2, 4 is the third member of the sequence. As 1 is not a neighbour of 4, it can finally take its place as fourth element.



The Task



Write a function or program in as few bytes as possible which generates the above sequence.



You may



  • output the sequence infinitely,

  • take an input $n$ and return the $n^th$ element of the sequence, or

  • take an input $n$ and return the first $n$ elements of the sequence.

Both zero- or one-indexing is fine in case you choose one of the two later options.



You don't need to follow the algorithm given above, any method which produces the same sequence is fine.




Inspired by Code golf the best permutation. Turns out this is A277618.
* Zero has literally only one neighbour and doesn't really care.










share|improve this question





















  • Sandbox post (deleted)
    – Laikoni
    1 hour ago












up vote
5
down vote

favorite









up vote
5
down vote

favorite











The non-negative integers are bored of always having the same two* neighbours, so they decide to mix things up a little. However, they are also lazy and want to stay as close as possible to their original position.



They come up with the following algorithm:



  • The first element is 0.

  • The $n^th$ element is the smallest number which is not yet present in the sequence and which is not a neighbour of the $(n-1)^th$ element.

This generates the following infinite sequence:



0,2,4,1,3,5,7,9,6,8,10,12,14,11,13,15,17,19,16,18,20,22,24,21,23,25,27,29,26,28 ...


0 is the first element. 1 is the smallest number not yet in the sequence, but it is a neighbour of 0. The next smallest number is 2, so it is the second element of the sequence. Now the remaining numbers are 1,3,4,5,6,..., but as both 1 and 3 are neighbours of 2, 4 is the third member of the sequence. As 1 is not a neighbour of 4, it can finally take its place as fourth element.



The Task



Write a function or program in as few bytes as possible which generates the above sequence.



You may



  • output the sequence infinitely,

  • take an input $n$ and return the $n^th$ element of the sequence, or

  • take an input $n$ and return the first $n$ elements of the sequence.

Both zero- or one-indexing is fine in case you choose one of the two later options.



You don't need to follow the algorithm given above, any method which produces the same sequence is fine.




Inspired by Code golf the best permutation. Turns out this is A277618.
* Zero has literally only one neighbour and doesn't really care.










share|improve this question













The non-negative integers are bored of always having the same two* neighbours, so they decide to mix things up a little. However, they are also lazy and want to stay as close as possible to their original position.



They come up with the following algorithm:



  • The first element is 0.

  • The $n^th$ element is the smallest number which is not yet present in the sequence and which is not a neighbour of the $(n-1)^th$ element.

This generates the following infinite sequence:



0,2,4,1,3,5,7,9,6,8,10,12,14,11,13,15,17,19,16,18,20,22,24,21,23,25,27,29,26,28 ...


0 is the first element. 1 is the smallest number not yet in the sequence, but it is a neighbour of 0. The next smallest number is 2, so it is the second element of the sequence. Now the remaining numbers are 1,3,4,5,6,..., but as both 1 and 3 are neighbours of 2, 4 is the third member of the sequence. As 1 is not a neighbour of 4, it can finally take its place as fourth element.



The Task



Write a function or program in as few bytes as possible which generates the above sequence.



You may



  • output the sequence infinitely,

  • take an input $n$ and return the $n^th$ element of the sequence, or

  • take an input $n$ and return the first $n$ elements of the sequence.

Both zero- or one-indexing is fine in case you choose one of the two later options.



You don't need to follow the algorithm given above, any method which produces the same sequence is fine.




Inspired by Code golf the best permutation. Turns out this is A277618.
* Zero has literally only one neighbour and doesn't really care.







code-golf sequence integer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Laikoni

19.1k43594




19.1k43594











  • Sandbox post (deleted)
    – Laikoni
    1 hour ago
















  • Sandbox post (deleted)
    – Laikoni
    1 hour ago















Sandbox post (deleted)
– Laikoni
1 hour ago




Sandbox post (deleted)
– Laikoni
1 hour ago










4 Answers
4






active

oldest

votes

















up vote
2
down vote














Python 2, 20 bytes





lambda n:2*n%5+n/5*5


Try it online!






share|improve this answer



























    up vote
    1
    down vote













    JavaScript (ES6), 13 bytes



    Returns the $n$th term of the sequence.





    n=>n-2-~++n%5


    Try it online!






    share|improve this answer





























      up vote
      0
      down vote














      Wolfram Language (Mathematica), 14 bytes



      #+Mod[#,5,-2]&


      Try it online!



      Prints the n-th, zero-indexed, integer in the sequence.






      share|improve this answer



























        up vote
        0
        down vote














        Common Lisp, 67 bytes





        (defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))


        Try it online!






        share|improve this answer




















          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "200"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174706%2fnew-neighbour-sequence%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote














          Python 2, 20 bytes





          lambda n:2*n%5+n/5*5


          Try it online!






          share|improve this answer
























            up vote
            2
            down vote














            Python 2, 20 bytes





            lambda n:2*n%5+n/5*5


            Try it online!






            share|improve this answer






















              up vote
              2
              down vote










              up vote
              2
              down vote










              Python 2, 20 bytes





              lambda n:2*n%5+n/5*5


              Try it online!






              share|improve this answer













              Python 2, 20 bytes





              lambda n:2*n%5+n/5*5


              Try it online!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              Chas Brown

              4,5061419




              4,5061419




















                  up vote
                  1
                  down vote













                  JavaScript (ES6), 13 bytes



                  Returns the $n$th term of the sequence.





                  n=>n-2-~++n%5


                  Try it online!






                  share|improve this answer


























                    up vote
                    1
                    down vote













                    JavaScript (ES6), 13 bytes



                    Returns the $n$th term of the sequence.





                    n=>n-2-~++n%5


                    Try it online!






                    share|improve this answer
























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      JavaScript (ES6), 13 bytes



                      Returns the $n$th term of the sequence.





                      n=>n-2-~++n%5


                      Try it online!






                      share|improve this answer














                      JavaScript (ES6), 13 bytes



                      Returns the $n$th term of the sequence.





                      n=>n-2-~++n%5


                      Try it online!







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 27 mins ago

























                      answered 39 mins ago









                      Arnauld

                      66.8k584281




                      66.8k584281




















                          up vote
                          0
                          down vote














                          Wolfram Language (Mathematica), 14 bytes



                          #+Mod[#,5,-2]&


                          Try it online!



                          Prints the n-th, zero-indexed, integer in the sequence.






                          share|improve this answer
























                            up vote
                            0
                            down vote














                            Wolfram Language (Mathematica), 14 bytes



                            #+Mod[#,5,-2]&


                            Try it online!



                            Prints the n-th, zero-indexed, integer in the sequence.






                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote










                              Wolfram Language (Mathematica), 14 bytes



                              #+Mod[#,5,-2]&


                              Try it online!



                              Prints the n-th, zero-indexed, integer in the sequence.






                              share|improve this answer













                              Wolfram Language (Mathematica), 14 bytes



                              #+Mod[#,5,-2]&


                              Try it online!



                              Prints the n-th, zero-indexed, integer in the sequence.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              Misha Lavrov

                              3,617320




                              3,617320




















                                  up vote
                                  0
                                  down vote














                                  Common Lisp, 67 bytes





                                  (defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))


                                  Try it online!






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote














                                    Common Lisp, 67 bytes





                                    (defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))


                                    Try it online!






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote










                                      Common Lisp, 67 bytes





                                      (defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))


                                      Try it online!






                                      share|improve this answer













                                      Common Lisp, 67 bytes





                                      (defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))


                                      Try it online!







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 17 mins ago









                                      JRowan

                                      1814




                                      1814



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174706%2fnew-neighbour-sequence%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Popular posts from this blog

                                          How to check contact read email or not when send email to Individual?

                                          Displaying single band from multi-band raster using QGIS

                                          How many registers does an x86_64 CPU actually have?