# On the distribution of PIPs and Ramanuajn primes in Ulam's spiral

The Ulam's spiral represents the prime numbers by spiral. The code provided by Wolfram Mathematica is easy and given by

   Square[{x_, y_}, r_: .5] :=
Polygon[{{x - r, y + r}, {x + r, y + r}, {x + r, y - r}, {x - r,
y - r}}]
Square[{x_, y_}, r_: .5, color_] := {color,
Polygon[{{x - r, y + r}, {x + r, y + r}, {x + r, y - r}, {x - r,
y - r}}]
}
Square[x_, y_] := Square[{x, y}, .5]

IntegerSpiral[n_] := {Re[#], Im[#]} & /@
Fold[Join[#1, Last[#1] - I^#2 Range[#2/2]] &, {0}, Range[n]]

PrimeSpiral[n_] :=
Module[{spiral = Most[IntegerSpiral[n]], primes, rect},
primes = Pick[spiral, PrimeQ[Range[Length[spiral]]]];
rect = {-1, 1}/2 + Outer[Apply, {Min, Max}, Transpose[spiral], 1];
Graphics[{
{Yellow, Rectangle @@ rect},
Square[#, .5, Red] & /@ primes,
MapIndexed[Text[#2[[1]], #1] &, spiral]
}]
]

*First plot*
spiral = IntegerSpiral[60];
primes = Pick[spiral, PrimeQ[Range[Length[spiral]]]];
ListPlot[spiral, PlotJoined -> True,  Axes -> False,
AspectRatio -> Automatic,
Epilog -> {PointSize[0.02], Point /@ primes}, PlotRange -> All];

*Second plot*
g2 = Show[PrimeSpiral[22], AspectRatio -> Automatic]

*Third plot*
Options[PrimeSpiral2] = {
ColorRules -> {1 -> Black, 0 -> White},
PixelConstrained -> True
};
PrimeSpiral2[n_Integer?OddQ, opts___] := Module[
{p = PrimeQ[NumberSpiral[n]] /. {True -> 1, False -> 0}, colors},
ArrayPlot[p, opts, Sequence @@ Options[PrimeSpiral2]]
]

PrimeSpiral2[399, ImageSize -> 500] // Timing


that is

Is is possible to plot a version of the Ulam's spiral for some subsets of the primes? For instance for the set of superprimes {3, 5, 11, 17, 31, 41, 59, 67, 83, 109, . . .} and for that of Ramanujan primes {2, 11, 17, 29, 41, 47, 59, 67, 71, 97,...}?

Replacing PrimeQ[] with PrimeQ[PrimePi[ ]] we should have the superprimes. Unfortunately the squares in red are the arguments of the function PrimePi[ ] instead of the superprimes, as shown here

Hence the code should be modified now in its graphic subsection!

• I have tried with another function and it works. Maybe I was doing some mistakes. I am not the author of this code. It is given by Wolfram. The problem now is to switch on the superprimes. Unfortunately, there is not a function as PrimeQ for the superprimes and Ramanujan primes. – Spook82 Jan 4 at 20:41
• So the question boils down to how to define RamanujanPrimeQ etc, can you edit the question then? p.s. it is always good idea to link the source of the code you are quoting. – Kuba Jan 4 at 22:33
• Yes, you are right. I have tried to do something similar for the superprimes by replacing PrimeQ[ ] with PrimeQ[PrimePi[ ]]. It should work but unfortunately the squares in red are the arguments of the function PrimePi[ ] instead of the superprimes. Hence the code should be modified now in its graphic subsection. – Spook82 Jan 5 at 0:43
• Please do not add irrelevant tags. Or did I miss a relationship to graphs or to complex numbers? – Szabolcs Jan 5 at 22:14
• As you can see in code, the IntegerSpiral is plotted by using Re[ ] and Im[ ]. This links this spiral with complex numbers. – Spook82 Jan 6 at 13:36