Triangular numbers stack into a triangle, square numbers into a square, and the same idea works for any polygon. This polygonal numbers calculator takes the number of sides, a position n and a number to test to give the n-th polygonal number, the next polygonal number, the sum of the first n, the position of the tested number and whether it is a polygonal number.
How the calculation works
- n-th s-gonal number = ((s − 2) × n² − (s − 4) × n) ÷ 2, where s is the number of sides. For s = 3 this gives the triangular numbers, for s = 4 the squares and for s = 5 the pentagonal numbers.
- Next number is the same formula with n + 1. The gap between neighbors is (s − 2) × n + 1.
- Sum of the first n numbers = n × (n + 1) × ((s − 2) × (2n + 1) − 3 × (s − 4)) ÷ 12.
- Position of a tested number T = ((s − 4) + √((s − 4)² + 8 × (s − 2) × T)) ÷ (2 × (s − 2)). T is polygonal when this position is a whole number.
Example
With s = 5 sides and n = 10, the pentagonal number is (3 × 100 − 1 × 10) ÷ 2 = 145. The next one, for n = 11, is 176, and the sum of the first ten is 550. To test the number 210, the position is (1 + √(1 + 24 × 210)) ÷ 6 = (1 + 71) ÷ 6 = 12. It is a whole number, so 210 is the 12th pentagonal number.
Why it matters
Polygonal numbers are a staple of number theory, recreational math and programming exercises, and they appear in Fermat’s polygonal number theorem, which says that every positive integer is the sum of at most s polygonal numbers with s sides. The test is also a good example of solving a quadratic to invert a formula.
Good to know
The formula is valid for polygons with three or more sides and for whole positions of 1 or higher. Large positions give very large numbers, and the calculator rounds the display to whole numbers. The test of a number uses a tolerance for floating point errors, which is reliable for numbers below about a billion, the limit of the field.
Related tools
The triangular numbers calculator covers the case of three sides in more detail and the arithmetic and geometric series calculator gives sums of other kinds of sequences.