问题描述
就像标题说,我是如何计算的形式正数的总和:(!1/2)1+ +⋯(1 / N!)?我已经得到了code为调和级数:
Like the title say, how I calculate the sum of n number of the form: 1+(1/2!)+⋯(1/n!)? I already got the code for the harmonic series:
#include <stdio.h>
int main( void )
{
int v=0,i,ch;
double x=0.;
printf("Introduce un número paracalcular la suma: ");
while(scanf("%d",&v)==0 || v<=0)
{
printf("Favor de introducir numeros reales positivos: ");
while((ch=getchar())!='\n')
if(ch==EOF)
return 1;
}
for (i=v; i>=1; i--)
x+=1./i;
printf("EL valor de la serie es %f\n", x);
getch();
return 0;
}
这里的问题是..我已经得到的总和的比例,但如何使变量i阶乘?
The question here is.. I already got the sum as the fraction, but how make the variable "i" factorial?
注:I'm编程语言C,与DEV - C ++ 4.9.9.2
Note: I´m programming in language C, with DEV -C++ 4.9.9.2
推荐答案
您得到的谐波求和1 / I + 1 ./(I-1)... 1/1稍微更准确的答案。建议你留在这个顺序。
You got a slightly more accurate answer for the harmonic summing 1./i + 1./(i-1) ... 1./1. Suggest you stay with that order.
改写:感谢@ pablo197您指出我的方式错误
[edit] Rewrite: Thanks to @pablo197 for pointing out the error of my ways.
要计算谐波的和的1+(1/2!)+ ... +(1 / N!),继续总结了至少显著术语以及第一因为这有助于最大限度地减少precision亏损。用最少的显著期限开始 1 / N
为之
,的那笔和n-1项是:和=(1 +总和)/(N-1)
等。 (见下文)
To calculate harmonic and 1+(1/2!)+…+(1/n!), continue summing the least significant terms together first as that helps to minimize precision loss. Starting with the least significant term 1/n
as sum
, sum of that and the n-1 term is : sum = (1 + sum)/(n-1)
and so on. (See below)
double x = 0.0;
double one_over_factorial_series = 0.0;
for (i = v; i >= 1; i--) {
x += 1.0/i;
one_over_factorial_series = (one_over_factorial_series + 1)/i;
}
printf("harmonic:%le\n", x);
// 2.828968e+00
printf("one_over_factorial:%.10le\n", one_over_factorial_series);
// 1.7182815256e+00
添加 1.0
或 1/0!
到 one_over_factorial_series
,结果约 E = 2.7182818284 ...
详细说明如何直接N!避免了计算。
Detail showing how direct n! calculation is avoided.
1 + (1/2!) + … + (1/n!) =
1/n! + 1/((n-1)!) + 1/((n-2)!) + 1/((n-3)!) + ... + 1 =
(1/n + 1)/((n-1)!) + 1/((n-2)!) + 1/((n-3)!) + ... + 1 =
((1/n + 1)/(n-1) + 1)/((n-2)!) + 1/((n-3)!) + ... + 1 =
...
((((1/n + 1)/(n-1) + 1)/(n-2) + 1)/(n-3) + 1)/(n-4) + ... =
这篇关于计算的总和1+(1/2!)+ ...(1 / N!)n个C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!