# 求和公式推译与C语言测试

5.1C/C++ 专栏收录该内容
15 篇文章 1 订阅

http://blog.csdn.net/chinayaosir/article/details/6781803

/*********************************************************/
function:THE SUMMATION FORMULA WITH ONE TO N
file        :summationchecking.TXT
author  :chinayaosir   QQ:44633197
Tools   :MATHS TECHNOLOGY ANALYSIS
date     :9/06/2011
blog     :http://blog.csdn.net/chinayaosir
note     :禁止其它网站转载此文章
/*********************************************************/

sum(1..n)=n*(n/2)+n/2

-----------------------------------------------------------------------------

sum(1..n)=?
sum(1..n)=n*(n/2)+n/2

sum(1..4) =4+3+2+1
=4+(3+1)+2 //加法交换律
=4+(3+1)+4/2 //整数换成分数2变成4/2
=4+4*1+4/2 //乘法4=4*1
=4*(2)+4/2 //乘法结合律4+4*1=4*(2)
=4*(4/2)+4/2 //整数换成分数2变成4/2

sum(1..8) =8+7+6+5+4+3+2+1
=8+(7+1)+(6+2)+(5+3)+4 //加法交换律
=8+8*3+4  //加法变乘法
=8*(4)+4  //乘法结合律8+8*3=8*4

=8*(8/2)+8/2  //整数换成分数4变成8/2

------------------------------------------------------------------------------
//sumcheck.c
#include <stdio.h>
#include <math.h>

//高斯求和
long int sum_one_to_n_gs(long int n){
return (long int)(1+n)*n/ 2;
}
//姚氏求和
long int sum_one_to_n_yao(long int n){
return (long int)(n* ((double)n/2)+(double)n/2);
}

int main(void){
printf("sum_one_to_n_gs(1)= %d\n",sum_one_to_n_gs(1));
printf("sum_one_to_n_yao(1)= %d\n",sum_one_to_n_yao(1));

printf("sum_one_to_n_gs(2)= %d\n",sum_one_to_n_gs(2));
printf("sum_one_to_n_yao(2)= %d\n",sum_one_to_n_yao(2));

printf("sum_one_to_n_gs(3)= %d\n",sum_one_to_n_gs(3));
printf("sum_one_to_n_yao(3)= %d\n",sum_one_to_n_yao(3));

printf("sum_one_to_n_gs(10)= %d\n",sum_one_to_n_gs(10));
printf("sum_one_to_n_yao(10))= %d\n",sum_one_to_n_yao(10));

printf("sum_one_to_n_gs(36)= %d\n",sum_one_to_n_gs(36));
printf("sum_one_to_n_yao(36))= %d\n",sum_one_to_n_yao(36));

printf("sum_one_to_n_gs(100)= %d\n",sum_one_to_n_gs(100));
printf("sum_one_to_n_yao(100))= %d\n",sum_one_to_n_yao(100));
return 0;
}
-----------------------------------

sum_one_to_n_gs(1)= 1
sum_one_to_n_yao(1)= 1
sum_one_to_n_gs(2)= 3
sum_one_to_n_yao(2)= 3
sum_one_to_n_gs(3)= 6
sum_one_to_n_yao(3)= 6
sum_one_to_n_gs(10)= 55
sum_one_to_n_yao(10))= 55
sum_one_to_n_gs(36)= 666
sum_one_to_n_yao(36))= 666
sum_one_to_n_gs(100)= 5050
sum_one_to_n_yao(100))= 5050
Press any key to continue

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-28

05-21 616
09-07 3065
05-25 683
12-12 1万+
01-28 933
04-09 780
05-06 3002
10-17 319
02-12 175
12-02 728
12-08 1117
11-30 549
05-07
04-09