The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::PlanePath::SquareSpiral -- integer points drawn around a square

SYNOPSIS

 use Math::PlanePath::SquareSpiral;
 my $path = Math::PlanePath::SquareSpiral->new;
 my ($x, $y) = $path->n_to_xy (123);

DESCRIPTION

This path makes a square spiral.

    37--36--35--34--33--32--31         3
     |                       |
    38  17--16--15--14--13  30         2
     |   |               |   |
    39  18   5---4---3  12  29         1
     |   |   |       |   |   |
    40  19   6   1---2  11  28    <- y=0
     |   |   |           |   |
    41  20   7---8---9--10  27        -1
     |   |                   |
    42  21--22--23--24--25--26        -2
     |
    43--44--45--46--47 ...
                 ^
    -3  -2  -1  x=0  1   2   3

This is quite well known from Stanislaw Ulam finding interesting straight lines plotting the prime numbers on it. See examples/ulam-spiral-xpm.pl in the sources for a program generating that, or see the author's math-image program using this SquareSpiral to draw Ulam's pattern and more.

The perfect squares 1,4,9,16,25 fall on diagonals with the even perfect squares going to the upper left and the odd ones to the lower right. The pronic numbers 2,6,12,20,30,42 etc half way between the squares fall on similar diagonals to the upper right and lower left.

In general straight lines in this SquareSpiral and other stepped spirals (meaning everything except the VogelFloret) are quadratics a*k^2+b*k+c, with a=step/2 where step is how much longer each loop takes than the preceding, which is 8 in the case of the SquareSpiral. There are various interesting properties of primes in quadratic progressions like this and some quadratics seem to have more primes than others. For instance see PyramidSides for Euler's k^2+k+41.

Corners

Other spirals can be formed by cutting the corners of the square to loop faster.

    Corners Cut    Class
    -----------    -----
         1        HeptSpiralSkewed
         2        HexSpiralSkewed
         3        PentSpiralSkewed
         4        DiamondSpiral

And see the PyramidSpiral for a re-shaped SquareSpiral, looping at the same rate.

FUNCTIONS

$path = Math::PlanePath::SquareSpiral->new ()

Create and return a new square spiral object.

($x,$y) = $path->n_to_xy ($n)

Return the x,y coordinates of point number $n on the path.

For $n < 1 the return is an empty list, it being considered the path starts at 1.

$n = $path->xy_to_n ($x,$y)

Return the point number for coordinates $x,$y. $x and $y are each rounded to the nearest integer, which has the effect of treating each N in the path as centred in a square of side 1, so the entire plane is covered.

SEE ALSO

Math::PlanePath, Math::PlanePath::PyramidSpiral

Math::PlanePath::DiamondSpiral, Math::PlanePath::PentSpiralSkewed, Math::PlanePath::HexSpiralSkewed, Math::PlanePath::HeptSpiralSkewed

HOME PAGE

http://user42.tuxfamily.org/math-planepath/index.html

LICENSE

Math-PlanePath is Copyright 2010 Kevin Ryde

Math-PlanePath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-PlanePath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.