Find the nth cross-alternate sum Given an input of a single positive integer, output the "cross-alternate sum"
that corresponds to that integer.

Take the example of the input n=5. To find the cross-alternate sum, first
create a square grid of width and height n that, reading from left to right
and top to bottom, starts at 1 and increases by one each position:

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


Then, take the sums from the grid that form a "cross" (that is, both diagonals
combined):

 1           5
    7     9
      13
   17    19
21          25

1 5 7 9 13 17 19 21 25


Finally, take the alternating sum of this sequence:

1+5-7+9-13+17-19+21-25

-11


Another example, for n=6 (just to show what the cross looks like for
even-numbered n):

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

 1              6
    8       11
      15 16
      21 22
   26       29
31             36

1+6-8+11-15+16-21+22-26+29-31+36

20


Since this is code-golf, the shortest code in bytes will win.

Here are the correct outputs for n=1 to n=100, which you can use as test
cases:

1
4
-3
10
-11
20
-23
34
-39
52
-59
74
-83
100
-111
130
-143
164
-179
202
-219
244
-263
290
-311
340
-363
394
-419
452
-479
514
-543
580
-611
650
-683
724
-759
802
-839
884
-923
970
-1011
1060
-1103
1154
-1199
1252
-1299
1354
-1403
1460
-1511
1570
-1623
1684
-1739
1802
-1859
1924
-1983
2050
-2111
2180
-2243
2314
-2379
2452
-2519
2594
-2663
2740
-2811
2890
-2963
3044
-3119
3202
-3279
3364
-3443
3530
-3611
3700
-3783
3874
-3959
4052
-4139
4234
-4323
4420
-4511
4610
-4703
4804
-4899
5002
Jelly, 21 19 11 10 7 bytes

²~³¡H+2


Try it online!

Idea

Assume for a second that the first term of the final sum is subtracted rather than added.

Let n be a positive integer.

Even case

 1              6
    8       11
      15 16
      21 22
   26       29
31             36


The differences between the diagonal elements on the lower half of the rows are the first n ÷ 2 odd natural numbers. Since 1 + 3 + 5 + &hellip; + (2k + 1) = k2, they sum to (n ÷ 2)2 = n2 ÷ 4.

In this example

- 1 + 6 - 8 + 11 - 15 + 16 - 21 + 22 - 26 + 29 - 31 + 36  =
-(1 - 6)-(8 - 11)-(15 - 16)-(21 - 22)-(26 - 29)-(31 - 36) =
(    5   +   3    +    1  )+(   1    +    3    +    5   ) =
             9             +              9               = 18


Thus, the sum is 2 × n2 ÷ 4 = n2 ÷ 2.

Odd case

 1           5
    7     9
      13
   17    19
21          25


The differences between the diagonal elements on the corresponding rows from above and below (1 and 5, and 21 and 25; 7 and 9, and 17 and 19) is the same, so they will cancel out in the alternating sum.

In this example

- 1 + 5 - 7 + 9 - 13 + 17 - 19 + 21 - 25  =
-(1 - 5)-(7 - 9)- 13 +(17 - 19)+(21 - 25) =
    4   +   2   - 13 -    2    -    4     = -13


All that's left is the negative of the central element, which is the arithmetic mean of the first and last number, so it can be calculated as -(n2 + 1) ÷ 2.

General case

Since ~x = -(x + 1) for two's complement integers (~ denotes bitwise NOT), the formula for the odd case can be rewritten as ~n2 ÷ 2.

Also, since the first term (1) of the original sum is added instead of subtracted, the above formulas leave an error of 2, which has to be corrected.

Therefore, the nth cross-alternate sum is n2 ÷ 2 + 2 if n is even, and ~n2 ÷ 2 + 2 if it is odd.

Finally, bitwise NOT is an involution, i.e., ~~x = x for all x. This way ~~~x = ~x, ~~~~x = x, and, in general, ~nx (meaning that ~ is applied n times) is x if n is even and ~x if it is odd.

Thus, we can rewrite our general formula as ~nn2 ÷ 2 + 2 for all positive integers n.

Code

²~³¡H+2    Main link. Input: n

²          Yield n².
 ~         Apply bitwise NOT to n²...
  ³¡           n times.
    H      Halve the result.
     +2    Add 2.