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. In general the k-gonals for k>=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 simplly 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 (key=>value,...)

Create and return a new sequence object.

$value = $seq->ith($i)

Return $i ** 2.

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

Return true if $value is a perfect square.

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/>.