Function Repository Resource:

UlamMatrix

Source Notebook

Generate the Ulam matrix

Contributed by: Jan Mangaldan

ResourceFunction["UlamMatrix"][n]

returns the n×n Ulam matrix.

Details and Options

In an n×n Ulam matrix, the first n2 positive integers are arranged in a counterclockwise spiral pattern, starting from the middle.
ResourceFunction["UlamMatrix"][,WorkingPrecisionp] gives a matrix with entries of precision p.

Examples

Basic Examples (2) 

A 5×5 Ulam matrix:

In[1]:=
ResourceFunction["UlamMatrix"][5]
Out[1]=

Visualize a large Ulam matrix:

In[2]:=
MatrixPlot[ResourceFunction["UlamMatrix"][90]]
Out[2]=

Show the same matrix mod 7:

In[3]:=
MatrixPlot[Mod[ResourceFunction["UlamMatrix"][90], 7]]
Out[3]=

Options (3) 

WorkingPrecision (3) 

By default, an exact matrix is computed:

In[4]:=
ResourceFunction["UlamMatrix"][4] // MatrixForm
Out[4]=

Use machine precision:

In[5]:=
ResourceFunction["UlamMatrix"][4, WorkingPrecision -> MachinePrecision] // MatrixForm
Out[5]=

Use arbitrary precision:

In[6]:=
ResourceFunction["UlamMatrix"][4, WorkingPrecision -> 20] // MatrixForm
Out[6]=

Applications (2) 

Mark the positions of prime numbers in the Ulam matrix and visualize the result:

In[7]:=
ArrayPlot[
 Map[Boole@*PrimeQ, ResourceFunction["UlamMatrix"][399], {2}]]
Out[7]=

Color numbers in the Ulam matrix by the number of their divisors:

In[8]:=
MatrixPlot[
 Map[Length@*Divisors, ResourceFunction["UlamMatrix"][199], {2}], ColorFunction -> (ColorData[61, Clip[#, {0, 9}]] &), ColorFunctionScaling -> False]
Out[8]=

Version History

  • 1.0.0 – 18 August 2023

Related Resources

License Information