resolve a system of linked equations with different modulo - algorithm

Is there any algorithm to solve a system of equations expressed in different modulo spaces?
For exemple, consider this system of equations:
(x1 + x2 ) % 2 = 0
( x2 + x3) % 2 = 0
(x1 + x2 + x3) % 3 = 2
One of the solutions of this system is:
x1 = 0
x2 = 2
x3 = 0
How could I arithmetically find this solution (without using a brute force algorithm)?
Thanks

You can rewrite these equations as
x1 + x2 = 2*n1
x2 + x3 = 2*n2
x1 + x2 + x3 = 3*n3 + 2
Now, this is a linear Diophantine equation problem for which there are solutions in the literature.
Example: http://www.wikihow.com/Solve-a-Linear-Diophantine-Equation
Also see: https://www.math.uwaterloo.ca/~wgilbert/Research/GilbertPathria.pdf
Algorithm:
Write xi as a function of nks
In this case:
x3 = 3*n3 + 2 - 2*n1
x2 = 2*n2 - (3*n3 + 2 - 2*n1)
x1 = 2*n1 - (2*n2 - (3*n3 + 2 - 2*n1))
Since there is no division on the right-hand side, pick any (n1, n2, n3) and you should get a solution.

First line is same as saying x1, x2 is all even or all odd numbers.
Second line is same as saying x2, x3 is all even or all odd numbers.
Hence x1,x2,x3 is all even or all odd numbers.
From third line we can replace the question to "3 odd or 3 even numbers that accumulate to 3k+2."

You can convert your system to modulo LCM (least common multiple). Just find the LCM of all equation's modulo, and multiply each equation appropriately.

Related

Gas mixture algorithm

I have a pool of ten different gas mixtures. Each gas mixture is made of nitrogen, oxygen and carbon dioxide in different percentages.
The goal is to mix and match sets of gasses to to get a requested percentage output.
Lets say we want 33-33-33 out. Whats the best methodology to select the best subset of gases and mixing proportions to get an optimal output?
Just solve 3 equations with 3 unknowns.
if mixture 1 has a1 fraction of chemical a (and similarly with other chemicals/mixtures), and you need xa : xb : xc mixture, where xa+xb+xc=1, this will be:
a1 * x1 + a2 * x2 + a3 * x3 = xa
b1 * x1 + b2 * x2 + b3 * x3 = xb
c1 * x1 + c2 * x2 + c3 * x3 = xc
Solve for x1, x2, x3. If you get negative numbers (or no solutions), this means that it's impossible to get the wanted mixture.

pow(X,Y,Z) <=> Z = X^Y with add

Would it be possible to do "pow" with "add" predicate (or just X is Y + Z )?
I make this:
pow(0,1,1).
pow(_,0,1).
pow(X,Y,Z) :- Y1 is Y - 1, pow(X,Y1,Z1), Z is Z1 * X.
But I want also make it with " + " (just for practise) like 3^2 = 3 * 3 = 3 + 3 + 3
You can write the multiplication (mul/3) in terms of addition. Like:
pow(0,1,1).
pow(_,0,1).
pow(X,Y,Z) :-
Y > 1,
Y1 is Y - 1,
pow(X,Y1,Z1),
mul(Z1,X,Z). %% originally: Z is Z1 * X.
mul(0,_,0).
mul(I,A,R) :-
I > 0,
I1 is I-1,
mul(I1,A,R1),
R is R1 + A.
Usually a basic exercise is to write addition, multiplication, and power predictates with the Peano number representation. In that case addition is written with the successor functor.

Merging two very large lists

Given a list of size 2n -1 elements and the list looks like this:
x1, x2, x3, ....., xn, y1, y2, y3, ....y(n-1)
Convert it to:
x1, y1, x2, y2, x3, y3, ........., y(n-1), xn
I can use two iterators for each of the lists and get the solution in O(n) time complexity and O(n) space complexity. But if my n was very large, is there a way to do this in lesser space complexity?
It feels like this can be done with O(1) space and O(n) time but the algorithm is far from trivial. Basically take an element that is out of place, say x2, look where it needs to be in the final arrangement take out the element that is there (i.e. x3) and put in x2.
Now look where x3 needs to go and so on.
When the cycle is closed, take the next element that is out of place (if there is any).
Lets do an example:
x1 x2 x3 y1 y2 x2 is out of place so take it into temp storage
x1 -- x3 y1 y2 temp: x2 needs to go where x3 currently is
x1 -- x2 y1 y2 temp: x3 needs to go where y2 currently is
x1 -- x2 y1 x3 temp: y2 needs to go where y1 currently is
x1 -- x2 y2 x3 temp: y1 needs to go into the empty slot
x1 y1 x2 y2 x3 all elements in place -> finished
If the array indices start at 0, the final position of the element at k is given by
2k if k < n
2(k-n) + 1 if k >= n
The difficulty is to find out an element of a cycle that is not yet handled. For example if n = 4 there are 3 cycles:
0 -> 0
1 -> 2 -> 4 -> 1
3 -> 6 -> 5 -> 3
I do not have an easy solution for that at the moment.
If you have one bit of storage available per array element it is trivial but then we are back to O(n) storage.
In Python:
lst = 'x1 x2 x3 x4 x5 y1 y2 y3 y4 y5'.split()
lst
Out[9]: ['x1', 'x2', 'x3', 'x4', 'x5', 'y1', 'y2', 'y3', 'y4', 'y5']
out = sum((list(xy) for xy in zip(lst[:len(lst)//2], lst[len(lst)//2:])), [])
out
Out[11]: ['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4', 'x5', 'y5']

How to solve this equation for solving "Finding duplicate in integer array"

I was looking at the problem and the discussion here: Easy interview question got harder: given numbers 1..100, find the missing number(s)
One of the user provided a solution using following equation.
k1 + k2 = x
k1^2 + k2^2 = y
Substituting provides (x-k2)^2 + k2^2 = y
I am trying to solve this equation further and come up with a C program to solve the problem of finding duplicates.
Inspite of spending lot of time I couldn't solve this equation to get k1 or k2 one side. I always ended up with k1 or k2 on both side of equation.
Any help is appreciated.
Expand the equation
(x - k2)^2 + k2^2 = y
and get
x^2 - 2xk2 + 2k2^2 = y
or
2k2^2 - 2xk2 + x^2 - y = 0
Now use the formula for solving the quadratic equation az^2 + bz + c = 0 which is (-b +/- sqrt(b^2 - 4ac))/2a. Only that in our case z=k2. So
k2 = (2x +/- sqrt(4x^2 - 8(x^2 - y))) / 4
or
k2 = (x +/- sqrt(x^2 - 2(x^2 - y))) / 2
= (x +/- sqrt(2y - x^2)) / 2
and you can put
k2 = (x + sqrt(2y - x^2)) / 2
k1 = (x - sqrt(2y - x^2)) / 2.

Using min/max *within* an Integer Linear Program

I'm trying to set up a linear program in which the objective function adds extra weight to the max out of the decision variables multiplied by their respective coefficients.
With this in mind, is there a way to use min or max operators within the objective function of a linear program?
Example:
Minimize
(c1 * x1) + (c2 * x2) + (c3 * x3) + (c4 * max(c1*x1, c2*x2, c3*x3))
subject to
#some arbitrary integer constraints:
x1 >= ...
x1 + 2*x2 <= ...
x3 >= ...
x1 + x3 == ...
Note that (c4 * max(c1*x1, c2*x2, c3*x3)) is the "extra weight" term that I'm concerned about. We let c4 denote the "extra weight" coefficient. Also, note that x1, x2, and x3 are integers in this particular example.
I think the above might be outside the scope of what linear programming offers. However, perhaps there's a way to hack/reformat this into a valid linear program?
If this problem is completely out of the scope of linear programming, perhaps someone can recommend an optimization paradigm that is more suitable to this type of problem? (Anything that allows me to avoid manually enumerating and checking all possible solutions would be helpful.)
Add in an auxiliary variable, say x4, with constraints:
x4 >= c1*x1
x4 >= c2*x2
x4 >= c3*x3
Objective += c4*x4

Resources