File Exchange

primespiral(varargi​n)

version 1.5 (43.4 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: 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).

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

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

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