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.