Title: Seeking 1+2+...+n,
Requirements: You cannot use multiplication, for, while, if, else, switch, case, and conditional judgment statements (A?). B:C).
Let's take a look at the algorithmic process of solving problems:
Explain it!
People who have studied computer technology know that the book on the front of the position there will always be such a word, 2 binary multiplication is the dislocation added. That's the principle. int has 32 bit bits, I will add this dislocation to write 32 times. is actually multiplication. This algorithm is faster than recursion. Recursive o (n), this O (1), code more? The user can not see it again.
Do you have any other ideas?
can be used multiplication more simple, you can use the arithmetic progression Formula summation formula Sn=n (A1+an)/2 or Sn=na1+n (n-1) D/2 (the process please check the relevant information yourself)
The first n and the formula of the arithmetic progression (n+1)/2, since the binary multiplication is addition, the N (n+1)/2 to the binary calculation is not more simple, the above ideas to keep everyone thinking, or I will try to achieve in the future.
Source: QQ algorithm Group chat record
Calculate the sum of arithmetic progression