Skip to main content

figurate-numbers

figurate-numbers logo

Pure-Python library for polygonal and figurate numbers. Zero runtime dependencies.

Provides forward generation, membership testing, and inverse lookup for the full k-gonal family, plus pyramidal numbers, generalized centered polygonal numbers, star, pronic, and Catalan numbers.

Install

pip install figurate-numbers

Quick start

from figurate_numbers import (
    triangular, pentagonal, hexagonal, polygonal,
    is_triangular, is_polygonal,
    inverse_triangular, inverse_polygonal,
    tetrahedral, square_pyramidal,
    is_tetrahedral, inverse_tetrahedral,
    centered_polygonal, is_centered_polygonal,
)

triangular(10)          # 55
pentagonal(7)           # 70
hexagonal(5)            # 45
polygonal(10, 3)        # 55  (same as triangular)

is_triangular(28)       # True
is_triangular(50)       # False
is_polygonal(70, 5)     # True  (pentagonal)

inverse_triangular(55)  # 10
inverse_polygonal(45, 6)  # 5

# Pyramidal numbers (3D figurate)
tetrahedral(5)          # 35
square_pyramidal(5)     # 55
is_tetrahedral(20)      # True
inverse_tetrahedral(20) # 4

# Generalized centered polygonal
centered_polygonal(3, 4)  # 19  (centered triangular)
centered_polygonal(6, 3)  # 19  (centered hexagonal, same as centered_hexagonal(3))
is_centered_polygonal(4, 25)  # True  (centered square)

API

Generators

Function Formula OEIS
triangular(n) n(n+1)/2 A000217
square(n) n^2 A000290
pentagonal(n) n(3n-1)/2 A000326
hexagonal(n) n(2n-1) A000384
heptagonal(n) n(5n-3)/2 A000566
octagonal(n) n(3n-2) A000567
polygonal(n, k) n((k-2)n-(k-4))/2
pronic(n) n(n+1) A002378
centered_hexagonal(n) 3n(n-1)+1 A003215
star(n) 6n(n-1)+1 A003154
catalan(n) C(2n,n)/(n+1) A000108
tetrahedral(n) n(n+1)(n+2)/6 A000292
square_pyramidal(n) n(n+1)(2n+1)/6 A000330
centered_polygonal(sides, n) sidesn(n-1)/2+1 (sides=3: A005448, sides=4: A001844, sides=6: A003215)

All generators require n >= 1. polygonal additionally requires k >= 3. centered_polygonal additionally requires sides >= 3.

Membership

is_triangular, is_square, is_pentagonal, is_hexagonal, is_heptagonal, is_octagonal - test whether an integer belongs to the sequence.

is_polygonal(m, k) - general k-gonal membership test. Requires k >= 3.

is_tetrahedral(m), is_square_pyramidal(m) - pyramidal membership tests (integer cube-root + exact verification).

is_centered_polygonal(sides, m) - centered s-gonal membership test. Requires sides >= 3.

All membership functions use integer arithmetic only (no floats in results).

Inverse lookup

inverse_triangular(m) - returns n such that triangular(n) == m, or None.

inverse_polygonal(m, k) - returns n such that polygonal(n, k) == m, or None. Requires k >= 3.

inverse_tetrahedral(m) - returns n such that tetrahedral(n) == m, or None.

inverse_square_pyramidal(m) - returns n such that square_pyramidal(n) == m, or None.

Development

uv venv .venv
uv pip install -e ".[dev]"
pytest
ruff check .
mypy src

License

MIT

Metadata

Release files for figurate-numbers 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for figurate-numbers 0.2.0
File Size Uploaded
figurate_numbers-0.2.0.tar.gz 814.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for figurate-numbers 0.2.0
File Interpreter ABI Platform
figurate_numbers-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 821.9 kB

Release files / figurate_numbers-0.2.0.tar.gz

Download URL figurate_numbers-0.2.0.tar.gz
Size 814.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ef12ad8da2b1231ed69991f3dc339ac816935aa03f64f7cf4c048a2c090f5994
BLAKE2b-256 checksum
How to use checksums
494cd44410de279b9bb2e8bdf479dd31d86d45a7c5194d526b4ddf6c5a72bebf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / figurate_numbers-0.2.0-py3-none-any.whl

Download URL figurate_numbers-0.2.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c652215cfadc1bfdc2c73ed28fd1467054b4a3e04e52451e92babf5374c00209
BLAKE2b-256 checksum
How to use checksums
9c96e1f8843cd0d5fe49d35c2dca43ee6278490ce9c2162d43978da04da98838
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page