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 [0,n)×[0,n), including non-axis-parallel ones. Starting from the standard parametrization by a primitive direction (u,v) and two side lengths, we derive several exact algorithms: the classical O(n2) sweep, decompositions of complexity O(n3/2 n) and O(n4/3 n), a ten-moment weighted-floor-sum reduction of complexity O(n3 n), and a divisor-layer algorithm with the complexity O(n2 n). We also give an all-values algorithm that computes F(1),…,F(N) in O(N3/2) 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, F(n)=4 2-1π2n4 n+B\,n4+o(n4) with the explicit formula for B, which provides an independent consistency check for the large-n numerical data produced by the algorithms.
Turn this paper into a lesson
ArcXiv compiles a structured reading guide from this paper's metadata: plain-English importance, contributions, prerequisite concepts, which sections to read first, flashcards, and a quiz. Grounded in the abstract, never invented.