What is the average distance between points in a square?

In my family we love riddles, puzzles and mathematical challenges and it’s not unusual that we share weird problems via our whatsapp group. A couple weeks ago, we received the following question from my brother Alvaro: “what is the average distance between two points in a square?”

Solving this problem mathematically with integrals exceeds my current level of skill, but I realised it would be rather easy to solve this for a discrete amount of points in a square NxN matrix. I could solve it for a matrix of 10×10, 20×20 and see whether the solution converged to a number.

With my raspberry pi, connected via ssh from my iphone, from the comfort of my sofa I wrote the following program in c that iterated with increasing sizes of square matrixes

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double distancia(int x1,int y1,int x2,int y2){
double distx=x2-x1;
double disty=y2-y1;
double distanciaalcuadrado=abs(pow(distx,2)+pow(disty,2));
double distancia=sqrt(distanciaalcuadrado);
return distancia;
}
void mediaPuntos(int intervalos){
double suma=0;
int contador=0;
double media=0.0;
double distancias=0.0;
int x1=1;
int x2=1;
int y1=1;
int y2=1;
double medianor=0;
for(x1=1;x1<=intervalos;x1++){
for(y1=1;y1<=intervalos;y1++){
for(x2=1;x2<=intervalos;x2++){
for(y2=1;y2<=intervalos;y2++){
contador++;
distancias=distancia(x1,y1,x2,y2);
suma=suma+distancias;
media=suma/contador;
medianor=media/intervalos;
//printf("%d:(%d,%d)-(%d,%d)=%f [%f/%d]=%f\n",contador,x1,y1,x2,y2,distancias,media,intervalos,medianor);
}
}
}
}
printf("%d:%d [%f/%d]=%f\n",intervalos,contador,media,intervalos,medianor);
}
void main(void){
printf("sumando distancias\n");
int intervalos=10;
int intervalosmax=100000;
for(;intervalos<=intervalosmax;intervalos++){
mediaPuntos(intervalos);
}
}
view raw meansquaredistance.c hosted with ❤ by GitHub

When reaching a 216×216 matrix, the number of combinations was 2 thousand million at which some variable must have exceeded the maximum size of a double type because I started getting negative results.


The result is that the mean distance between any two points in a square is 0.5214 times the length of the side. I have no clue what this ratio means mathematically.

Do you know what 0.5214 represents in a square of side 1? đŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s