MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: simplifying ulam spiral code

  • To: mathgroup at
  • Subject: [mg56477] Re: simplifying ulam spiral code
  • From: Paul Abbott <paul at>
  • Date: Tue, 26 Apr 2005 21:52:32 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <d4cmlr$39e$>
  • Sender: owner-wri-mathgroup at

In article <d4cmlr$39e$1 at>, zak <chocolatez at> 

> in the site:
> there is a mathematica code for drawing ULAM'S SPIRAL
> ....
> Please i hope someone transform it to a functional programming code.

Ulam's spiral appeared in the second issue of The Mathematica Journal, 
1(2): 39 (1990). It uses functional programming and complex numbers. 
I've updated that code here:

- Spiral on an Integer Lattice

The following code creates a spiral on an integer lattice:  

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

For example,

  spiral = IntegerSpiral[60];

Indexing the points on the spiral, select those points that have prime 
index (Pick is new in 5.1):

  primes = Pick[spiral, PrimeQ[Range[Length[spiral]]]];

Draw the spiral and display those with prime index as dots:

  spiralplot = ListPlot[spiral, PlotJoined->True,  Axes -> False,
   AspectRatio->Automatic, Epilog -> {PointSize[0.02], Point /@ primes}];

Some patterns in the distribution of the primes are apparent.

Paul Abbott                                   Phone: +61 8 6488 2734
School of Physics, M013                         Fax: +61 8 6488 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at 

  • Prev by Date: Re: odd mathematica blindspot
  • Next by Date: Re: Compile. Not again
  • Previous by thread: Re: simplifying ulam spiral code
  • Next by thread: Shell script and Mathematica.