26 July 2025

Ulam's Spiral in GeoGebra

Ulam's Spiral is a square spiral of natural numbers, on which the prime numbers are highlighted. (See here.) In this post we implement it in GeoGebra 5 (manual here). 

A. The grid


GeoGebra commands.
  1. d=15
  2. verti=Sequence(Segment((k, -d), (k, d)), k, -d, d)
  3. hori=Sequence(Segment((-d, k), (d, k)), k, -d, d)
Comment. 
  1. d is the dimension of the grid, which will extend from (-d,-d) lower left to (d,d) upper right. 
  2. verti is the list of vertical segments in the grid.
  3. hori contains the horizontal segments.





 B. The square spiral


Introduction.

To generate the counterclockwise square spiral, the steps (Right, Up, Left, Down) starting from the origin (0,0) are: 

R U LL DD RRR UUU LLLL DDDD ...     (*)

The last complete cycle right-up-left-down is

R(2d-1 times) U(2d-1 times) L(2d times) D(2d times)

and it ends in (-d,-d). The effects of (*) on the abscissas are

+1 0 -1 -1 0 0 +1 +1 +1 0 0 0 -1 -1 -1 -1 0 0 0 0 ...     (a)

and on the ordinates

0 +1 0 0 -1 -1 0 0 0 +1 +1 +1 0 0 0 0 -1 -1 -1 -1 ...     (o) 

These sequences are obtained as xsteps and ysteps below. To end in (-d,d) there are 2d more steps right required. 

GeoGebra commands.

  1. plus=Sequence(Sequence(1, m, 1, k), k, 1, 2d)
  2. minus=Sequence(Sequence(-1, m, 1, k), k, 1, 2d)
  3. zero=Sequence(Sequence(0, m, 1, k), k, 1, 2d)
  4. odds=Sequence(k, k, 1, 2d - 1, 2)
  5. xstepsb=Zip({plus(A), zero(A), minus(A + 1), zero(A + 1)}, A, odds)
  6. xsteps=Flatten(xstepsb)
  7. xstepsclose=Sequence(1, i, 1, 2d)
  8. xstepsclosed=Join({xsteps, xstepsclose})
  9. xs=Sequence(Sum(xstepsclosed(m), m, 1, k), k, 1, Length(xstepsclosed))
  10. ystepsb=Zip({zero(A), plus(A), zero(A + 1), minus(A + 1)}, A, odds)
  11. ysteps=Flatten(ystepsb)
  12. ystepsclose=Sequence(0, i, 1, 2d)
  13. ystepsclosed=Join({ysteps, ystepsclose})
  14. ys=Sequence(Sum(ystepsclosed(m), m, 1, k), k, 1, Length(ystepsclosed))
  15. xsys=Sequence((xs(k), ys(k)), k, 1, Length(xstepsclosed))
  16. points=Append((0, 0), xsys)
  17. spiral=Polyline(points)
Comment.
  1. plus is the sequence {{1},{1,1},{1,1,1},...} with the last term containing 2d numbers.
  2. minus is {{-1},{-1,-1},{-1,-1,-1},...}. 
  3. zero is {{0},{0,0},{0,0,0},...}. 
  4. odds is the sequence {1,3,5,...,2d-1}, used in the two ZIP operations, in which the step is 2. 
  5. xstepsb is the sequence {{{1}, {0}, {-1, -1}, {0, 0}}, {{1, 1, 1}, {0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}, ...}, consisting of two sets of 1 term, then two of 2 terms, three of 3 terms etc.
  6. xsteps is the sequence (a) mentioned above.
  7. xstepsclose is a constant sequence of 2d numbers 1.
  8. xstepsclosed contains the steps in the abscissa up to the last point (-d,d).
  9. xs is {1, 1, 0, -1, -1, -1, ..., d}, the successive partial sums of the previous list, hence the successive abscissas of the points on the spiral.
  10. is 5. for ordinates.
  11. is 6. for ordinates, resulting in the sequence (o) mentioned above.
  12. is 7. for ordinates.
  13. is 8. for ordinates.
  14. is 9. for ordinates.
  15. xsys is the list of successive coordinates of all points on the spiral except the origin.
  16. points is {(0,0),(1,0),(1,1),...(d,-d)}, the list of all points on the spiral. 
  17. spiral is the square spiral starting in (0,0) and ending in (d,-d).


C. Ulam's spiral 


 GeoGebra commands.
  1. gridnumber=(2d + 1)²
  2. lastprime=PreviousPrime(gridnumber)
  3. gridprime=KeepIf(IsPrime(a), a, Sequence(gridnumber))
  4. ulam=Sequence(points(gridprime(k)), k, 1, Length(gridprime))
Comment.
  1. gridnumber is the number of points in the grid, i.e., on the spiral.
  2. lastprime is the greatest prime number in the sequence {1,2,...,(2d + 1)²}.
  3. gridprime contains the prime numbers in {1,2,...,(2d + 1)²}.
  4. ulam is {(1,0),(1,1),(-1,1),(-1,-1),(2,0),...}, the sequence of the points on the spiral  whose sequence number is prime.