# First spiral, then diagonal

Given a positive input number n, construct a spiral of numbers from 1 to n^2, with 1 in the top-left, spiraling inward clockwise. Take the sum of the diagonals (if n is odd, the middle number n^2 is counted twice) and output that number.

Example for n = 1:

1

(1) + (1) = 2


Example for n = 2:

1 2
4 3

(1+3) + (4+2) = 4 + 6 = 10


Example for n = 4:

 1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7

(1+13+15+7) + (10+16+14+4) = 36 + 44 = 80


Example of n = 5:

 1  2  3  4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

(1+17+25+21+9) + (13+23+25+19+5) = 73 + 85 = 158


## Further rules and clarifications

• This is OEIS A059924 and there are some closed-form solutions on that page.
• The input and output can be assumed to fit in your language's native integer type.
• The input and output can be given in any convenient format.
• You can choose to either 0-index or 1-index, as I am here in my examples, for your submission. Please state which you're doing.
• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• If possible, please include a link to an online testing environment so other people can try out your code!
• Standard loopholes are forbidden.
• This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

# R, 43 34 bytes

function(n)(8*n^3-3*n^2+4*n+3)%/%6


Try it online!

The OEIS page lists the following formula for a(n):

(16*n^3 - 6*n^2 + 8*n + 3 - 3*(-1)^n)/12


However, I skipped right over that to get to the PROG section where the following PARI code is found:

floor((16*n^3 - 6*n^2 + 8*n + 3 - 3*(-1^n))/12))


Naturally, +3-3*(-1^n) is the same as +6 so we can simplify the linked formula, first by reducing it to

(16*n^3-6*n^2+8*n+6)/12 -> (8*n^3-3*n^2+4*n+3)/6


and using %/%, integer division, rather than / to eliminate the need for floor.

• +3-3*(-1)^n is not really same as 6, although the difference is lost in integer division. – fergusq Aug 30 '17 at 20:19
• @fergusq you're right, but the formula given as the expression in PARI (which I base my solution on) has +3-3*(-1^n) which is the same as +6. I will update my answer to make that more obvious. – Giuseppe Aug 30 '17 at 20:21
• @Giuseppe It's +6 if n is odd, but +0 when n is even – Bergi Aug 30 '17 at 22:32
• @Bergi 3-3*(-1^n) is always 6 but 3-3*(-1)^n has that alternating behavior. The original formula has the latter, which makes the use of integer division unnecessary, as it is always divisible by 12 – Giuseppe Aug 30 '17 at 22:36
• Ah, right. It's weird though that the original author overlooked this, isn't it? – Bergi Aug 30 '17 at 22:44

# Python 2, 30 bytes

Saved some bytes by porting Giuseppe's approach.

lambda n:((8*n-3)*n*n+4*n+3)/6


Try it online!

# Python 2,  36  34 bytes

Saved some more bytes thanks to @LeakyNun.

lambda n:((8*n-3)*n*n+4*n+n%2*3)/6


Try it online!

• 36 bytes – Leaky Nun Aug 30 '17 at 17:24
• 34 bytes – Leaky Nun Aug 30 '17 at 17:24
• @LeakyNun Thanks and Thanks. – Mr. Xcoder Aug 30 '17 at 17:26
• 30 bytes credits to Giuseppe for approach – Leaky Nun Aug 30 '17 at 17:27
• @LeakyNun I was updating with that too – Mr. Xcoder Aug 30 '17 at 17:27

# Mathematica, 19 bytes

((8#-3)#*#+4#+3)/6&


You can run it with the following syntax:

((8#-3)#*#+4#+3)/6&


Where 5 can be replaced with the input.

You can Try it in the Wolfram Sandbox (Copy-Paste + Evaluate Cells)

# Mathematica, 19 bytes

((8#-3)#*#+4#+3)/6&


Try it online!

Saved some bytes by porting Giuseppe's approach.

# Mathematica, 58 bytes

I always enjoy questions with given answers thanx to oeis (for the nice question and answer)

LinearRecurrence[{3,-2,-2,3,-1},{0,2,10,34,80},2#][[#+1]]&

• Wouldn't it be golfier to use the closed form? – Leaky Nun Aug 30 '17 at 17:23
• many answers are using my simplification, so you may as well do the same – Giuseppe Aug 30 '17 at 17:40

# Jelly, 11 10 bytes

1 byte thanks to Jonathan Allan.

⁽ø\DN2¦ḅ:6


Try it online!

• 8,-3,4,3 -> ⁽ø\DN2¦ to save one byte – Jonathan Allan Aug 30 '17 at 20:05

# MATL, 21 bytes

tt2^Qw6Y3YL-wXytP+*ss


Try it online!

# Cubix, 33 bytes

I:u8**.../\*3t3n*3t+u@O,6+3+t3*4p


Try it online!

cube version:

      I : u
8 * *
. . .
/ \ * 3 t 3 n * 3 t + u
@ O , 6 + 3 + t 3 * 4 p
. . . . . . . . . . . .
. . .
. . .
. . .


Implements the same algorithm as my R answer. I suspect this can be golfed down.

# SOGL V0.12, 2515 14 bytes

8*3-**4.*+3+6÷


Try it Here!

Translation of Mr.Xcoder's Python answer which is using Giuseppe's approach. SOGL's not winning anything here :p

# Java 8, 24 bytes

n->((8*n-3)*n*n+4*n+3)/6


Note that /6 floors by default when calculating with integers in Java.

Try it here.

# Excel, 35 30 bytes

Saved 5 bytes using Giuseppe's approach.

=INT((8*A1^3-3*A1^2+4*A1+3)/6)


First attempt:

=(8*A1^3-3*A1^2+4*A1+3*MOD(A1,2))/6


Evolved from a direct implementation of formula from OEIS (37 bytes):

=(16*A1^3-6*A1^2+8*A1+3-3*(-1)^A1)/12


+3-3*(-1)^A1 logic can be changed to 6*MOD(A1,2).

=(16*A1^3-6*A1^2+8*A1+6*MOD(A1,2))/12


Does not save bytes, but allows removal of a common factor for 2 bytes.

# 05AB1E,  13  12 bytes

Uses the same base-conversion technique as Leaky Nun's Jelly submission

Maybe there is a shorter way to create the list of coefficients

-1 byte thanks to Datboi (use spaces ans wrap to beat compression(!))

8 3(4 3)¹β6÷


Try it online!

### How?

8 3(4 3)¹β6÷               stack: []
8            - literal            ['8']
3          - literal            ['8','3']
(         - negate             ['8',-3]
4        - literal            ['8',-3,'4']
3      - literal            ['8',-3,'4','3']
)     - wrap               [['8',-3,'4','3']]
¹    - 1st input (e.g. 4) [['8',-3,'4','3'], 4]
β   - base conversion    
6  - literal six        [483,6]
÷ - integer division   
- print TOS           80


My 13s...

•VŠ•S3(1ǝ¹β6÷


•2ùë•₂в3-¹β6÷


•мå•12в3-¹β6÷


All using compressions to find the list of coefficients.

• 8 3(4 3)¹β6÷ to safe 1 byte (no fancy compression though) – Datboi Aug 31 '17 at 11:10

# Pyth, 17 bytes

/+3+*^Q2-*8Q3*4Q6


Try it here.

# Pyke, 14 bytes

8*3-**Q4*+3+6f


Try it here!

# 05AB1E, 14 bytes

8*3-**4I*+3+6÷


Try it online!

# MATL, 12 bytes

[DcKI]6/iZQk


Try it at MATL Online!

### Explanation

[DcKI]   % Push array [8, -3, 4, 3]
6/       % Divide each entry by 6
i        % Push input
ZQ       % Evaluate polynomial
k        % Round down. Implicitly display


# Gaia, 14 bytes

8×3⁻××@4×+3+6/


Try it online!