Tech News
← Back to articles

Zigzag Number Spiral - Closed Form Expression

read original related products more articles

Zigzag Number Spiral

By Susam Pal on 27 Jul 2025

\[ \gdef\lf{\hspace{-5mm}\leftarrow\hspace{-5mm}} \gdef\rt{\hspace{-5mm}\rightarrow\hspace{-5mm}} \gdef\up{\uparrow} \gdef\dn{\downarrow} \gdef\sp{} \gdef\cd{\cdots} \gdef\vd{\vdots} \gdef\dd{\ddots} \gdef\arraystretch{1.2} \gdef\hl{{\small\blacktriangleright}} \]

Consider the following infinite grid of numbers, where the numbers are arranged in a spiral-like manner, but the spiral reverses direction each time it reaches the edge of the grid: \begin{array}{rcrcrcrcrl} 1 & \rt & 2 & \sp & 9 & \rt & 10 & \sp & 25 & \cd \\ \sp & \sp & \dn & \sp & \up & \sp & \dn & \sp & \up & \sp \\ 4 & \lf & 3 & \sp & 8 & \sp & 11 & \sp & 24 & \cd \\ \dn & \sp & \sp & \sp & \up & \sp & \dn & \sp & \up & \sp \\ 5 & \rt & 6 & \rt & 7 & \sp & 12 & \sp & 23 & \cd \\ \sp & \sp & \sp & \sp & \sp & \sp & \dn & \sp & \up & \sp \\ 16 & \lf & 15 & \lf & 14 & \lf & 13 & \sp & 22 & \cd \\ \dn & \sp & \sp & \sp & \sp & \sp & \sp & \sp & \up & \sp \\ 17 & \rt & 18 & \rt & 19 & \rt & 20 & \rt & 21 & \cd \\ \vd & \sp & \vd & \sp & \vd & \sp & \vd & \sp & \vd & \dd \end{array} Can we find a closed-form expression that tells us the number at the \( m \)th row and \( n \)th column?

Contents

Introduction

Before we explore this problem further, let us rewrite the zigzag number spiral grid in a cleaner form, omitting the arrows: \begin{array}{rrrrrl} 1 & 2 & 9 & 10 & 25 & \cd \\ 4 & 3 & 8 & 11 & 24 & \cd \\ 5 & 6 & 7 & 12 & 23 & \cd \\ 16 & 15 & 14 & 13 & 22 & \cd \\ 17 & 18 & 19 & 20 & 21 & \cd \\ \vd & \vd & \vd & \vd & \vd & \dd \end{array} Let \( f(m, n) \) denote the number at the \( m \)th row and \( n \)th column. For example, \( f(1, 1) = 1 \) and \( f(2, 5) = 24. \) We want to find a closed-form expression for \( f(m, n). \)

Let us first clarify what we mean by a closed-form expression. There is no universal definition of a closed-form expression, but the term typically refers to a mathematical expression involving variables and constants, built using a finite combination of basic operations: addition, subtraction, multiplication, division, integer exponents, roots with integer index, and functions such as exponentials, logarithms and trigonometric functions.

In this article, however, we need only addition, subtraction, division, squares and square roots. This may be a bit of a spoiler, but I must mention that the \( \max \) function appears in the closed-form expressions we are about to see. If you are concerned about whether functions like \( \max \) and \( \min \) are permitted in such expressions, note that \begin{align*} \max(m, n) & = \frac{m + n + \sqrt{(m - n)^2}}{2}, \\ \min(m, n) & = \frac{m + n - \sqrt{(m - n)^2}}{2}. \end{align*} So \( \max \) and \( \min \) are simply shorthand for expressions involving addition, subtraction, division, squares and square roots. In the discussion that follows, we will use only the \( \max \) function.

Patterns on the Edges

... continue reading