Skip to main content
Commonmark migration
Source Link

#JavaScript (ES7),  46 45  41 bytes

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!

###How?

How?

This is based on the 1-indexed formula used in the example programs of 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!

\$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!

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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!

\$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!

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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

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!

How?

This is based on the 1-indexed formula used in the example programs of 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!

\$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!

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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
added example code for each part
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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!

\$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!

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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$$

\$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}$$

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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!

\$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!

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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
added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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$$

\$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}$$

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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

#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!

#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!

###How?

This is based on the 1-indexed formula used in the example programs of 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$$

\$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}$$

Then \$a_n\$ is given by:

$$a_n=8{x_n}^2+k_nx_n+2-n$$

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
saved 4 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading