English

Counting Lattice Rectangles in $O(n\log n)$ Operations

Computational Geometry 2026-07-20 v1

Abstract

Let F(n)F(n) be the number of rectangles, not necessarily axis-parallel, whose vertices belong to the n×nn\times n square grid of lattice points. We give an exact algorithm that computes one prescribed value F(n)F(n) in O(nlogn)O(n\log n) arithmetic operations and O(n3/4)O(n^{3/4}) arithmetic words of working memory. The algorithm decomposes the count into M\"obius divisor layers, partitions weighted floor-moment queries by a truncated Euclidean coefficient-cone recursion, and reuses uniform marker grids along common coefficient paths. Each marker requires only its uniform cell and constant-size corrections at nearby boundaries, which select an exact precompiled cell operator. All integer operands have O(logn)O(\log n) bits. An exact 128-bit C++ implementation for the reported input range is compared experimentally with the previous O(nlog2n)O(n\log^2 n) algorithm.

Cite

@article{arxiv.2607.17961,
  title  = {Counting Lattice Rectangles in $O(n\log n)$ Operations},
  author = {Dmitry Babichev and Tatiana Shpakova},
  journal= {arXiv preprint arXiv:2607.17961},
  year   = {2026}
}