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.
- d=15
- verti=Sequence(Segment((k, -d), (k, d)), k, -d, d)
- hori=Sequence(Segment((-d, k), (d, k)), k, -d, d)
Comment.
- d is the dimension of the grid, which will extend from (-d,-d) lower left to (d,d) upper right.
- verti is the list of vertical segments in the grid.
- hori contains the horizontal segments.
B. The square spiral
Introduction.
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.
- plus=Sequence(Sequence(1, m, 1, k), k, 1, 2d)
- minus=Sequence(Sequence(-1, m, 1, k), k, 1, 2d)
- zero=Sequence(Sequence(0, m, 1, k), k, 1, 2d)
- odds=Sequence(k, k, 1, 2d - 1, 2)
- xstepsb=Zip({plus(A), zero(A), minus(A + 1), zero(A + 1)}, A, odds)
- xsteps=Flatten(xstepsb)
- xstepsclose=Sequence(1, i, 1, 2d)
- xstepsclosed=Join({xsteps, xstepsclose})
- xs=Sequence(Sum(xstepsclosed(m), m, 1, k), k, 1, Length(xstepsclosed))
- ystepsb=Zip({zero(A), plus(A), zero(A + 1), minus(A + 1)}, A, odds)
- ysteps=Flatten(ystepsb)
- ystepsclose=Sequence(0, i, 1, 2d)
- ystepsclosed=Join({ysteps, ystepsclose})
- ys=Sequence(Sum(ystepsclosed(m), m, 1, k), k, 1, Length(ystepsclosed))
- xsys=Sequence((xs(k), ys(k)), k, 1, Length(xstepsclosed))
- points=Append((0, 0), xsys)
- spiral=Polyline(points)
Comment.
- plus is the sequence {{1},{1,1},{1,1,1},...} with the last term containing 2d numbers.
- minus is {{-1},{-1,-1},{-1,-1,-1},...}.
- zero is {{0},{0,0},{0,0,0},...}.
- odds is the sequence {1,3,5,...,2d-1}, used in the two ZIP operations, in which the step is 2.
- 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.
- xsteps is the sequence (a) mentioned above.
- xstepsclose is a constant sequence of 2d numbers 1.
- xstepsclosed contains the steps in the abscissa up to the last point (-d,d).
- 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.
- is 5. for ordinates.
- is 6. for ordinates, resulting in the sequence (o) mentioned above.
- is 7. for ordinates.
- is 8. for ordinates.
- is 9. for ordinates.
- xsys is the list of successive coordinates of all points on the spiral except the origin.
- points is {(0,0),(1,0),(1,1),...(d,-d)}, the list of all points on the spiral.
- spiral is the square spiral starting in (0,0) and ending in (d,-d).
C. Ulam's spiral
GeoGebra commands.
- gridnumber=(2d + 1)²
- lastprime=PreviousPrime(gridnumber)
- gridprime=KeepIf(IsPrime(a), a, Sequence(gridnumber))
- ulam=Sequence(points(gridprime(k)), k, 1, Length(gridprime))
Comment.
- gridnumber is the number of points in the grid, i.e., on the spiral.
- lastprime is the greatest prime number in the sequence {1,2,...,(2d + 1)²}.
- gridprime contains the prime numbers in {1,2,...,(2d + 1)²}.
- 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.