[Question]
  [
We can roll up the natural numbers in a rectangular spiral:
```
 17--16--15--14--13
  |               |
 18   5---4---3  12
  |   |       |   |
 19   6   1---2  11
  |   |           |
 20   7---8---9--10
  |
 21--22--23--24--25
```
But now that we have them on a rectangular grid we can unwind the spiral in a different order, e.g. going clockwise, starting north:
```
 17  16--15--14--13
  |   |           |
 18   5   4---3  12
  |   |   |   |   |
 19   6   1   2  11
  |   |       |   |
 20   7---8---9  10
  |               |
 21--22--23--24--25
```
The resulting sequence is clearly a permutation of the natural numbers:
```
1, 4, 3, 2, 9, 8, 7, 6, 5, 16, 15, 14, 13, 12, 11, 10, 25, 24, 23, 22, 21, 20, 19, 18, 17, ...
```
Your task is to compute this sequence. ([OEIS A020703](https://oeis.org/A020703), but spoiler warning: it contains another interesting definition and several formulae that you might want to figure out yourself.)
**Fun fact:** all 8 possible unwinding orders have their own OEIS entry.
## The Challenge
Given a positive integer `n`, return the `n`th element of the above sequence.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply.
## Test Cases
```
1       1
2       4
3       3
4       2
5       9
6       8
7       7
8       6
9       5
100     82
111     111
633     669
1000    986
5000    4942
9802    10000
10000   9802
```
For a complete list up to and including `n = 11131` [see the b-file on OEIS](https://oeis.org/A020703/b020703.txt).
  
[Answer]
# Japt, ~~20~~ ~~19~~ 16 bytes
```
V=U¬c)²-V *2-U+2
```
[Test it online!](http://ethproductions.github.io/japt?v=master&code=MStVrGMgsi0oVS0oVawtMSBjILI=&input=MTAwMDA=)
Based on the observation that
> 
>  F(N) = ceil(N^.5) \* (ceil(N^.5)-1) - N + 2
> 
> 
> 
Or, rather, that
> 
>  F(N) = the first square greater than or equal to N, minus its square root, minus N, plus 2.
> 
> 
> 
I don't know if this explanation is on the OEIS page, as I haven't looked at it yet.
[Answer]
# Jelly, ~~11~~ 10 bytes
```
’Æ½ð²+ḷ‘Ḥ_
```
Another Jelly answer on my phone.
```
’Æ½ð²+ḷ‘Ḥ_   A monadic hook:
’Æ½          Helper link. Input: n
’             n-1
 Æ½            Atop integer square root. Call this m.
   ð         Start a new dyadic link. Inputs: m, n
    ²+ḷ‘Ḥ_    Main link:
    ²+ḷ       Square m, add it to itself,
       ‘      and add one.
        Ḥ     Double the result
         _    and subtract n.
```
Try it [here](http://jelly.tryitonline.net/#code=4oCZw4bCvcOwwrIr4bi34bikKzJf&input=&args=MTAw).
[Answer]
# Julia, 28 bytes
```
n->2((m=isqrt(n-1))^2+m+1)-n
```
This is a lambda function that accepts an integer and returns an integer. To call it, assign it to a variable.
We define *m* to be the largest integer such that *m*2 ≤ *n*-1, i.e. the integer square root of *n*-1 (`isqrt`). We can then simplify the OEIS expression 2 (*m* + 1) *m* - *n* + 2 down to simply 2 (*m*2 + *m* + 1) - *n*.
[Try it online](http://goo.gl/rwF4hG)
[Answer]
# CJam, 14 bytes
```
qi_(mQ7Ybb2*\-
```
Using Alex's approach: `2*(m^2+m+1)-n` where `m = isqrt(n-1)`.
[Answer]
## ES7, ~~31~~ ~~28~~ 26 bytes
```
n=>(m=--n**.5|0)*++m*2-~-n
```
I had independently discovered Alex's formula but I can't prove it because I wasn't near a computer at the time.
Edit: Saved 3 bytes partly thanks to @ETHproductions. Saved a further 2 bytes.
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), ~~25~~ ~~19~~ 14 bytes
```
{2⊥⍨3⍴⌊√⍵-1}-⊢
```
-6 bytes from Adám.
-5 bytes from Bubbler.
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qHeuQqJGnqatkZZGWk5@fpFGcWFRiUaerqGmprahpha6mG6ettH/aqNHXUsf9a4wftS75VFP16OOWY96t@oa1uo@6lr0P@1R24RHvX2Pupof9a4BKji03vhR28RHfVODg5yBZIiHZ/D/NAVDrjQFIyA2BmITIDYFYjMQbWBgAKQsLQxAsoZAngEA "APL (Dyalog Extended) – Try It Online")
A direct implementation of the last function mentioned on the OEIS page:
\$a(n)=2×\Big(\big\lfloor\sqrt{n-1}\big\rfloor+1\Big)×\big\lfloor\sqrt{n-1}\big\rfloor-n+2\$
## Explanation:
```
{(2-⍵)+2×(1+⌊√⍵-1)×⌊√⍵-1} ⍵ → n
                   ⌊√⍵-1  floor of root of n-1
         (1+⌊√⍵-1)×       times 1 + floor of root of n-1
       2×                 times 2
 (2-⍵)                    plus 2-n
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qHeuQqJGnqatkZZGWk5@fpFGcWFRiUaerqGmprahpha6mG6ettH/ag0j3Ue9WzW1jQ5P1zDUftTT9ahjFlAAqODwdCRe7f@0R20THvX2PepqftS75lHvlkPrjR@1TXzUNzU4yBlIhnh4Bv9PUzDkSlMwAmJjIDYBYlMgNgNiQwMgAAA "APL (Dyalog Extended) – Try It Online")
[Answer]
# Pyth, 21 bytes
```
K2-h+^.E@QKK^t.E@QKKQ
```
[Try it online!](http://pyth.herokuapp.com/?code=K2-h%2B%5E.E%40QKK%5Et.E%40QKKQ&input=8&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A100%0A111%0A633%0A1000%0A5000%0A9802%0A10000&debug=0)
Nothing fancy going on. Same method as in the JAPT answer.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 20 bytes
```
-1$r$[I*I+I+1=*2-?=.
```
This uses the same technique as pretty much all other answers.
### Explanation
```
-1                   § Build the expression Input - 1
  $r                 § Square root of Input - 1
    $[I              § Unify I with the floor of this square root
       *I+I+1        § Build the expression I * I + I + 1
             =*2-?   § Evaluate the previous expression (say, M) and build the expression
                     § M * 2 - Input
                  =. § Unify the output with the evaluation of M * 2 - Input
```
A midly interesting fact about this answer is that it is easier and shorter to use `=` rather than parentheses.
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), ~~16~~ 13 bytes
```
qX^Y[tQ*Q2*G-
```
Based on [Lynn's CJam answer](https://codegolf.stackexchange.com/a/70805/36398).
[**Try it online!**](http://matl.tryitonline.net/#code=cVhea3RRKlEyKkct&input=MTAw) (`Y[` has been replaced by `k` according to changes in the language)
```
q       % input n. Subtract 1
X^      % square root
Y[      % floor
tQ      % duplicate and add 1
*       % multiply
Q       % add 1
2*      % multiply by 2
G-      % subtract n
```
---
This uses a different approach than other answers (**16 bytes**):
```
6Y3iQG2\+YLt!G=)
```
It explicitly generates the two spiral matrices (actually, vertically flipped versions of them, but that doesn't affect the output). The first one is
```
17    16    15    14    13
18     5     4     3    12
19     6     1     2    11
20     7     8     9    10
21    22    23    24    25
```
and the second one traces the modified path:
```
25    10    11    12    13
24     9     2     3    14
23     8     1     4    15
22     7     6     5    16
21    20    19    18    17
```
To find the `n`-th number of the sequence it suffices to find `n` in the second matrix and pick the corresponding number in the first. The matrices need to be big enough so that `n` appears, and should have *odd* size so that the origin (number `1`) is in the same position in both.
**[Try it online](http://matl.tryitonline.net/#code=UUcyXCsgNlkzWUx0IUc9KQ&input=MTAw)** too! (`6Y3` has been moved according to changes in the language)
```
6Y3      % 'spiral' string
i        % input n
QG2\+    % round up to an odd number large enough
YL       % generate spiral matrix of that size: first matrix
t!       % duplicate and transpose: second matrix
G=       % logical index that locates n in the second matrix
)        % use that index into first matrix
```
]