Mathematica solution