题目:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
等差数列:
一个等差数列的和等于其首项与末项的和乘以项数除以2。
公式
一个公差为d的等差数列前n项的级数为:
等差级数在中文教科书中常表达为:
-
- 一个等差数列的和等于其首项与末项的和乘以项数除以2。
证明
将一个等差级数写作以下两种形式:
将两公式相加来消掉公差d:
整理公式,并且注意 ,我们有:
int main(int argc, char* argv[]) { int max=1000-1;//不包含1000 //3的倍数,5的倍数,减去两者15倍数重复值 int i=sum(3,3,max)+sum(5,5,max)-sum(15,15,max); printf("%d\n",i); system("PAUSE"); return 0; } int sum(int a1,int d,int max){ int n=max/a1; int s=(2*a1+(n-1)*d)*n/2; return s; }