The Ulam prime spiral numbers a grid in a spiral from the centre and marks the prime numbers. The mathematician Stanisław Ulam drew it during a dull meeting in 1963 and noticed that the primes tend to line up along diagonals.
Quick answer: Number the grid with a spiral that starts at 1 in the centre, the reverse spiral matrix. Then print "* " for a prime and ". " for anything else. A short is_prime test decides each cell.
This program is part of the Spiral Patterns collection.
Table of contents8 sections
Ulam Prime Spiral Pattern output
Output (9 × 9):
. . * . . . . . * . . . * . . . * . . . . . * . . . * . . . . . * . * . * . * * . . * . . . * . * . * . . . . . * . . . * . * . * . . . . . * . . . * . * . . . .

How the ulam prime spiral pattern works
The numbering is the reverse spiral from the previous pattern: fill an inward spiral and print n² + 1 − value, which puts 1 in the centre. For every cell the program then asks one question, is this number prime? Looking at the result you can already see short diagonal runs of stars, the effect that made the spiral famous. On a large grid the diagonal lines become striking.
- Fill an
n × ngrid with zeros and start in the top-left cell. - For each value from 1 to the last one: write it in the current cell.
- Try to step in the current direction. If that cell is outside the grid or already filled, turn to the next direction and try again.
- For each cell, work out
v = n² + 1 − a[i][j]and print a star ifvis prime, otherwise a dot.
Key takeaways
- The grid is numbered outwards from the centre, with 1 in the middle.
- Only the printing step changes: prime or not.
- Primes cluster on diagonals, which is what Ulam noticed.
- Testing divisors up to the square root keeps the prime check fast.
Ulam Prime Spiral Pattern in C
#include <stdio.h>
int is_prime(int x) {
if (x < 2)
return 0;
for (int d = 2; d * d <= x; d++)
if (x % d == 0)
return 0;
return 1;
}
int main(void) {
int n = 9;
int a[20][20] = {0}; /* n can be up to 20 */
int dr[] = {0, 1, 0, -1}; /* right, down, left, up: clockwise */
int dc[] = {1, 0, -1, 0};
int r = 0, c = 0, d = 0;
for (int value = 1; value <= n * n; value++) {
a[r][c] = value;
for (int turn = 0; turn < 4; turn++) { /* find the next free cell */
int nr = r + dr[d], nc = c + dc[d];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
r = nr;
c = nc;
break;
}
d = (d + 1) % 4;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%s", is_prime(n * n + 1 - a[i][j]) ? "* " : ". ");
printf("\n");
}
return 0;
}Ulam Prime Spiral Pattern in C++
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
bool isPrime(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; d++)
if (x % d == 0)
return false;
return true;
}
int main() {
int n = 9;
vector<vector<int>> a(n, vector<int>(n, 0));
int dr[] = {0, 1, 0, -1}; // right, down, left, up: clockwise
int dc[] = {1, 0, -1, 0};
int r = 0, c = 0, d = 0;
for (int value = 1; value <= n * n; value++) {
a[r][c] = value;
for (int turn = 0; turn < 4; turn++) { // find the next free cell
int nr = r + dr[d], nc = c + dc[d];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
r = nr;
c = nc;
break;
}
d = (d + 1) % 4;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << (isPrime(n * n + 1 - a[i][j]) ? "* " : ". ");
cout << endl;
}
return 0;
}Ulam Prime Spiral Pattern in Java
public class UlamPrimeSpiral {
static boolean isPrime(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; d++)
if (x % d == 0)
return false;
return true;
}
public static void main(String[] args) {
int n = 9;
int[][] a = new int[n][n];
int[] dr = {0, 1, 0, -1}; // right, down, left, up: clockwise
int[] dc = {1, 0, -1, 0};
int r = 0, c = 0, d = 0;
for (int value = 1; value <= n * n; value++) {
a[r][c] = value;
for (int turn = 0; turn < 4; turn++) { // find the next free cell
int nr = r + dr[d], nc = c + dc[d];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
r = nr;
c = nc;
break;
}
d = (d + 1) % 4;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print((isPrime(n * n + 1 - a[i][j]) ? "* " : ". "));
System.out.println();
}
}
}Ulam Prime Spiral Pattern in Python
def is_prime(x):
if x < 2:
return False
d = 2
while d * d <= x:
if x % d == 0:
return False
d += 1
return True
n = 9
a = [[0] * n for _ in range(n)]
dr = [0, 1, 0, -1] # right, down, left, up: clockwise
dc = [1, 0, -1, 0]
r = c = d = 0
for value in range(1, n * n + 1):
a[r][c] = value
for turn in range(4): # find the next free cell
nr, nc = r + dr[d], c + dc[d]
if 0 <= nr < n and 0 <= nc < n and a[nr][nc] == 0:
r, c = nr, nc
break
d = (d + 1) % 4
for i in range(n):
for j in range(n):
print("* " if is_prime(n * n + 1 - a[i][j]) else ". ", end="")
print()Time and space complexity
Filling the grid takes O(n²) time. Each of the n² numbers is tested for primality in up to O(n) steps (its square root), so the whole program runs in O(n³) time with O(n²) memory.
Frequently asked questions
Why do the primes line up on diagonals?
Each diagonal of the spiral follows a quadratic formula such as 4k² + 2k + 1. Some of these formulas produce primes much more often than others, so their diagonals look busy. Nobody has fully explained the pattern yet.
How do I see the effect better?
Use a larger odd n, such as 41, and print a single character per cell instead of two. The diagonals stand out clearly at that size.
Where is 1 in the output?
In the centre cell, row 5 and column 5. It shows a dot, because 1 is not a prime.






