# [SPOILER] solving triple X systematically

Hello everybody,

So I just finished the section about triple X and it was good fun.
I just thought I’d give a go at coding a solver for it, which would find back the triplet of unknown from their product and sum.
It is not too difficult and depending on your level in math can also be interesting so I thought I would share

So we have our three variables x, y, and z and we know their sum S and product P. In other words:
x + y + z = S
x * y * z = P

We have two equations and three unknown so we know there will be a degree of freedom (for now).
Let’s isolate y (assuming it is not null):
x = S - y - z
(S - y) z - z^2 - P / y = 0

Let us leave x alone for now and focus on the second equation.
It is what we call a second degree equation with respect to z.
You can find some basics about them here:

We know that there is at least a positive solution (because we actually generated z in this way) which implies the following constraint (not equivalent though, see the wiki link):
(y - S)^2 - (4 P / y) >= 0
which is the same as
y^3 - 2 S y^2 + S^2 y - 4 P >= 0
Which is now a third degree equation in y (!!!)
here goes the wikipedia link for the curious:

The big idea is that it has either one or three solutions on the real axis which can be expressed depending on S and P (the full expressions are a bit heavy to be written here but you can find the, in the wikipedia).
You can hence compute them and look for one that is a positive integer (there might be several of them!)

You now have your value for y!
You replace it in the equation for z to obtain the value for z.
And you replace z and y by their values in the equation for x.

You are all set!

I hope some of you find it interesting! Do not hesitate to give your comments on it or ask more details.