Math::N...olygonal(3)
β¨ NAME β¨
Math::N...olygonal β bestie, it's giving polygonal numbers π
triangular, square, pentagonal, etc. This page lives in the User Contributed Perl Documentation, and yeah the man name got chopped to Math::N...olygonal(3) but it's really Math::NumSeq::Polygonal π«Ά
β¨ SYNOPSIS β¨
use Math::NumSeq::Polygonal;
my $seq = Math::NumSeq::Polygonal->new (polygonal => 7);
my ($i, $value) = $seq->next;
okay so that's literally it, three lines and you're generating heptagonal numbers like a main character π
πΈ DESCRIPTION πΈ
It's the sequence of polygonal numbers, no cap. 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. iconic behavior honestly β¨
In general the k-gonals for k>=3 are
P(i) = (k-2)/2 * i*(i+1) - (k-3)*i
The values are how many points are in a triangle, square, pentagon, hexagon, etc of side i. Like, for example the triangular numbers,
d
c c d
b b c b c d
a a b a b c a b c d
i=1 i=2 i=3 i=4
value=1 value=3 value=6 value=10
Or the squares,
d d d d
c c c c c c d
b b b b c b b c d
a a b a b c a b c d
i=1 i=2 i=3 i=4
value=1 value=4 value=9 value=16
Or pentagons (which should be a pentagonal grid, so skewing a bit here β not me apologizing, the original said it first π),
d
d d
c d c d
c c d c c d
b c b c c b c d
b b b b c b b c d
a a b a b c a b c d
i=1 i=2 i=3 i=4
value=1 value=5 value=12 value=22
The letters "a", "b" "c" show the extra added onto the previous figure to grow its points. Each side except two are extended. In general the k-gonals increment by k-2 sides of i points, plus 1 at the end of the last side, so
P(i+1) = P(i) + (k-2)*i + 1
periodt π
π Second Kind π
Option pairs => 'second' gives the polygonals of the second kind, which are the same formula but with a negative i. lowkey a plot twist but okay:
S(i) = P(-i) = (k-2)/2 * i*(i-1) + (k-3)*i
The result is still positive values, bigger than the plain P(i). For example the pentagonals are 0,1,5,12,22,etc and the second pentagonals are 0,2,7,15,26,etc. cute, they're siblings π¦
π¦ Both Kinds π¦
pairs => 'both' gives the firsts and seconds interleaved. P(0) and S(0) are both 0 and that value is given just once at i=0, so
0, P(1), S(1), P(2), S(2), P(3), S(3), ...
π Average π
Option 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 k odd and i is even. If k and i both odd then it's an 0.5 fraction. yes bestie, half-numbers, we don't gatekeep π
π FUNCTIONS π
See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes. (iykyk β it's the shared lore π)
$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"
β Random Access β
$value = $seq->ith($i)β Return the$i'th polygonal value, of the given"pairs"type. just hand it an index, it hands you the number, slay π«Ά$bool = $seq->pred($value)β Return true if$valueis a polygonal number, of the given"pairs"type. vibe check for numbers fr fr β$i = $seq->value_to_i_estimate($value)β Return an estimate of the i corresponding to$value. it's an estimate bestie, not a promise, don't come for me π
π SEE ALSO π
Math::NumSeqMath::NumSeq::Cubes
go read those too, they're the friend group π
πΈ HOME PAGE πΈ
<http://user42.tuxfamily.org/math-numseq/index.html>
π LICENSE π
Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2018, 2019, 2020, 2021 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/>.
no warranty bestie, the software is serving looks only, not legal guarantees π
okay that's the whole page, and the little footer whisper says this is perl v5.42.0, dated 2025-07-25, filed under Math::N...olygonal(3) β so yeah, that's the tea, bye bestie πβ¨
π¦ where this page comes from
- fedora 43: perl-Math-NumSeq 75-11.fc43 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms