A magic square is a square grid containing consecutive integers from 1 to N², arranged so that every row, column and diagonal adds up to the same number. That number is a constant. There is no way to create a valid N x N magic square that does not sum to the associated constant.

EG
A 3 x 3 magic square always sums to 15.

    ┌───┬───┬───┐
    │ 2 │ 7 │ 6 │
    ├───┼───┼───┤
    │ 9 │ 5 │ 1 │
    ├───┼───┼───┤
    │ 4 │ 3 │ 8 │
    └───┴───┴───┘
A 4 x 4 magic square always sums to 34.

Traditionally, the sequence leaves off terms for n = 0 and n = 1 as the magic squares of order 0 and 1 are trivial; and a term for n = 2 because it is impossible to form a magic square of order 2.



Task
 Starting at order 3, show the first 20 magic constants.
 Show the 1000th magic constant. (Order 1003)
 Find and show the order of the smallest N x N magic square whose constant is greater than 10¹ through 10¹⁰.


Stretch
 Find and show the order of the smallest N x N magic square whose constant is greater than 10¹¹ through 10²⁰.


See also
 Wikipedia: Magic constant
 OEIS: A006003 (Similar sequence, though it includes terms for 0, 1 & 2.)


 
# syntax: GAWK -f MAGIC_CONSTANT.AWK
# converted from FreeBASIC
BEGIN {
    printf("The first 20 magic constants are:")
    for (i=1; i<=20; i++) {
      printf(" %d",a(i))
    }
    printf("\n")
    printf("The 1,000th magic constant is: %d\n",a(1000))
    for (i=1; i<=20; i++) {
      printf("10^%02d: %8d\n",i,inv_a(10^i))
    }
    exit(0)
}
function a(n) {
    n += 2
    return(n*(n^2+1)/2)
}
function inv_a(x,  k) {
    while (k*(k^2+1)/2+2 < x) {
      k++
    }
    return(k)
}
 