8
\$\begingroup\$

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.
\$\endgroup\$

16 Answers 16

21
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ +3-3*(-1)^n is not really same as 6, although the difference is lost in integer division. \$\endgroup\$ – fergusq Aug 30 '17 at 20:19
  • \$\begingroup\$ @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. \$\endgroup\$ – Giuseppe Aug 30 '17 at 20:21
  • \$\begingroup\$ @Giuseppe It's +6 if n is odd, but +0 when n is even \$\endgroup\$ – Bergi Aug 30 '17 at 22:32
  • 3
    \$\begingroup\$ @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 \$\endgroup\$ – Giuseppe Aug 30 '17 at 22:36
  • 1
    \$\begingroup\$ Ah, right. It's weird though that the original author overlooked this, isn't it? \$\endgroup\$ – Bergi Aug 30 '17 at 22:44
5
\$\begingroup\$

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!

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

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)

\$\endgroup\$
3
\$\begingroup\$

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]]&
\$\endgroup\$
  • \$\begingroup\$ Wouldn't it be golfier to use the closed form? \$\endgroup\$ – Leaky Nun Aug 30 '17 at 17:23
  • 1
    \$\begingroup\$ many answers are using my simplification, so you may as well do the same \$\endgroup\$ – Giuseppe Aug 30 '17 at 17:40
2
\$\begingroup\$

Jelly, 11 10 bytes

1 byte thanks to Jonathan Allan.

⁽ø\DN2¦ḅ:6

Try it online!

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

MATL, 21 bytes

tt2^Qw6Y3YL-wXytP+*ss

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

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

Pyth, 17 bytes

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

Try it here.

\$\endgroup\$
0
\$\begingroup\$

Pyke, 14 bytes

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

Try it here!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 14 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Gaia, 14 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.