First spiral, then diagonal

8

3

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.

AdmBorkBork

Posted 2017-08-30T17:14:38.300

Reputation: 41 581

Answers

21

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.

Giuseppe

Posted 2017-08-30T17:14:38.300

Reputation: 21 077

1+3-3*(-1)^n is not really same as 6, although the difference is lost in integer division. – fergusq – 2017-08-30T20:19:33.940

@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 – 2017-08-30T20:21:45.103

@Giuseppe It's +6 if n is odd, but +0 when n is even – Bergi – 2017-08-30T22:32:16.947

3@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 – 2017-08-30T22:36:24.810

1Ah, right. It's weird though that the original author overlooked this, isn't it? – Bergi – 2017-08-30T22:44:47.533

Congrats on 7k (you had 6,999 before I upvoted) – NoOneIsHere – 2017-11-20T20:50:52.113

5

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!

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774

136 bytes – Leaky Nun – 2017-08-30T17:24:21.700

34 bytes – Leaky Nun – 2017-08-30T17:24:55.790

@LeakyNun Thanks and Thanks. – Mr. Xcoder – 2017-08-30T17:26:11.797

230 bytes credits to Giuseppe for approach – Leaky Nun – 2017-08-30T17:27:23.500

@LeakyNun I was updating with that too – Mr. Xcoder – 2017-08-30T17:27:57.287

3

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]]&

J42161217

Posted 2017-08-30T17:14:38.300

Reputation: 15 931

Wouldn't it be golfier to use the closed form? – Leaky Nun – 2017-08-30T17:23:53.217

1many answers are using my simplification, so you may as well do the same – Giuseppe – 2017-08-30T17:40:29.467

3

Mathematica, 19 bytes

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

You can run it with the following syntax:

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

Where 5 can be replaced with the input.

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

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774

2

Jelly, 11 10 bytes

1 byte thanks to Jonathan Allan.

⁽ø\DN2¦ḅ:6

Try it online!

Leaky Nun

Posted 2017-08-30T17:14:38.300

Reputation: 45 011

8,-3,4,3 -> ⁽ø\DN2¦ to save one byte – Jonathan Allan – 2017-08-30T20:05:17.740

1

SOGL V0.12, 25 15 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

dzaima

Posted 2017-08-30T17:14:38.300

Reputation: 19 048

1

MATL, 21 bytes

tt2^Qw6Y3YL-wXytP+*ss

Try it online!

James

Posted 2017-08-30T17:14:38.300

Reputation: 54 537

You can shorten a bit with UQG1YL-tP,wXds]+

– Luis Mendo – 2017-08-30T20:17:22.777

1

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.

Giuseppe

Posted 2017-08-30T17:14:38.300

Reputation: 21 077

1

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    [483]
          6  - literal six        [483,6]
           ÷ - integer division   [80]
             - print TOS           80

My 13s...

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

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

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

All using compressions to find the list of coefficients.

Jonathan Allan

Posted 2017-08-30T17:14:38.300

Reputation: 67 804

8 3(4 3)¹β6÷ to safe 1 byte (no fancy compression though) – Datboi – 2017-08-31T11:10:42.283

1

Java 8, 24 bytes

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

Port of @Giuseppe's R answer.
Note that /6 floors by default when calculating with integers in Java.

Try it here.

Kevin Cruijssen

Posted 2017-08-30T17:14:38.300

Reputation: 67 575

1

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.

Wernisch

Posted 2017-08-30T17:14:38.300

Reputation: 2 534

0

Pyth, 17 bytes

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

Try it here.

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774

0

Pyke, 14 bytes

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

Try it here!

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774

0

05AB1E, 14 bytes

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

Try it online!

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774

0

MATL, 12 bytes

[DcKI]6/iZQk

Try it at MATL Online!

Explanation

Same approach as Giuseppe's answer.

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

Luis Mendo

Posted 2017-08-30T17:14:38.300

Reputation: 87 464

0

Gaia, 14 bytes

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

Try it online!

Mr. Xcoder

Posted 2017-08-30T17:14:38.300

Reputation: 39 774