TheAlgorithms/JavaScript · error · Error

Dimension must be odd

Error message

Dimension must be odd

What it means

Thrown by `problem28(dim)` (Project Euler #28, number spiral diagonals) when `dim % 2 === 0`. The spiral formula assumes an odd-dimensioned square (a centre cell plus concentric odd rings), so even dimensions are rejected first. The check uses `%`, which coerces strings - so `'5' % 2` is 1 and the string passes, then later arithmetic may misbehave.

Source

Thrown at Project-Euler/Problem028.js:22

 * @see {@link https://projecteuler.net/problem=28}
 *
 * Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
 *
 * 21 22 23 24 25
 * 20 07 08 09 10
 * 19 06 01 02 11
 * 18 05 04 03 12
 * 17 16 15 14 13
 *
 * It can be verified that the sum of the numbers on the diagonals is 101.
 * What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
 *
 * @author ddaniel27
 */

function problem28(dim) {
  if (dim % 2 === 0) {
    throw new Error('Dimension must be odd')
  }
  if (dim < 1) {
    throw new Error('Dimension must be positive')
  }

  let result = 1
  for (let i = 3; i <= dim; i += 2) {
    /**
     * Adding more dimensions to the matrix, we will find at the top-right corner the follow sequence:
     * 01, 09, 25, 49, 81, 121, 169, ...
     * So this can be expressed as:
     * i^2, where i is all odd numbers
     *
     * Also, we can know which numbers are in each corner dimension
     * Just develop the sequence counter clockwise from top-right corner like this:
     * First corner: i^2
     * Second corner: i^2 - (i - 1) | The "i - 1" is the distance between corners in each dimension
     * Third corner: i^2 - 2 * (i - 1)

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Validate `Number.isInteger(dim) && dim % 2 === 1 && dim >= 1` upstream.
  2. Coerce with `Number(dim)` so string inputs are handled deterministically.
  3. If even dimensions must be supported in your domain, pick odd-floor or odd-ceil before calling.

Example fix

// before
problem28(gridSize) // gridSize could be 100

// after
const d = Number(gridSize)
if (!Number.isInteger(d) || d % 2 === 0 || d < 1) throw new RangeError('dim must be a positive odd integer')
problem28(d)
Defensive patterns

Strategy: validation

Validate before calling

const d = Number(dim)
if (!Number.isInteger(d) || d % 2 === 0 || d < 1) {
  throw new RangeError('dim must be a positive odd integer')
}
problem28(d)

Type guard

const isPositiveOddInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1 && x % 2 === 1

Prevention

When it happens

Trigger: Call `problem28(2)`, `problem28(100)`, `problem28(0)`. Negative even numbers like -2 also trigger this branch; negative odd numbers pass and hit the positivity check next.

Common situations: UI spinners that allow any integer, grid-size config from a request param, or confusion between 'size' and 'number of rings'.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/b43112f365c3645f. Report an issue: GitHub.