What equation to use when finding 6 variables?

276 Views Asked by At

I'm programming an application which will need to calculate and invert 6 variables.

e.g.

[4][20][15][16][0][1] -> (perform calculation) -> 450

450 -> (perform calculation) -> [4][20][15][16][0][1]

What would be the best direction to go to calculate and invert the calculation for these variables or nth variables if I wish to expand later on? I was looking at Matrices, linear equations, etc, but not sure the best and easiest method for programmatically making this a bidirectional equation. Edit: I’m looking to get the smallest number possible, and the range is 0 to 55 for my variables.

2

There are 2 best solutions below

0
On BEST ANSWER

With 6 variables $x_0, \ldots, x_5$ all in the range $0 - 55$ you could form $$n = x_0 + 56 x_1 + 56^2 x_2 + \cdots + 56^5 x_5.$$ To revert to the variables you can do (pseudocode):

x = [0, 0, 0, 0, 0, 0]
i = 0
while n > 0 {
  x[i] = n % 56
  n := floor(n / 56)     # integer division
}
2
On

Imagine that the variables you have had the range $0$-$9$, i.e. single digits. In that case you could obviously append them to form a single 6-digit number.

To do that in a computer program, appending a digit $d$ to a number $t$ is done by calculating $10t+d$. The multiplication by $10$ shifts it to the left by appending a zero, and then adding $d$ sets the last digit to $d$. So you can simply do this six times in a loop, appending the six variables to zero.

You'll also want to extract the digits one by one. You can get the last digit of a number $t$ by evaluating $t \bmod 10$, which is $t\%10$ in some computer languages. Once you have that digit $d$, you can calculate $\frac{t-d}{10}$, removing the digit and shifting it to the right, so that you can then access the next digit.

In your case you have variables in the range $0$ to $55$ instead of $0$ to $9$. Therefore you should use $56$ at every spot I used $10$ in the above explanation. It may be easier to implement it base 10 first, so that it is easy to see that it works the way you want to before changing it to base 56.