Calculate the sum of arithmetic progression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.