File Exchange

## Primespiral

version 2.0 (225 KB) by

Play into the world of prime numbers!

Updated

Primespiral is a function to explore the distribution of prime numbers arranged into a spiral pattern.
The well-known Ulam spiral and the variant developed by Robert Sacks,
the Sacks spiral, show interesting geometric patterns in the positions of primes.
Syntax: primesout=primespiral(t)

Inputs:
t - is an integer number. The spiral will be plotted between 0 and t
If t is omitted, it will be set to 3571 (between 0 and 3571
there are 500 prime numbers).
Outputs:
primesout - the primes of the choosed family, between 0 and t.

The function will ask if you want to use the Ulam, Sacks, Vogel or Archimede spiral.
Then it will ask if you want to highlight a particular prime family.

Outputs:
The primes of the family you choosed between 0 and t

Created by Giuseppe Cardillo
giuseppe.cardillo-edta@poste.it

To cite this file, this would be an appropriate format:
Cardillo G. (2014) Primespiral: Play into the world of prime numbers!
http://www.mathworks.com/matlabcentral/fileexchange/46025

Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

Thank you Stephen for your review. This function was born many years ago as a game due to my love for prime numbers and Riemann. So. I was not so concentrated to write a fine script (as I usually do) but only to obtain the result to see prime numbers disposition. It is an ugly and dirt function I know but... It is mesmerizing and I love so much.
If you want, we could rewrite it toghether

Stephen Cobeldick

### Stephen Cobeldick (view profile)

What an interesting idea: it really supports an impressively large number of "prime family" definitions!

I really like the code comments with links and explanations for the different "prime families". The Mfile help is concise and clear.

Some aspects of the design and code should be improved:

1. The global variables could be easily avoided by using nested functions:

2. Currently to change the spiral type or highlighting requires calling the function again, which closes all open figures (Why? What did they do to deserve that?) and replots everything from scratch. It would be easy to create the figure to include drop-down menus instead, which would be a neater solution (and would not destroy all other figures that I might have open). Using a dialog box for each option is not a particularly convenient design: it is much simpler for the user to have all options available in one figure via some drop-down menus, which means they can mix-and-match and try out the different options much faster than calling the function again each time and clicking through some pesky dialog boxes.

3. The long definitions of the different families should be kept in a local function, and then the main function would be simpler and more easily understood: for complex UI's it often helps when the main function acts more like a backbone connecting local/nested functions, each of which encapsulate some specific functionality.

4. All graphics functions and objects should return and use explicit graphics handles. It is not robust to assume that the current figure/axes/... are always going to be the correct object to refer to.

5. The input checking is very strange, and far too complex for one simple numeric input. Using VARARGIN is overkill and is a bad practice if not required (no variable hinting in the editor or command line: VARARGIN should be avoided unless there really are an indeterminate number of input arguments). Currently this code is just far too complex for *one* simple numeric scalar: the input checking uses two cell arrays, one structure, and two numerics, which get cleared afterwards anyway! Here is a simpler solution:

function primesout = primespiral(num)
if nargin
assert(isnumeric(num)&&isscalar(num)&&isreal(num),'Input num must be a real scalar numeric.')
assert(isfinite(num)&&num>0&&fix(num)==num,'Input num must be a positive integer.')
else
num = 3571;
end

Note how this is simpler (yet has more thorough input checking), does not define any intermediate variables, does not require clearing anything, and does not create multiple copies of the data in multiple variables of multiple classes.

Summary: With some improvements this could easily be a model submission on MATLAB FEX. Its interface, which currently relies on dialog boxes and repeatedly calling the function to make any changes to the plot, needs to be improved.

Bob Elders

### Bob Elders (view profile)

Really cool indeed!

alexander

### alexander (view profile)

really cool

Markus Petershofen

### Markus Petershofen (view profile)

 15 Mar 2018 2 Github link 23 Feb 2018 2 each plot contains two boxes with descriptions of chosen spiral and prime family 19 Feb 2018 1.6 The function is more compact and rational. Archimede spiral was added (to shows that prime numbers greater then 7 have 1, 3, 7 or 9 as last digit) 13 Feb 2018 1.5 some minor bugs correction and new primes families added 28 Mar 2017 1.4 some other primes families were added 20 Oct 2014 1.3 Ulam Spiral added; more simplex algorithm 28 Apr 2014 1.2 bug correction in Dihedral primes computation; minimal primes added 27 Mar 2014 1.1 change in help section to correctly cite the file
##### MATLAB Release
MATLAB 8.4 (R2014b)

### MATLAB Online Live Editor Challenge

Win cash prizes and have your live script featured on our website