Project_Euler-06 题解
题目描述
两个公式
等差数列求和公式
i i i项: a i a_{i} ai
项数: n n n
公差: d d d
和: S n S_{n} Sn
a n = a 1 + ( n − 1 ) d S n = n ( a 1 + a n ) 2 a_{n} = a_{1} + (n - 1)d\\ S_{n} = \frac{n(a_{1} + a_{n})}{2} an=a1+(n−1)dSn=2n(a1+an)
平方和公式
a n s = n ( n + 1 ) ( 2 n + 1 ) 6 ans = \frac{n(n + 1)(2n + 1)}{6} ans=6n(n+1)(2n+1)
代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
#define n 100
int main(){int sum1 = 0, sum2 = 0, sum;sum1 = n * (n + 1) * (2 * n + 1) / 6;sum2 = pow(((1 + n) * n / 2), 2);sum = sum2 - sum1;printf("%d\n", sum);return 0;
}