Skip to content

The Ulam Spiral: Visual Patterns in Prime Numbers

  • Updated: September 7, 2026
📌

Complete guide: Prime Number Patterns

The Ulam spiral places the positive integers on a square grid, starting with 1 at the center and winding outward. When the prime numbers are marked, diagonal lines and broken bands appear. These visual patterns are genuine arithmetic effects, but they do not form a rule that predicts every prime.

What the Ulam Spiral Shows

The spiral turns a one-dimensional sequence of integers into a two-dimensional picture. Prime-rich diagonals appear because many straight lines in the grid correspond to quadratic expressions. Some of those expressions avoid small divisors for long stretches, so they produce an unusually high share of prime values within certain ranges.

How the Ulam Spiral Is Constructed

The standard square spiral begins with 1 in the center. The number 2 is placed immediately to its right. The path then turns counterclockwise, placing consecutive integers around the center in expanding square layers.

Starting Point

The center contains 1, which is neither prime nor composite.

Spiral Direction

Consecutive integers move right, up, left, and down in longer runs.

Prime Marking

Cells containing primes are colored or highlighted; composite cells are left unmarked.

Visible Result

Marked cells form diagonal streaks, gaps, short clusters, and sparse outer regions.

A 7 × 7 Ulam spiral; prime numbers are shown in green and bold.
RowC1C2C3C4C5C6C7
R137363534333231
R238171615141330
R339185431229
R440196121128
R541207891027
R642212223242526
R743444546474849

Even in this small grid, primes such as 5, 17, and 37 line up on one diagonal, while 3, 13, 31, and 43 contribute to another visible direction. The lines are interrupted because no simple diagonal expression produces only primes.

Why Diagonal Patterns Appear

Square layers give the spiral a strong algebraic structure. The outer corner of layer m is an odd square, and the other three corners are obtained by subtracting equal side-length steps. This turns corner sequences into quadratic polynomials.

Corner Values on Layer m

(2m + 1)²

(2m + 1)² − 2m

(2m + 1)² − 4m

(2m + 1)² − 6m

For m = 1, the corner values are 9, 7, 5, and 3. For m = 2, they are 25, 21, 17, and 13. Because each expression is quadratic in m, cells lying along diagonal rays often follow quadratic sequences as the spiral expands.

A Diagonal Can Be Structurally Composite

One principal diagonal contains 1, 9, 25, 49, 81, and later odd squares. Every value after 1 is composite. The same geometry that creates prime-rich streaks also creates lines with no primes at all beyond the center.

Quadratic Sequences and Prime-Rich Lines

A polynomial does not need to produce a prime at every input to create a visible streak. It only needs to return primes more often than nearby sequences across the displayed range. A short run can look striking when prime cells are marked against thousands of unmarked composite cells.

One full diagonal contains 43, 21, 7, 1, 3, 13, and 31 in spatial order. When its cells are ordered outward by spiral layer, the values are 1, 3, 7, 13, 21, 31, and 43. They match n² + n + 1 for n = 0, 1, 2, 3, and so on. Several early terms are prime, but 21 is composite. The diagonal therefore has a prime-rich appearance without being an all-prime formula.

Prime-Rich Does Not Mean Always Prime

A quadratic expression may generate many primes for small inputs and still produce composite values later. Passing residue filters or avoiding a few small divisors makes a number a prime candidate, not a proven prime.

Modular arithmetic helps explain the contrast between different diagonals. If a polynomial repeatedly lands on multiples of 3, 5, or another small prime, its marked line quickly breaks apart. A polynomial that avoids those residue classes can keep producing plausible prime candidates. Each candidate still requires a primality test.

What the Pattern Does and Does Not Mean

Common Ulam spiral observations and their mathematical interpretation.
ObservationMathematical ReasonLimit
Long diagonal streaksGrid lines can follow quadratic integer sequences.Every visible streak eventually contains composite values.
Empty diagonal bandsSome sequences contain squares or frequent multiples of small primes.A sparse line does not describe prime behavior elsewhere in the grid.
More open space farther outPrime density decreases on average as numbers grow.Local regions can still contain clusters of nearby primes.
Repeated directional biasThe square geometry organizes related polynomial sequences along fixed rays.Rotating the spiral changes the picture’s orientation, not which integers are prime.

The Ulam spiral does not reveal a hidden repeating cycle for all primes. Prime numbers are fully determined by divisibility, yet their exact locations do not settle into a simple periodic pattern. The spiral makes selected arithmetic relationships visible by changing the layout, not by changing the numbers themselves.

Prime Density in Larger Spirals

As the spiral grows, the marked cells become less dense. Near a large number x, the average chance that an integer is prime is roughly related to 1 / log(x). This is an average-density statement, not a test for an individual number.

Average Prime Count Near x

The prime counting function satisfies the approximation π(x) ≈ x / log(x) for large x.

This thinning changes the picture gradually. Diagonal structures may remain visible because certain polynomial sequences continue to outperform nearby lines, even while the share of primes across the whole grid declines.

Density Is Not Exact Prediction

The expression x / log(x) estimates how many primes occur up to x. It does not identify the next prime, prove that a diagonal term is prime, or give the size of the next prime gap.

Changes in Starting Value and Orientation

The familiar image uses 1 at the center, but other starting values create different alignments. Shifting the center changes which polynomial sequences fall on prominent diagonals. Reversing the turn direction or rotating the finished grid preserves primality while moving the streaks to different visual directions.

This sensitivity is useful. It shows that the picture depends on both number-theoretic structure and the chosen coordinate layout. A strong diagonal in one version may become an offset band in another, while the underlying sequence of prime and composite integers remains unchanged.

Related Spiral Designs

Using the Spiral to Study a Candidate Number

A marked spiral is only as accurate as the primality decisions used to create it. Small values can be checked by trial division up to the square root. Larger values may require more efficient primality tests, depending on their size.

Check a Number Directly

A position on a diagonal does not prove primality. Use the prime number checker to test a whole number before treating it as a marked spiral cell.

Questions About the Ulam Spiral

Who Created the Ulam Spiral?

The pattern is named after mathematician Stanisław Ulam, who explored the square-number arrangement in 1963 and noticed that marked primes formed unexpected diagonal structures.

Why Do Prime Numbers Form Diagonal Lines?

Many diagonal rays correspond to quadratic expressions in the layer number. Some of these expressions avoid small divisors across long intervals, allowing prime values to occur more frequently than on nearby lines.

Does the Ulam Spiral Predict Prime Numbers?

No. It displays arithmetic patterns after integers have been arranged and tested. A number can lie on a prime-rich diagonal and still be composite.

Are All Numbers on a Strong Diagonal Prime?

No. Even the most visible streaks contain breaks. Quadratic sequences often produce both prime and composite values.

Why Are There Fewer Marked Cells Farther From the Center?

Prime numbers become less dense on average as integers grow. The spiral reflects this long-term decrease while still showing local clusters and diagonal concentrations.

Is 1 Marked as Prime in the Center?

No. The number 1 has only one positive divisor, so it is neither prime nor composite. It serves only as the conventional center of the standard spiral.