Learn R Programming

numbers (version 0.9-2)

polygonal: Polygonal Numbers

Description

Computes the k-polygonal number(s) for an integer or a sequence of integers.

Usage

polygonal(k, n)

Value

A natural number or a vector of such numbers.

Arguments

k

single natural number, the number of sides of the polygon.

n

the n-th number of all k-polygonal number; can be a vector.

Details

A polygonal number is a number of dots that can be laid out to form a regular k-sided polygon, incl. triangular (k=3), square (k=4), pentagonal (k=5) and hexagonal (k=6) numbers. See the Wikipedia article "Polygonal Numbers" for visualizations of these figurative numbers .

k=3 Triangular: 1 3 6 10 15 21 28 36 45 55 ...
k=4 Square : 1 4 9 16 25 36 49 64 81 100 ...
k=5 Pentagonal: 1 5 12 22 35 51 70 92 117 145 ...
k=6 Hexagonal : 1 6 15 28 45 66 91 120 153 190 ...
...

References

S.A. Khan. Sums of Reciprocals of Polygonal Numbers and a Theorem of Gauss. Intern. Journal of Applied Mathematics. Vol. 33 (2020), No. 2, pp. 265-282.

CCY Kwan. The Sum of Reciprocals of Polygonal Numbers: A Spreadsheet-Based Illustration. 'Spreadsheets in Education' Journal, August 2025.

See Also

the arithmetic progression function arithmetic_progression().

Examples

Run this code
polygonal(3, 1:10)  #  1   3   6  10  15  21  28  36  45  55
polygonal(4, 1:10)  #  1   4   9  16  25  36  49  64  81 100
polygonal(5, 1:10)  #  1   5  12  22  35  51  70  92 117 145
polygonal(6, 1:10)  #  1   6  15  28  45  66  91 120 153 190

# Sums of reciprocals of polygonal numbers:
n = 1000
sum(1/polygonal(3, 1:n)) # 1.998002 -> 2.0
sum(1/polygonal(4, 1:n)) # 1.643935 -> pi^2/6
sum(1/polygonal(5, 1:n)) # 1.481371 -> 3*log(3)-pi/sqrt(3)
sum(1/polygonal(6, 1:n)) # 1.385794 -> 2*log(2)

Run the code above in your browser using DataLab