As you recognize yourself, if you simply form the sum of three integers, you cannot then recover the original values from only that sum. Sufficient information is lost to recover the original components. Thus, given
2 + 1 + 1 == 1 + 2 + 1 == 1 + 1 + 2 == 4
if the sum was 4, then it is never possible to identify which of the three options caused that sum. Sorry, but that is simple fact. You cannot escape the laws of mathematics.
However, you CAN do something that allows recovery, if you use a proper encoding. Think of it as a base transformation, working in base 255. Given three integers, each in the range 1 - 255. I'll call them K, N, & M. Form an encoding as:
KNM = 255^2*(K-1) + 255*(N-1) + (M-1)
Thus for any values of K, N, M, we will get a distinct result, treating those numbers as effectively "digits" in base 255, then mapping the result to base 10. Try it out.
KNMencode = @(K,N,M) 255^2*(K-1) + 255*(N-1) + (M-1);
KNMencode(2,1,1)
ans =
65025
KNMencode(1,2,1)
ans =
255
KNMencode(1,1,2)
ans =
1
So we now get a distinct and unique result for any input combination in that range.
The reverse is true too. Now you can decode any possible integer value in the interval [0,16451834], back into the original set that created the number.
KNMencode(1,1,1)
ans =
0
KNMencode(255,255,255)
ans =
16451834
Again, this is just a case of a base conversion from base 10 back into base 255. Lets try it out.
KNMdecode = @(E) [mod(floor(E/255^2),255) + 1,mod(floor(E/255),255) + 1,mod(E,255) + 1];
KNMencode(1,2,3)
ans =
257
KNMdecode(257)
ans =
1 2 3
So any triple of integers (K,N,M) will result in a unique encoded value as needed, that can then be decoded, recovering the original values.
Best Answer