I need to maximize a function that involves an alternating sum and a set of constraints. I have tried the following code:

NMaximize[{(-1)^{m}*n!, n + m == 7, m > 0, n > 0}, {m, n}]

However, the error message

NMaximize: The objective function {(-1)^m n!} should be scalar-valued


Are there any numerical maximization methods that can be used for functions like this?


1 Answer 1


(1) Change (-1)^{m} to (-1)^(m) and (2) add the constraint that m and n are integers:

NMaximize[{(-1)^(m)*n!, n + m == 7, m > 0, n > 0, Element[{m, n}, Integers]}, {m, n}]

{6., {m -> 4, n -> 3}}

  • $\begingroup$ Thank you! I am glad it was such an easy fix. $\endgroup$
    – smallscot
    Commented Aug 20, 2019 at 13:33
  • $\begingroup$ @smallscot, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented Aug 20, 2019 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.