Where does this formula in problem 486A come from?

Revision en1, by hot3eed, 2016-12-29 17:10:42

The formula I'm asking about in problem 486A is the one in most C++ answers which is: n/2-n*(n%2), now where does this formula come from? Also, I found this solution in which the author says that he looked for a pattern (tldr; if the number is even then n/2 or else if it's odd then -1 * ((n+1)/2)) until he found one which actually works, is there any explanation for this pattern? Also, is this kind of patterns a matter of trial and error or is there a way to identify them?

Tags math, implementation, algorithm

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English hot3eed 2016-12-29 17:10:42 715 Initial revision (published)