# General form of amount of triangles that can be formed in an MxN point lattice

I've been searching for the answer for many years, both by researching by myself and reading about the subject. Now I'm wondering if this has a solution.

The problem can be stated as follows.

Given a M x N grid of points, how many triangles with vertices in the grid can be formed? Note that you can't select two points that coincide or three collinear points because that wouldn't conform a triangle (area would be 0)

OK, I know a bit of programming and could manage to code a program that solves this, but would REALLY like to know if there is a general form depending on M and N. I suspect it has to do with prime numbers... (Perhaps I totally missed heheh)

Thanks for your time! Manuel

-
The answer is given by taking an appropriate binomial coefficient and then subtracting the number of colinear three-point configurations. – Steve Huntsman Feb 15 '10 at 14:39
Yeah, the thing is how many collinear three-point configurations to subtract. That's the real challenge. Think about it for a moment and you'll realize this is not as trivial as it may seem. – Manuel Feb 15 '10 at 17:19
The question is equivalent to count the collinear triples, for which it is not hard to write up a summation but I donno whether an explicit formula would exist. – domotorp Feb 15 '10 at 17:48
I added the number-theory tag, as this is more of a number theory problem than a combinatorial one. – Douglas Zare Feb 16 '10 at 8:27
It follows from the Szemeredi-Trotter theorem that the number of collinear triples in a finite set of points $P\subseteq\mathbb{R}^2$ is $< C|P|^2 log|P|$ for some absolute constant $C$, assuming that no line is incident to more than $\sqrt{|P|}$ points. Thus if $P$ is an $N\times N$ grid, the number of collinear triples is $\lesssim N^4\log N$. – Brendan Murphy Feb 3 at 4:42

## 2 Answers

For the NxN case, at least, there is a literature. An entry point is sequence A000938 in the On-Line Encyclopedia of Integer Sequences.

-

ok here is one answer, but I think, you already knew that:

so there are $\left((M+1)(N+1)\atop 3\right)$ possible triangles an we have to subtract the number of triangles, which are degenerated. Every degenerated triangle is given by a choice of 2 points $P=(x,y),P'=(x',y')$ and a choice of a lattice point on the segment from $P$ to $P'$. Let $R:=gcd(x-x',y-y')$. There should be exactly $R+1$ points on that line, so we get at least a method, how a computer programme could compute that number.

-
@HenrikRüping hey can you explaine more about R ? – user66661 Feb 3 at 3:02
Should it be $\ R-1\$ points ( **inside** interval $\ PP'$)? – Włodzimierz Holsztyński Feb 3 at 3:56