I have the following code:
n /. Solve({c == 1/2 (-2 + 3 n^2) (-1 + 3 n^2) (1 - 3 n + 3 n^3),
n (Element) PositiveIntegers}, n)((1))
This procudes the number $n$ such that the equation is solved in the natural numbers because $cinmathbb{N}$. However when there is no solution found the code gives an error message:
Part::partw: Part 1 of {} does not exist.
ReplaceAll::reps: {{}((1))} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
How can I write code that gives $n$ when it exists and it gives $0$ if no solution exists? So for example when $c=1045$ we see that $n=2inmathbb{N}$ so the code must give 2
as the output but when $c=1046$ there is no $ninmathbb{N}$ that solves the equation so the code must give 0
.