Unique Aggregation of Numbers

74 Views Asked by At

Suppose I have 5 numbers: 1,7,13,5,6. I want to perform some kind of algorithm that allows me to derive a number such that if any of those numbers changed in value or switched in order that I would get a different aggregate.

The aggregate must be unique. In other words the only way to get that aggregate is using exactly those 5 numbers in that exact order.

I have something that is passing all my initial testing but would love to have other people weight in.

Proposed: 1^2 + 1^2 + 7^2 + 2^2 + 13 ^ 2 + 3 ^ 2 + 5^2 + 4 ^2 + 6 ^ 2 + 5 ^ 2

Explanation: I square the number add that to the square of the position it is in the list and then accumulate a grand total.

Bonus: Provided that the proposed solution does work. Any solution that would produce a smaller number or require less operations would be fantastic.

2

There are 2 best solutions below

6
On

It is not unique, even up to permutation of the numbers. Note the sum of the squares of the positions just gives a constant depending on the length of the string. We know $\sum_{i=1}^ni^2=\frac 16n(n+1)(2n-1)$, so in your case it is $45$. The simplest example that fails is $(0,0,0,2)$ and $(1,1,1,1)$. Another is $(3,4)$ and $(0,5)$

To get a provably unique value for every string of naturals the values will be large. You can do this with the Cantor pairing function, which can be extended to arbitrary length lists, but it gets complicated. Easier would be to use a hash function.

0
On

Assuming that your numbers are constrained to be integers (though this was not specified) then here's a way to do it:

Choose 5 distinct primes, {$2,3,5,7,11$} for instance. Then given your five numbers {$a_i$} just form the single integer: $$2^{a_1}3^{a_2}5^{a_3}7^{a_4}11^{a_5}$$