New Neighbour Sequence
Clash 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.
code-golf sequence integer
add a comment |Â
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.
code-golf sequence integer
Sandbox post (deleted)
â Laikoni
1 hour ago
add a comment |Â
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.
code-golf sequence integer
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
code-golf sequence integer
asked 1 hour ago
Laikoni
19.1k43594
19.1k43594
Sandbox post (deleted)
â Laikoni
1 hour ago
add a comment |Â
Sandbox post (deleted)
â Laikoni
1 hour ago
Sandbox post (deleted)
â Laikoni
1 hour ago
Sandbox post (deleted)
â Laikoni
1 hour ago
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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.
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
up vote
2
down vote
Python 2, 20 bytes
lambda n:2*n%5+n/5*5
Try it online!
add a comment |Â
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!
Python 2, 20 bytes
lambda n:2*n%5+n/5*5
Try it online!
answered 1 hour ago
Chas Brown
4,5061419
4,5061419
add a comment |Â
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
JavaScript (ES6), 13 bytes
Returns the $n$th term of the sequence.
n=>n-2-~++n%5
Try it online!
edited 27 mins ago
answered 39 mins ago
Arnauld
66.8k584281
66.8k584281
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
Wolfram Language (Mathematica), 14 bytes
#+Mod[#,5,-2]&
Try it online!
Prints the n-th, zero-indexed, integer in the sequence.
answered 1 hour ago
Misha Lavrov
3,617320
3,617320
add a comment |Â
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
Common Lisp, 67 bytes
(defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))
Try it online!
answered 17 mins ago
JRowan
1814
1814
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sandbox post (deleted)
â Laikoni
1 hour ago