Many algorithms require to compute (-1)^n
(both integer), usually as a factor in a series. That is, a factor that is -1
for odd n and 1
fo
Usually you don't actually calculate (-1)^n
, instead you track the current sign (as a number being either -1
or 1
) and flip it every operation (sign = -sign
), do this as you handle your n
in order and you will get the same result.
EDIT: Note that part of the reason I recommend this is because there is rarely actually semantic value is the representation (-1)^n
it is merely a convenient method of flipping the sign between iterations.