zoukankan      html  css  js  c++  java
  • (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

     

  • 相关阅读:
    kill一个pthread_test.bin测试程序主线程、子线程退出kernel flow
    signal bit operation
    pthread
    信号发送处理流程
    sdcardfs
    node小贴士03
    node小贴士02
    node小贴士01
    siteserver cms 搜索功能
    语法的高亮显示
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367349.html
Copyright © 2011-2022 走看看