Two methods are used to calculate the sum of all adjacent parity numbers within 1-, and the 1--100 parity

Source: Internet
Author: User

Two methods are used to calculate the sum of all adjacent parity numbers within 1-, and the 1--100 parity

Analysis:

Evaluate the sum of all adjacent parity numbers within 100

1 2 3 4 5 6 7 8 9 10 11... 100
That is:
1*2 + 3*4 + 5*6 +...
Method 1:
(2*1-1) 2*1 + (2*2-1) 2*2 + (2*3-1) 2*3: (2n-1) * 2n (loop 50 times)
Method 2:

1*2 + 3*4 +... + 99*100 conclusion: n * (n + 1) (2 steps per hop I + = 2)

# Include <stdio. h> # include <stdlib. h> // calculate the sum of the products of all adjacent parity numbers within 100 and void fun1 () {int res = 0; for (int I = 1; I <= 50; I ++) // loop 50 times {res + = (2 * i-1) * (2 * I ); // inference mathematical formula printf ("% d --> % d \ n", I, res) ;}/// it is recommended that void fun2 () {int res = 0; for (int I = 1; I <100; I ++ = 2) // cyclically 50 times, level 2, level 2 jumps, that is, the output is an odd number such as 1, 3, 5, 7, 9 {res + = I * (I + 1 ); // inference mathematical formula 1*2 3*4 5*6... printf ("% d --> % d \ n", I, res) ;}} int main () {fun1 (); fun2 (); return 0 ;} // If the for loop does not have EXPRESSIONS 1 and 2, it is equivalent to the while loop.


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.