Ternary equations solution (Klimt algorithm)