Ulam Spiral Diagonal Corners and Sum

Return the diagonal corner values of square spiral layer r and the total of both diagonals through that layer, counting the central one once and using exact arithmetic.

Description

A square number spiral places successive integers around a central one.

A square number spiral places successive integers around a central one. A layer index determines four outer diagonal corners and the sum of both diagonals through all enclosed layers.1

The tool returns corners, diagonalSum. The indexing and output conventions below determine how to read these values.

When to use Ulam Spiral Diagonal Corners and Sum

Use this for the following task: Return the diagonal corner values of square spiral layer r and the total of both diagonals through that layer, counting the central one once and using exact arithmetic. Use the input and output conventions below to reproduce the intended mathematical quantity.

How to use Ulam Spiral Diagonal Corners and Sum and understand its results

  1. Choose the required indexes, integers or sequence entries. Preserve decimal-string encodings where the field table specifies strings.
  2. Enter layer according to the field table, then run the tool.
  3. Read corners, diagonalSum. Preserve exact decimal strings and the stated indexing or coefficient order.
  4. Keep exact decimal strings for subsequent integer arithmetic; distinguish them from any explicitly approximate numeric result.
Input fieldMeaningValue type and boundsRequirement
layerNonnegative layer index; side length is 2r+1string (integer/arbitrary); maximum text length 200; pattern ^(0|[1-9][0-9]*)$Required

Parse the nonnegative layer as BigInt and use closed-form integer arithmetic. At layer zero return the single corner one, counted once in the sum.

side=2r+1; corners=side²−{0,2r,4r,6r}; sum=1+16r(r+1)(2r+1)/6+2r(r+1)+4r

The symbols and boundary conditions refer to the supplied fields and the implementation described above.

Output fieldMeaningValue type
cornersCorner values in descending order; center only for layer zerolist of string (integer/arbitrary)
diagonalSumSum of both diagonals through this layerstring (integer/arbitrary)
json
{
  "input": {
    "layer": "2"
  },
  "output": {
    "corners": [
      "25",
      "21",
      "17",
      "13"
    ],
    "diagonalSum": "101"
  }
}

Limitations and assumptions

The tool returns corner values and a diagonal sum as decimal strings, not an image, full spiral or prime-pattern analysis.

Use the declared integer bounds and string formats. Large exact outputs remain decimal strings; numerical approximations use ordinary floating-point arithmetic. A rejected input is different from a valid zero count or false membership flag.

Alternative or Complementary methods

Choose Pascal Triangle Row when you need its operation: Return the zero-indexed nth row of Pascal’s triangle as exact decimal binomial coefficients; n is capped at 1,000 to bound the output. Choose nth Fibonacci Number for the complementary operation: Return F(n) exactly as a decimal string with F(0)=0 and F(1)=1; the index is capped at 100,000 for bounded computation.

Background reference: Number Spiral Diagonals.

References

  1. Number Spiral Diagonals — Project Euler

Similar or alternative tools

  • Pascal Triangle Row

    Return the zero-indexed nth row of Pascal’s triangle as exact decimal binomial coefficients; n is capped at 1,000 to bound the output.

  • nth Fibonacci Number

    Return F(n) exactly as a decimal string with F(0)=0 and F(1)=1; the index is capped at 100,000 for bounded computation.

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies