is there any possibility of displaying all possible series of the form: "1+/-2^2+/-3^2+/-...+/-n^2", where one can choose the sign before the each term and knows the numbers of the terms in the series? (Summing up I would like to find out all possible combinations of the signs.) I appreciate your help.


1 Answer 1


For n terms there are $2^{n-1}$ arrangements of signs of terms after first term. If the aim (for small n) is to display some examples of all sign arrangements for n-1 terms after first term then here is a way. There are doubtless much better ways.

f[n_?Positive] := 
 With[{t = Tuples[{-1, 1}, n - 1], s = Tuples[{"-", "+"}, n - 1], 
   sq = HoldForm[#^2] & /@ Range[1, n]}, 
  Grid[{#1, Row[{Row@#2, " = ", #3}]} & @@@ 
    Thread[{Range[2^(n - 1)], 
      Riffle[sq, #] & /@ s, (({1}~Join~#).ReleaseHold[sq] & /@ t)}], 
   Frame -> All, BaseStyle -> {FontFamily -> "Kartika", Blue, 12}]]

for example:f[3]

enter image description here

or f[5]

enter image description here

If the sign of first term is also included then $2^n$ and code can be simplified.

  • $\begingroup$ You beat me to it, and with a solution that's more elegant than what I was working on, both from the code and from the presentation perspective, so I don't even feel bad about it ;-) (+1) $\endgroup$
    – MarcoB
    May 13, 2015 at 10:00
  • $\begingroup$ @MarcoB thank you for kind words and vote...variety of answers makes this site so enjoyable and instructive :) $\endgroup$
    – ubpdqn
    May 13, 2015 at 10:03

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.