Counting All Lattice Rectangles in the Square Grid in Near-Linear Time
Abstract
We study the exact counting problem for all lattice rectangles contained in the square , including non-axis-parallel ones. Starting from the standard parametrization by a primitive direction and two side lengths, we derive several exact algorithms: the classical sweep, decompositions of complexity and , a ten-moment weighted-floor-sum reduction of complexity , and a divisor-layer algorithm with the complexity . We also give an all-values algorithm that computes in arithmetic operations. The main idea behind the near-linear one-value algorithms is to reduce the geometric summation to constant-size families of weighted floor sums closed under Euclidean-style affine and reciprocal transformations. Besides the exact algorithmic results, we derive a two-term asymptotic expansion, with the explicit formula for , which provides an independent consistency check for the large- numerical data produced by the algorithms.
Cite
@article{arxiv.2604.22456,
title = {Counting All Lattice Rectangles in the Square Grid in Near-Linear Time},
author = {Dmitry Babichev and Sergey Babichev},
journal= {arXiv preprint arXiv:2604.22456},
year = {2026}
}
Comments
Full version of an extended abstract submitted to MFCS 2026. Updated version with new near-linear and all-values algorithms, plus expanded experiments