# Find $n$ given average distance between $0 \le a \le n-1$ and $1 \le b \le n-1$

Given a natural number $$n>1$$,

natural numbers $$a$$ are chosen uniform randomly between $$0$$ and $$n-1$$ i.e.: $$0 \le a \le n - 1$$

and

natural numbers $$b$$ are chosen uniform randomly between $$1$$ and $$n-1$$ i.e.: $$1 \le b \le n - 1$$ The average $$\delta$$ of the distance $$|a-b|$$ between numbers $$a$$ and $$b$$ equals $$6.5$$.

What is the given value of $$n$$?

First, my true apologies for, initially, not having been conform forum guidelines.

Have a look at answer provided, also upon request (by @lulu), where formula is derived.

Thanks all for getting me on rails.

I personally believe the formula is strikingly simple. Some people would perhaps expect numerical methods to be required for general reasoning, but not so.

Here is the motivation: average distance in this particular case (note $$0 \le a$$ and $$1 \le b$$) turns out to have a very simple formula in terms of $$n$$, allowing to, conversely, easily find $$n$$ in function of it. As it so happens, linear Gauss formula, and its quadratic generalization, in this particular case, both contain a common factor that perfectly cancels out for probability computation.

I have used (in disguise) the formulas for two puzzles (112210 112821) on puzzling exchange where also, conversely, some amount is asked in function of some average. So now I thought to present the math.

Have a nice day. I hope to contribute in simplicity.

To be honest: I doubted myself about the validity of such answer and therefore presented the question to the kind exchange community.

• What have you tried? Dec 18, 2021 at 16:19
• I suggest: pick a number like $n=10$ or whatever and compute the expected value of $|a-b|$. That ought to give you a pretty good idea.
– lulu
Dec 18, 2021 at 17:22
• I would ideally want to see equation(s) that express $n$ in function of the average distance, and/or vice versa, so one can find $n$ by solving such equation(s). Dec 18, 2021 at 19:07
• Please edit your post to include your efforts. I would not expect a simple closed formula, numerical methods will almost certainly be required.
– lulu
Dec 18, 2021 at 21:55
• I think this question and its answer have some interest, but its format is very unusual and confusing. If it was written in a more standard way, I would vote to re-open. Dec 21, 2021 at 0:26

$$n = 20$$

To calculate average distance between $$a$$ and $$b$$ there are a total amount of $$n(n-1)$$ pairs $$(a,b)$$ with: $$0<=a<=n-1$$ and $$1<=b<=n-1$$ so consider the $$n*(n-1)$$ matrix:

$$\begin{bmatrix}|0-(n-1)| & .. & |(n-1)-(n-1)|\\: & |a-b| & :\\|0-1| & ... & |(n-1)-1|\end{bmatrix}$$

The total of all distances between $$a$$ and $$b$$ is the total of the distances between all pairs in the square $$n*n$$ matrix:

$$\begin{bmatrix}|0-(n-1)| & .. & |(n-1)-(n-1)|\\: & |a-b| & :\\|0-0| & ... & |(n-1)-0|\end{bmatrix}$$

minus the total of the distances between all pairs from the excluded $$n*1$$ bottom row:

$$\begin{bmatrix}|0-0| & .. & |a-b| & .. & |(n-1)-0|\end{bmatrix}$$

giving (using standard formulas) : $$(n-1)n(n+1)/3 - (n-1)n/2$$ where the total amount of pairs $$n(n-1)$$ nicely cancels out in both terms and the average of the distance between $$a$$ and $$b$$ therefore is $$(n+1)/3 - 1/2 = (2n-1)/6$$ such gives $$n=20$$ for $$\delta=6.5$$

• Do we need all these spoiler tags? This is a maths Q&A website. People know they would be "spoiled" when reading an answer. Dec 21, 2021 at 0:27
• @Taladris ... I am very sorry and apologise ... I come from puzzling exchange where spoilers are 'part of the game' etiquette and the challenge is to find an answer rather than getting one. I will remove spoilers if you think that would help draw attention conform to math exchange policy. Again, sorry for clumsyness, I really wanted to show and have verified an IMO cool mathematical fact. Dec 21, 2021 at 0:36
• All right :-) spoilers removed. Thanks for the constructive comments. $\delta = (2n-1)/6$ is (hopefully) the correct formula I humbly wanted to present... Dec 21, 2021 at 0:48