p3 Mathematica solution