New Order #6: Easter Egg
$begingroup$
Introduction (may be ignored)
Putting all positive integers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive integers. This is the sixth challenge in this series (links to the first, second, third, fourth and fifth challenge).
This challenge has a mild Easter theme (because it's Easter). I took my inspiration from this highly decorated (and in my personal opinion rather ugly) goose egg.

It reminded me of the Ulam spiral, where all positive integers are placed in a counter-clockwise spiral. This spiral has some interesting features related to prime numbers, but that's not relevant for this challenge.

We get to this challenge's permutation of positive integers if we take the numbers in the Ulam spiral and trace all integers in a clockwise turning spiral, starting at 1. This way, we get:
1, 6, 5, 4, 3, 2, 9, 8, 7, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 25, 24, 23, etc.
If you would draw both of the spirals, you'd get some sort of an infinite mesh of (egg shell) spirals (note the New Order reference there).
This sequence is present in the OEIS under number A090861. Since this is a "pure sequence" challenge, the task is to output $a(n)$ for a given $n$ as input, where $a(n)$ is A090861.
Task
Given an integer input $n$, output $a(n)$ in integer format, where $a(n)$ is A090861.
Note: 1-based indexing is assumed here; you may use 0-based indexing, so $a(0) = 1; a(1) = 6$, etc. Please mention this in your answer if you choose to use this.
Test cases
Input | Output
---------------
1 | 1
5 | 3
20 | 10
50 | 72
78 | 76
123 | 155
1234 | 1324
3000 | 2996
9999 | 9903
29890 | 29796
Rules
- Input and output are integers.
- Your program should at least support input in the range of 1 up to 32767).
- Invalid input (0, floats, strings, negative values, etc.) may lead to unpredicted output, errors or (un)defined behaviour.
- Default I/O rules apply.
Default loopholes are forbidden.- This is code-golf, so the shortest answers in bytes wins
code-golf sequence
$endgroup$
add a comment |
$begingroup$
Introduction (may be ignored)
Putting all positive integers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive integers. This is the sixth challenge in this series (links to the first, second, third, fourth and fifth challenge).
This challenge has a mild Easter theme (because it's Easter). I took my inspiration from this highly decorated (and in my personal opinion rather ugly) goose egg.

It reminded me of the Ulam spiral, where all positive integers are placed in a counter-clockwise spiral. This spiral has some interesting features related to prime numbers, but that's not relevant for this challenge.

We get to this challenge's permutation of positive integers if we take the numbers in the Ulam spiral and trace all integers in a clockwise turning spiral, starting at 1. This way, we get:
1, 6, 5, 4, 3, 2, 9, 8, 7, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 25, 24, 23, etc.
If you would draw both of the spirals, you'd get some sort of an infinite mesh of (egg shell) spirals (note the New Order reference there).
This sequence is present in the OEIS under number A090861. Since this is a "pure sequence" challenge, the task is to output $a(n)$ for a given $n$ as input, where $a(n)$ is A090861.
Task
Given an integer input $n$, output $a(n)$ in integer format, where $a(n)$ is A090861.
Note: 1-based indexing is assumed here; you may use 0-based indexing, so $a(0) = 1; a(1) = 6$, etc. Please mention this in your answer if you choose to use this.
Test cases
Input | Output
---------------
1 | 1
5 | 3
20 | 10
50 | 72
78 | 76
123 | 155
1234 | 1324
3000 | 2996
9999 | 9903
29890 | 29796
Rules
- Input and output are integers.
- Your program should at least support input in the range of 1 up to 32767).
- Invalid input (0, floats, strings, negative values, etc.) may lead to unpredicted output, errors or (un)defined behaviour.
- Default I/O rules apply.
Default loopholes are forbidden.- This is code-golf, so the shortest answers in bytes wins
code-golf sequence
$endgroup$
add a comment |
$begingroup$
Introduction (may be ignored)
Putting all positive integers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive integers. This is the sixth challenge in this series (links to the first, second, third, fourth and fifth challenge).
This challenge has a mild Easter theme (because it's Easter). I took my inspiration from this highly decorated (and in my personal opinion rather ugly) goose egg.

It reminded me of the Ulam spiral, where all positive integers are placed in a counter-clockwise spiral. This spiral has some interesting features related to prime numbers, but that's not relevant for this challenge.

We get to this challenge's permutation of positive integers if we take the numbers in the Ulam spiral and trace all integers in a clockwise turning spiral, starting at 1. This way, we get:
1, 6, 5, 4, 3, 2, 9, 8, 7, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 25, 24, 23, etc.
If you would draw both of the spirals, you'd get some sort of an infinite mesh of (egg shell) spirals (note the New Order reference there).
This sequence is present in the OEIS under number A090861. Since this is a "pure sequence" challenge, the task is to output $a(n)$ for a given $n$ as input, where $a(n)$ is A090861.
Task
Given an integer input $n$, output $a(n)$ in integer format, where $a(n)$ is A090861.
Note: 1-based indexing is assumed here; you may use 0-based indexing, so $a(0) = 1; a(1) = 6$, etc. Please mention this in your answer if you choose to use this.
Test cases
Input | Output
---------------
1 | 1
5 | 3
20 | 10
50 | 72
78 | 76
123 | 155
1234 | 1324
3000 | 2996
9999 | 9903
29890 | 29796
Rules
- Input and output are integers.
- Your program should at least support input in the range of 1 up to 32767).
- Invalid input (0, floats, strings, negative values, etc.) may lead to unpredicted output, errors or (un)defined behaviour.
- Default I/O rules apply.
Default loopholes are forbidden.- This is code-golf, so the shortest answers in bytes wins
code-golf sequence
$endgroup$
Introduction (may be ignored)
Putting all positive integers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive integers. This is the sixth challenge in this series (links to the first, second, third, fourth and fifth challenge).
This challenge has a mild Easter theme (because it's Easter). I took my inspiration from this highly decorated (and in my personal opinion rather ugly) goose egg.

It reminded me of the Ulam spiral, where all positive integers are placed in a counter-clockwise spiral. This spiral has some interesting features related to prime numbers, but that's not relevant for this challenge.

We get to this challenge's permutation of positive integers if we take the numbers in the Ulam spiral and trace all integers in a clockwise turning spiral, starting at 1. This way, we get:
1, 6, 5, 4, 3, 2, 9, 8, 7, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 25, 24, 23, etc.
If you would draw both of the spirals, you'd get some sort of an infinite mesh of (egg shell) spirals (note the New Order reference there).
This sequence is present in the OEIS under number A090861. Since this is a "pure sequence" challenge, the task is to output $a(n)$ for a given $n$ as input, where $a(n)$ is A090861.
Task
Given an integer input $n$, output $a(n)$ in integer format, where $a(n)$ is A090861.
Note: 1-based indexing is assumed here; you may use 0-based indexing, so $a(0) = 1; a(1) = 6$, etc. Please mention this in your answer if you choose to use this.
Test cases
Input | Output
---------------
1 | 1
5 | 3
20 | 10
50 | 72
78 | 76
123 | 155
1234 | 1324
3000 | 2996
9999 | 9903
29890 | 29796
Rules
- Input and output are integers.
- Your program should at least support input in the range of 1 up to 32767).
- Invalid input (0, floats, strings, negative values, etc.) may lead to unpredicted output, errors or (un)defined behaviour.
- Default I/O rules apply.
Default loopholes are forbidden.- This is code-golf, so the shortest answers in bytes wins
code-golf sequence
code-golf sequence
asked 5 hours ago
agtoeveragtoever
1,431425
1,431425
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 60 bytes
8(s=⌊(⌊Sqrt[#-1]⌋+1)/2⌋)^2-#+2+If[#<=4s^2+2s,-2,6]s&
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 46 45 41 bytes
0-indexed.
n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 16 14 11 bytes
Ḃ¬+×)RUẎQị@
A monadic Link accepting an integer, n, which yields an integer, a(n).
Try it online! (very inefficient since it goes out to layer $lceilfrac n2rceil$)
A 14-byte version, ½‘Ḃ¬+×Ʋ€RUẎQị@, completes all but the largest test case in under 30s - Try it at TIO - this limits the layers used to $lceilfrac{lfloorsqrt nrfloor+1}2rceil$.
How?
The permutation is to take the natural numbers in reversed slices of lengths [1,5,3,11,5,17,7,23,9,29,11,35,13,...] - the odd positive integers interspersed with the positive integers congruent to five modulo six, i.e [1, 2*3-1, 3, 4*3-1, 5, 6*3-1, 7, 8*3-1, 9, ...].
This is the same as concatenating and then deduplicating reversed ranges [1..x] of where x is the cumulative sums of these slice lengths (i.e. the maximum of each slice) - [1,6,9,20,25,42,49,72,81,110,121,156,169,...], which is the odd integers squared interspersed with even numbers multiplied by themselves incremented, i.e. [1*1, 2*3, 3*3, 4*5, 5*5, 6*7, 7*7,...].
Ḃ¬+×)RUẎQị@ - Link: integer, n e.g. 10
) - for each v in [1..n]: vs = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
Ḃ - modulo 2 [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
¬ - logical NOT [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
+ - add (v) [ 1, 3, 3, 5, 5, 7, 7, 9, 9,11]
× - multiply (by v) [ 1, 6, 9,20,25,42,49,72,81,110]
R - range (vectorises) [[1],[1..6],[1..9],[1..20],...,[1..110]]
U - upend [[1],[6..1],[9..1],[20..1],...,[110..1]]
Ẏ - tighten [1,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,20,...,1,...,110,...,1]
Q - de-duplicate [1,6,5,4,3,2,9,8,7,20,...,110,...82]
@ - with swapped arguments:
ị - (n) index into 20
$endgroup$
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 67 bytes
n=>8*(x=(int)Math.Sqrt(--n)+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n;int x;
Try it online!
$endgroup$
add a comment |
$begingroup$
MATL, 12 bytes
Eq1YL!tPwG=)
Try it online!
Very memory-inefficient. Prepending X^k makes it more efficient.
$endgroup$
add a comment |
$begingroup$
Python 3.8, 104 74 65 60 57 bytes
lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x
Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!
This solution uses 0-based indexing.
$endgroup$
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using>in place of<=andx*xin place ofx**2...like so:def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n...TIO
$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
add a comment |
$begingroup$
Japt, 15 bytes
Port of Jonathan's Jelly solution. 1-indexed.
gUòÈ+v)*X õÃcÔâ
Try it
gUòÈ+v)*X õÃcÔâ :Implicit input of integer U
g :Index into
Uò : Range [0,U]
È : Map each X
+v : Add 1 if divisible by 2
) : Group result
*X : Multiply by X
õ : Range [1,result]
à : End map
c : Flatten
Ô : After reversing each
â : Deduplicate
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 53 bytes
A direct port of Arnauld's JavaScript answer, go upvote that, and/or J42161217's Mathematica answer, and/or Kapocsi's Python answer :)
lambda n:((x:=int(n**.5+1)&-2)*2-(n<x*x+x)*4+3)*x+1-n
0-indexed.
Try it online!
$endgroup$
add a comment |
Your Answer
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f183528%2fnew-order-6-easter-egg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 60 bytes
8(s=⌊(⌊Sqrt[#-1]⌋+1)/2⌋)^2-#+2+If[#<=4s^2+2s,-2,6]s&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 60 bytes
8(s=⌊(⌊Sqrt[#-1]⌋+1)/2⌋)^2-#+2+If[#<=4s^2+2s,-2,6]s&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 60 bytes
8(s=⌊(⌊Sqrt[#-1]⌋+1)/2⌋)^2-#+2+If[#<=4s^2+2s,-2,6]s&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 60 bytes
8(s=⌊(⌊Sqrt[#-1]⌋+1)/2⌋)^2-#+2+If[#<=4s^2+2s,-2,6]s&
Try it online!
answered 5 hours ago
J42161217J42161217
14.3k21354
14.3k21354
add a comment |
add a comment |
$begingroup$
JavaScript (ES7), 46 45 41 bytes
0-indexed.
n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 46 45 41 bytes
0-indexed.
n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 46 45 41 bytes
0-indexed.
n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n
Try it online!
$endgroup$
JavaScript (ES7), 46 45 41 bytes
0-indexed.
n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n
Try it online!
edited 4 hours ago
answered 5 hours ago
ArnauldArnauld
81.7k798337
81.7k798337
add a comment |
add a comment |
$begingroup$
Jelly, 16 14 11 bytes
Ḃ¬+×)RUẎQị@
A monadic Link accepting an integer, n, which yields an integer, a(n).
Try it online! (very inefficient since it goes out to layer $lceilfrac n2rceil$)
A 14-byte version, ½‘Ḃ¬+×Ʋ€RUẎQị@, completes all but the largest test case in under 30s - Try it at TIO - this limits the layers used to $lceilfrac{lfloorsqrt nrfloor+1}2rceil$.
How?
The permutation is to take the natural numbers in reversed slices of lengths [1,5,3,11,5,17,7,23,9,29,11,35,13,...] - the odd positive integers interspersed with the positive integers congruent to five modulo six, i.e [1, 2*3-1, 3, 4*3-1, 5, 6*3-1, 7, 8*3-1, 9, ...].
This is the same as concatenating and then deduplicating reversed ranges [1..x] of where x is the cumulative sums of these slice lengths (i.e. the maximum of each slice) - [1,6,9,20,25,42,49,72,81,110,121,156,169,...], which is the odd integers squared interspersed with even numbers multiplied by themselves incremented, i.e. [1*1, 2*3, 3*3, 4*5, 5*5, 6*7, 7*7,...].
Ḃ¬+×)RUẎQị@ - Link: integer, n e.g. 10
) - for each v in [1..n]: vs = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
Ḃ - modulo 2 [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
¬ - logical NOT [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
+ - add (v) [ 1, 3, 3, 5, 5, 7, 7, 9, 9,11]
× - multiply (by v) [ 1, 6, 9,20,25,42,49,72,81,110]
R - range (vectorises) [[1],[1..6],[1..9],[1..20],...,[1..110]]
U - upend [[1],[6..1],[9..1],[20..1],...,[110..1]]
Ẏ - tighten [1,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,20,...,1,...,110,...,1]
Q - de-duplicate [1,6,5,4,3,2,9,8,7,20,...,110,...82]
@ - with swapped arguments:
ị - (n) index into 20
$endgroup$
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
add a comment |
$begingroup$
Jelly, 16 14 11 bytes
Ḃ¬+×)RUẎQị@
A monadic Link accepting an integer, n, which yields an integer, a(n).
Try it online! (very inefficient since it goes out to layer $lceilfrac n2rceil$)
A 14-byte version, ½‘Ḃ¬+×Ʋ€RUẎQị@, completes all but the largest test case in under 30s - Try it at TIO - this limits the layers used to $lceilfrac{lfloorsqrt nrfloor+1}2rceil$.
How?
The permutation is to take the natural numbers in reversed slices of lengths [1,5,3,11,5,17,7,23,9,29,11,35,13,...] - the odd positive integers interspersed with the positive integers congruent to five modulo six, i.e [1, 2*3-1, 3, 4*3-1, 5, 6*3-1, 7, 8*3-1, 9, ...].
This is the same as concatenating and then deduplicating reversed ranges [1..x] of where x is the cumulative sums of these slice lengths (i.e. the maximum of each slice) - [1,6,9,20,25,42,49,72,81,110,121,156,169,...], which is the odd integers squared interspersed with even numbers multiplied by themselves incremented, i.e. [1*1, 2*3, 3*3, 4*5, 5*5, 6*7, 7*7,...].
Ḃ¬+×)RUẎQị@ - Link: integer, n e.g. 10
) - for each v in [1..n]: vs = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
Ḃ - modulo 2 [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
¬ - logical NOT [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
+ - add (v) [ 1, 3, 3, 5, 5, 7, 7, 9, 9,11]
× - multiply (by v) [ 1, 6, 9,20,25,42,49,72,81,110]
R - range (vectorises) [[1],[1..6],[1..9],[1..20],...,[1..110]]
U - upend [[1],[6..1],[9..1],[20..1],...,[110..1]]
Ẏ - tighten [1,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,20,...,1,...,110,...,1]
Q - de-duplicate [1,6,5,4,3,2,9,8,7,20,...,110,...82]
@ - with swapped arguments:
ị - (n) index into 20
$endgroup$
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
add a comment |
$begingroup$
Jelly, 16 14 11 bytes
Ḃ¬+×)RUẎQị@
A monadic Link accepting an integer, n, which yields an integer, a(n).
Try it online! (very inefficient since it goes out to layer $lceilfrac n2rceil$)
A 14-byte version, ½‘Ḃ¬+×Ʋ€RUẎQị@, completes all but the largest test case in under 30s - Try it at TIO - this limits the layers used to $lceilfrac{lfloorsqrt nrfloor+1}2rceil$.
How?
The permutation is to take the natural numbers in reversed slices of lengths [1,5,3,11,5,17,7,23,9,29,11,35,13,...] - the odd positive integers interspersed with the positive integers congruent to five modulo six, i.e [1, 2*3-1, 3, 4*3-1, 5, 6*3-1, 7, 8*3-1, 9, ...].
This is the same as concatenating and then deduplicating reversed ranges [1..x] of where x is the cumulative sums of these slice lengths (i.e. the maximum of each slice) - [1,6,9,20,25,42,49,72,81,110,121,156,169,...], which is the odd integers squared interspersed with even numbers multiplied by themselves incremented, i.e. [1*1, 2*3, 3*3, 4*5, 5*5, 6*7, 7*7,...].
Ḃ¬+×)RUẎQị@ - Link: integer, n e.g. 10
) - for each v in [1..n]: vs = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
Ḃ - modulo 2 [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
¬ - logical NOT [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
+ - add (v) [ 1, 3, 3, 5, 5, 7, 7, 9, 9,11]
× - multiply (by v) [ 1, 6, 9,20,25,42,49,72,81,110]
R - range (vectorises) [[1],[1..6],[1..9],[1..20],...,[1..110]]
U - upend [[1],[6..1],[9..1],[20..1],...,[110..1]]
Ẏ - tighten [1,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,20,...,1,...,110,...,1]
Q - de-duplicate [1,6,5,4,3,2,9,8,7,20,...,110,...82]
@ - with swapped arguments:
ị - (n) index into 20
$endgroup$
Jelly, 16 14 11 bytes
Ḃ¬+×)RUẎQị@
A monadic Link accepting an integer, n, which yields an integer, a(n).
Try it online! (very inefficient since it goes out to layer $lceilfrac n2rceil$)
A 14-byte version, ½‘Ḃ¬+×Ʋ€RUẎQị@, completes all but the largest test case in under 30s - Try it at TIO - this limits the layers used to $lceilfrac{lfloorsqrt nrfloor+1}2rceil$.
How?
The permutation is to take the natural numbers in reversed slices of lengths [1,5,3,11,5,17,7,23,9,29,11,35,13,...] - the odd positive integers interspersed with the positive integers congruent to five modulo six, i.e [1, 2*3-1, 3, 4*3-1, 5, 6*3-1, 7, 8*3-1, 9, ...].
This is the same as concatenating and then deduplicating reversed ranges [1..x] of where x is the cumulative sums of these slice lengths (i.e. the maximum of each slice) - [1,6,9,20,25,42,49,72,81,110,121,156,169,...], which is the odd integers squared interspersed with even numbers multiplied by themselves incremented, i.e. [1*1, 2*3, 3*3, 4*5, 5*5, 6*7, 7*7,...].
Ḃ¬+×)RUẎQị@ - Link: integer, n e.g. 10
) - for each v in [1..n]: vs = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10]
Ḃ - modulo 2 [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
¬ - logical NOT [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
+ - add (v) [ 1, 3, 3, 5, 5, 7, 7, 9, 9,11]
× - multiply (by v) [ 1, 6, 9,20,25,42,49,72,81,110]
R - range (vectorises) [[1],[1..6],[1..9],[1..20],...,[1..110]]
U - upend [[1],[6..1],[9..1],[20..1],...,[110..1]]
Ẏ - tighten [1,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,20,...,1,...,110,...,1]
Q - de-duplicate [1,6,5,4,3,2,9,8,7,20,...,110,...82]
@ - with swapped arguments:
ị - (n) index into 20
edited 1 hour ago
answered 4 hours ago
Jonathan AllanJonathan Allan
54.7k537175
54.7k537175
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
add a comment |
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
$begingroup$
Very nice. And you surpassed the MATL answer!
$endgroup$
– agtoever
2 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 67 bytes
n=>8*(x=(int)Math.Sqrt(--n)+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n;int x;
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 67 bytes
n=>8*(x=(int)Math.Sqrt(--n)+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n;int x;
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 67 bytes
n=>8*(x=(int)Math.Sqrt(--n)+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n;int x;
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 67 bytes
n=>8*(x=(int)Math.Sqrt(--n)+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n;int x;
Try it online!
edited 5 hours ago
answered 5 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
3,054127
3,054127
add a comment |
add a comment |
$begingroup$
MATL, 12 bytes
Eq1YL!tPwG=)
Try it online!
Very memory-inefficient. Prepending X^k makes it more efficient.
$endgroup$
add a comment |
$begingroup$
MATL, 12 bytes
Eq1YL!tPwG=)
Try it online!
Very memory-inefficient. Prepending X^k makes it more efficient.
$endgroup$
add a comment |
$begingroup$
MATL, 12 bytes
Eq1YL!tPwG=)
Try it online!
Very memory-inefficient. Prepending X^k makes it more efficient.
$endgroup$
MATL, 12 bytes
Eq1YL!tPwG=)
Try it online!
Very memory-inefficient. Prepending X^k makes it more efficient.
answered 4 hours ago
Luis MendoLuis Mendo
75.5k889293
75.5k889293
add a comment |
add a comment |
$begingroup$
Python 3.8, 104 74 65 60 57 bytes
lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x
Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!
This solution uses 0-based indexing.
$endgroup$
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using>in place of<=andx*xin place ofx**2...like so:def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n...TIO
$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
add a comment |
$begingroup$
Python 3.8, 104 74 65 60 57 bytes
lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x
Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!
This solution uses 0-based indexing.
$endgroup$
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using>in place of<=andx*xin place ofx**2...like so:def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n...TIO
$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
add a comment |
$begingroup$
Python 3.8, 104 74 65 60 57 bytes
lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x
Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!
This solution uses 0-based indexing.
$endgroup$
Python 3.8, 104 74 65 60 57 bytes
lambda n:(-2,6)[n>4*(x:=(n**.5+1)//2)*x+2*x]*x+2+~n+8*x*x
Edit: Thanks to Johnathan Allan for getting it from 74 to 57 bytes!
This solution uses 0-based indexing.
edited 23 mins ago
answered 1 hour ago
KapocsiKapocsi
913
913
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using>in place of<=andx*xin place ofx**2...like so:def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n...TIO
$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
add a comment |
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using>in place of<=andx*xin place ofx**2...like so:def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n...TIO
$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
1
1
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using
> in place of <= and x*x in place of x**2 ...like so: def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n ...TIO$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Save 39 avoiding the imports, removing some redundant parentheses, and using
> in place of <= and x*x in place of x**2 ...like so: def f(n):x=((n-1)**.5+1)//2;return 8*x**2+(-2,6)[n>4*x*x+2*x]*x+2-n ...TIO$endgroup$
– Jonathan Allan
40 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Awesome! I will incorporate the edits. Made some changes before I saw your comment and got it down to 74 bytes. Does it matter that yours returns floats? I assume not...
$endgroup$
– Kapocsi
37 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Float representations of integers should be fine. Save some more using Python 3.8 assignment ...EDIT: make it zero indexed
$endgroup$
– Jonathan Allan
34 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
$begingroup$
Very cool. Feel free to make any further edits directly!
$endgroup$
– Kapocsi
21 mins ago
add a comment |
$begingroup$
Japt, 15 bytes
Port of Jonathan's Jelly solution. 1-indexed.
gUòÈ+v)*X õÃcÔâ
Try it
gUòÈ+v)*X õÃcÔâ :Implicit input of integer U
g :Index into
Uò : Range [0,U]
È : Map each X
+v : Add 1 if divisible by 2
) : Group result
*X : Multiply by X
õ : Range [1,result]
à : End map
c : Flatten
Ô : After reversing each
â : Deduplicate
$endgroup$
add a comment |
$begingroup$
Japt, 15 bytes
Port of Jonathan's Jelly solution. 1-indexed.
gUòÈ+v)*X õÃcÔâ
Try it
gUòÈ+v)*X õÃcÔâ :Implicit input of integer U
g :Index into
Uò : Range [0,U]
È : Map each X
+v : Add 1 if divisible by 2
) : Group result
*X : Multiply by X
õ : Range [1,result]
à : End map
c : Flatten
Ô : After reversing each
â : Deduplicate
$endgroup$
add a comment |
$begingroup$
Japt, 15 bytes
Port of Jonathan's Jelly solution. 1-indexed.
gUòÈ+v)*X õÃcÔâ
Try it
gUòÈ+v)*X õÃcÔâ :Implicit input of integer U
g :Index into
Uò : Range [0,U]
È : Map each X
+v : Add 1 if divisible by 2
) : Group result
*X : Multiply by X
õ : Range [1,result]
à : End map
c : Flatten
Ô : After reversing each
â : Deduplicate
$endgroup$
Japt, 15 bytes
Port of Jonathan's Jelly solution. 1-indexed.
gUòÈ+v)*X õÃcÔâ
Try it
gUòÈ+v)*X õÃcÔâ :Implicit input of integer U
g :Index into
Uò : Range [0,U]
È : Map each X
+v : Add 1 if divisible by 2
) : Group result
*X : Multiply by X
õ : Range [1,result]
à : End map
c : Flatten
Ô : After reversing each
â : Deduplicate
edited 1 hour ago
answered 1 hour ago
ShaggyShaggy
19.1k21768
19.1k21768
add a comment |
add a comment |
$begingroup$
Python 3.8 (pre-release), 53 bytes
A direct port of Arnauld's JavaScript answer, go upvote that, and/or J42161217's Mathematica answer, and/or Kapocsi's Python answer :)
lambda n:((x:=int(n**.5+1)&-2)*2-(n<x*x+x)*4+3)*x+1-n
0-indexed.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 53 bytes
A direct port of Arnauld's JavaScript answer, go upvote that, and/or J42161217's Mathematica answer, and/or Kapocsi's Python answer :)
lambda n:((x:=int(n**.5+1)&-2)*2-(n<x*x+x)*4+3)*x+1-n
0-indexed.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 53 bytes
A direct port of Arnauld's JavaScript answer, go upvote that, and/or J42161217's Mathematica answer, and/or Kapocsi's Python answer :)
lambda n:((x:=int(n**.5+1)&-2)*2-(n<x*x+x)*4+3)*x+1-n
0-indexed.
Try it online!
$endgroup$
Python 3.8 (pre-release), 53 bytes
A direct port of Arnauld's JavaScript answer, go upvote that, and/or J42161217's Mathematica answer, and/or Kapocsi's Python answer :)
lambda n:((x:=int(n**.5+1)&-2)*2-(n<x*x+x)*4+3)*x+1-n
0-indexed.
Try it online!
edited 6 mins ago
answered 13 mins ago
Jonathan AllanJonathan Allan
54.7k537175
54.7k537175
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f183528%2fnew-order-6-easter-egg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown