#JavaScript (ES7), <s> 46 45 </s> 41 bytes 0-indexed. <!-- language-all: lang-javascript --> n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n [Try it online!](https://tio.run/##Zc/BDoIwDAbgu0@xk9lGBt3GgCbiuxAEoyGbEWN28tUn3ljtqcn/tU3vw3tYx@ft8VI@XKY098n3Z85j76UsXaGPHy2kUdyfooxFFLIurNg6rXwag1/DMpVLuPKZa/YrxbQQrKoY04c8dyS3JDdA5oEuIKA1BLQdAQ0B2tj8hHP/os6ENTUhFgD2xCDSO7jVniDC37fYIWRbWmzSFw "JavaScript (Node.js) – Try It Online") ###How? This is based on the 1-indexed formula used in the example programs of [A090861](https://oeis.org/A090861). \$x_n\$ is the layer index of the spiral, starting with \$0\$ for the center square: $$x_n=\left\lfloor\frac{\sqrt{n-1}+1}{2}\right\rfloor$$ [Try it online!](https://tio.run/##PY9NbsIwFIT3OcXssGlwY8cUUEVvkBVLmoUFBIJSOzJWr5@Oqchi5PfzzTz57n7d4xT7Ma18OF@mqcEexwI4otYlakPVlKXW1Ae1QVv@E1UJza3mVnOrudUbEtsXYXacMAOkQAqZ2pLYzQQ7zUvIVH5zBl22mgkmal4CpyAN9oa91TNBj2Gy4QXDS4ZZhlnWvAibE@m19FrSlrQlbWu0RftZFB2/7bH/QuPSTXVDCFEI4bGCllguUak13nL9DiOL4hT8IwwXNYSraNSPG0XM5vgsnzmd8FKlcEix91ch1ejOh@RiErWU6h56LxaLufr2rKfpDw "JavaScript (Node.js) – Try It Online") \$k_n\$ is set to \$6\$ for the bottom part of each layer (including the center square), and to \$-2\$ everywhere else: $$k_n=\begin{cases} -2&\text{if }n\le 4{x_n}^2+2x_n\\ 6&\text{otherwise} \end{cases}$$ [Try it online!](https://tio.run/##PY/NboMwEITvPMXcYlPi4sX5a5v2CTjlmHJASciPUhsRVOXt6ThVOIzY9Xw7Iy71b33bdee2n/qwPwxDiTW2CbBFYTMUQhWUo2bUnFqgyv6JPIOla@laupauXZBYPglZ8YUZIAVSiNSSxGokuFk2IVLxGzN45fKRYKJlE/gK0uAu3J0dCd4Ik4UNwiZhljDLyZNwMZG3jreOtCPtSLsCVVK9J0nD3/ZYf0LdOZV1fzLNNYROKeUxhdVIU@Rmhpc4v0J0Rv5jDYcU92gKLXksX5gK3jDXSbIL/hauB3MNR1Wan7pVXSzpHuOjr1Femz5s@u7sj0qbtt5v@rrrVaG1uYSzV5PJOH17zsPwBw "JavaScript (Node.js) – Try It Online") Then \$a_n\$ is given by: $$a_n=8{x_n}^2+k_nx_n+2-n$$ [Try it online!](https://tio.run/##PY/NboMwEITvPMXcYqfg4sX5a0v7BDnlmHJA@Y9SGxFU5e3pmCocVuzsfjNrrvVvfd@1l6bLfNgf@n6NEtsE2KKwKQphFSzHmrHmrAWq9J/IU1huLbeWW8utXZBYPglZccIMkAIpRGpJYjUSVJaXEKn4jRl0uXwkmGh5CZyCNKiF2tmRoEeYLLwgvCTMEmY5eRIuJtLr6HWkHWlH2hWokuo9SY78bY/yE0tMoR5U67o7m@MthFYp5ZHBakynyM0ML7F/hehhItQEPko4eh/PkQziC5ngDXM9qDjN4JNkF/w93A7mFk5qbX7qRrXxeDu0wzuOymvThU3XXvxJadPU@01Xt50qtDbXcPFqMhm7b8@@7/8A "JavaScript (Node.js) – Try It Online") Which can be translated into: n=>8*(x=(n-1)**.5+1>>1)*x+(n<=4*x*x+2*x?-2:6)*x+2-n Making it 0-indexed saves 5 bytes right away: n=>8*(x=n**.5+1>>1)*x+(n<4*x*x+2*x?-2:6)*x+1-n The formula can be further simplified by using: $${x'}_n=2\times\left\lfloor\frac{\sqrt{n}+1}{2}\right\rfloor$$ which can be expressed as: x=n**.5+1&~1 leading to: n=>2*(x=n**.5+1&~1)*x+(n<x*x+x?-1:3)*x+1-n and finally: n=>((x=n**.5+1&~1)*2-(n<x*x+x)*4+3)*x+1-n