(Problem 6)Sum square difference

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <ctype.h>
 4 #include <math.h>
 5   
 6 #define N 100
 7   
 8 int powplus(int n, int k)
 9 {
10     int s=1;
11     while(k--)
12     {
13        s*=n;
14     }
15   return s;
16 }
17   
18 int sum1(int n)
19 {
20    return  powplus((n+1)*n/2,2);
21 } 
22   
23 int sum2(int n)
24 {
25    return (n*(n+1)*(2*n+1))/6;
26 }
27   
28 void solve()
29 {
30      printf("%d\n",sum1(N));
31      printf("%d\n",sum2(N));
32    printf("%d\n",sum1(N)-sum2(N));
33 }
34   
35 int main()
36 {
37   solve();
38   return 0;
39 }

 

Answer:
25164150

 

posted @ 2013-07-26 11:02  cpoint  阅读(597)  评论(0编辑  收藏  举报
浏览次数:travelocity promotion codes