I am a hobby computer scientist and searching for an algorithm to construct a set of **n** numbers (integers) with certain properties.


**Property 1 / Step 1**


All pairwise differences of the elements should be **unique**. From what I read, I think this is called 'difference set'.


**Property 2 / Step 2**


Having constructed all differences in step 1, we build the differences of the new set again. The resulting elements should also be as unique as possible. Meaning equal differences should be minimal.


**What I have so far**


It is clear to me, that "step 1" implies, that the largest number in the set is of order n². I was hoping that after "step 2" the largest number would not increase too much (like n² \* log(n)), but my simulations suggest an order of n³.


**Example 1**


The set {0,1,4,9} fullfills poperty 1, but it fails on property 2.


After steps 1 we have the difference set {0, 1, 3, 4, 5, 8, 9}. If arranged in a table one can write:



```
0 1 4 9   
  0 3 8
    0 5
      0

```

It is clear, that 9-4 and 8-3 have the same difference. Due to the construction some differences cannot be prevented.


But 5-1 = 4-0 is a difference which should not happen in step 2.


**Example 2**


The set {0,1,4,13} fullfills poperty 1 and property 2.



```
0 1 4 13   
  0 3 12
    0  9
       0

```

The only equal differences occur, when four choosen numbers form a "rectangle", like 13-4 = 9 -0


I would really appreciate any help!



Difference set of difference set
I’m assuming from the examples that you are only considering *non-negative* differences.


Let us first see what “as unique as possible” means. If the original set is $\{a\_i:0\le i<n\}$, the set of differences is
$$\{0\}\cup\{a\_i-a\_j:i,j<n,a\_i>a\_j\},$$
thus one checks easily that the set of second-order differences consists of the elements


* $0$,
* $a\_i-a\_j$ for $i,j<n$ such that $a\_i>a\_j$,
* $|a\_i+a\_j-a\_k-a\_l|$ for $i,j,k,l<n$ such that $a\_i>a\_k\ge a\_j$, $a\_k>a\_l$.


Moreover, all the expressions above are pairwise distinct if the $a\_i$'s are sufficiently linearly independent. Thus, the maximal size of the second-difference set is
$$2\binom n4+\binom n3+\binom n2+1=\frac{n^4}{12}+O(n^3).$$
Consequently, any set $\{a\_i:i<n\}$ of non-negative integers that attains this maximum has to have $\max\_ia\_i=\Omega(n^4)$.


It is possible to meet the $O(n^4)$ bound by a modification of the [Erdős–Turán construction](https://en.wikipedia.org/wiki/Golomb_ruler#Erd%C5%91s%E2%80%93Tur%C3%A1n_construction) of Golomb rulers. Given $n$, we fix a prime $p$ such that $p\ge\max\{n,5\}$ and $p=(1+o(1))n$. Then we define a set $\{a\_i:0\le i<n\}$ of numbers $a\_i<64p^3n=(64+o(1))n^4$ by
$$a\_i=(4p)^3i+(4p)^2(i^2\bmod p)+4p(i^3\bmod p)+(i^4\bmod p).$$



> 
> **Lemma:** $a\_i+a\_j+a\_k+a\_l=a\_{i'}+a\_{j'}+a\_{k'}+a\_{l'}$ only if $\{i,j,k,l\}=\{i',j',k',l'\}$ as multisets (i.e., including multiplicities).
> 
> 
> 


**Proof:** By considering base-$4p$ expansion, we have
$$i^d+j^d+k^d+l^d\equiv i'^d+j'^d+k'^d+l'^d\pmod p$$
for each $d\le 4$, thus using [Newton’s identities](https://en.wikipedia.org/wiki/Newton%27s_identities) in $\mathbb F\_p$, we obtain
$$e\_d(i,j,k,l)\equiv e\_d(i',j',k',l')\pmod p$$
for each $d\le 4$, where $e\_d(x\_0,x\_1,x\_2,x\_3)$ denotes elementary symmetric polynomials. In other words, we get the identity of polynomials
$$(X-i)(X-j)(X-k)(X-l)=(X-i')(X-j')(X-k')(X-l')$$
in $\mathbb F\_p[X]$. Since this polynomial uniquely determines $\{i,j,k,l\}\subseteq\mathbb F\_p$ as the multiset of its roots, the lemma follows.



> 
> **Corollary:** $a\_i+a\_j-a\_k-a\_l=a\_{i'}+a\_{j'}-a\_{k'}-a\_{l'}$ only if $\{i,j,k',l'\}=\{i',j',k,l\}$ as multisets.
> 
> 
> 


In other words, the second-order differences $a\_i+a\_j-a\_k-a\_l$ coincide only when necessary; thus, the $2\binom n4+\binom n3+\binom n2+1$ expressions for elements of the second-order difference set of $\{a\_i:i<n\}$ as given in the beginning of this post are pairwise distinct.


