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

NAME

Math::NumSeq::Polygonal -- polygonal numbers, triangular, square, pentagonal, etc

SYNOPSIS

 use Math::NumSeq::Polygonal;
 my $seq = Math::NumSeq::Polygonal->new (polygonal => 7);
 my ($i, $value) = $seq->next;

DESCRIPTION

The sequence of polygonal numbers. The 3-gonals are the triangular numbers i*(i+1)/2, the 4-gonals are squares i*i, the 5-gonals are pentagonals (3i-1)*i/2, etc.

The numbers are how many points are in a triangle, square, penatgon, hexagon, etc of side i,

      Squares                      Pentagons

    *          i=1  1                *

                                     *
    * *        i=2  3              *   *
    * *                             * *

                                
                                      *      
    * * *                           *   *    
    * * *      i=3  6             *  *    *  
    * * *                          *   * *   
                                    * * *    



                                       * 
    * * * *                          *           * * *     
    * * * *    i=4  10             *   *      *  *   *    *
    * * * *                      *  *    *     *  *    * * 
    * * * *                       *   * *       *   * * *  
                                   * * *         * * * *   
From a given i, the next value is formed by adding i+1, being a new row of
that length on the bottom of the triangle.






  In general the k-gonals for kE<gt>=3 are

    P(i) = (k-2)/2 * i*(i+1) - (k-3)*i

pairs => 'second' gives the polygonals of the second kind, which are the same formula negating the i.

    S(i) = (k-2)/2 * i*(i-1) + (k-3)*i

The result is positive values, bigger than the plain polygonals. For example the pentagonals are 0,1,5,12,22,etc and the second pentagonals are 0,2,7,15,26,etc.

pairs => 'both' gives the firsts and seconds interleaved. P(0) and S(0) are both 0 and is given just once at i=0,

    0, P(1),S(1), P(2),S(2), P(3),S(3), ...

pairs => 'average' is the average of the first and second, which ends up being simply a multiple of the perfect squares,

    A(i) = (P(i)+S(i))/2
         = (k-2)/2 * i*i

This is an integer if k is even, or if k is odd but i is even. If both k and i odd then it's an 0.5.

FUNCTIONS

See "FUNCTIONS" in Math::NumSeq for the behaviour common to all path classes.

$seq = Math::NumSeq::Polygonal->new ()
$seq = Math::NumSeq::Polygonal->new (pairs => $str)

Create and return a new sequence object. The default is the polygonals of the "first" kind, or the pairs option (a string) can be

    "first"
    "second"
    "both"
    "average"
$value = $seq->ith($i)

Return the $i'th polygonal value, of the given pairs type.

$bool = $seq->pred($value)

Return true if $value is a polygonal number, of the given pairs type.

SEE ALSO

Math::NumSeq, Math::NumSeq::Cubes

HOME PAGE

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

LICENSE

Copyright 2010, 2011 Kevin Ryde

Math-NumSeq 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-NumSeq 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-NumSeq. If not, see <http://www.gnu.org/licenses/>.