Math::N...olygonal(3)

πŸ“¦
version 75 Β· 2 versions
, in fedora 43

✨ 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 $value is 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::NumSeq
  • Math::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