在 C 中使用 while 循环计算 e 常量



我最近开始编程,当我做一些练习时,我遇到了一个说:

编写一个程序,该程序可以使用公式计算 e 常数的近似值,e=1+1/1!+1/2!+1/3!+...必要时使用 while。你不能使用做...而

写了我的代码,我几乎可以发誓程序需要两个while循环,但是,正如你可能猜到的那样,它不能正常工作。这是我的代码:

#include<stdio.h>
int main()
{
  float number=3, factorial=1, constant=0, counter=3, variable=0;
  float euler=0;
  while(counter>1)
  {
    variable = number;
    while(number>1)
    {
      factorial = factorial*number;
      number--;
    }                      // number is now 0, or that's what I think
    constant = (1/factorial)+constant;
    counter--;
    variable = variable-1; // variable is still number?
    number = variable;     // to have a variable called number again?
  }
  euler = constant+1;      // the 1 in the original formula...
  printf("e = : %fn", euler);
  return 0;
}

它没有显示正确答案,希望你能帮助我。多谢!

  • 您的迭代次数太少。迭代更多以获得更准确的值。
  • 您必须初始化每个循环中的factorial才能以这种方式计算阶乘。
  • 您忘了添加1/1!.

试试这个:

#include<stdio.h>
int main(void)
{
  float number=30, factorial=1, constant=0, counter=30, variable=0;
  float euler=0;
  while(counter>1)
   {
      variable=number;
      factorial = 1;
      while(number>1)
       {
         factorial=factorial*number;
         number--;
       }//number is now 1
      constant=(1/factorial)+constant;
      counter--;
      variable=variable-1;
      number=variable;
   }
  euler=constant+1+(1/1.f);//the 1 and 1/1! in the original formula...
  printf("e = : %fn", euler);
  return 0;
}

正如@MikeCAT所指出的,各种编码错误。

由于 OP 的迭代次数很少:3 次,导致精度低。由于所有术语最终都添加到 1.0(被 OP 遗漏),一旦一个术语加 1.0 仍然是 1.0,是时候停止搜索较小的术语了。 通常大约 18 次迭代,典型double

在计算序列的总和时,通过首先对最小的项求和,在本例中,是OP所做的最后一项,可以获得稍微准确的答案。 这可以使用递归求和来完成,以避免大量的阶乘重新计算。

double e_helper(unsigned n, double term) {
  double next_term = term/n;
  if (next_term + 1.0 == 1.0) return next_term;
  return next_term + e_helper(n+1, next_term);
}
double e(void) {
  return 1.0 + e_helper(1, 1.0);
}
#include <stdio.h>
#include <float.h>
#include <math.h>
int main(void) {
  printf("%.*fn", DBL_DECIMAL_DIG - 1, e());
  printf("%.*fn", DBL_DECIMAL_DIG - 1, exp(1));
  puts("2.71828182845904523536028747135266249775724709369995...");
}

输出

2.7182818284590451
2.7182818284590451
2.71828182845904523536028747135266249775724709369995...
#include <stdio.h>
#include <math.h>
int main()
{
        printf("e = : %.20lfn", M_E );
        return 0;
}

C 数学库具有常数M_E为欧拉数。但是,这可能不是您想要的。

求方程为"e"的方程 e =

1 + 1/1! + 1/2! ....

仅使用 while 循环

对于初学者

    #include <stdio.h>
    int main (void) {
    float n =5   , fact = 1 , f , x = 0  , e ,i  ; //taking input or   n as 5  ,our formula now is  e = 1+ 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ; as the formula depends upon the input value of n    
  
      while(n >=1  ) {   /* We need to find factorial of n no of values, so keeping n in a loop starting with 5....1 , atm  n is 5 */
                           f = n ;         // let f = n , i.e, f = 5 
                          fact = 1 ;
                      while(f >= 1 ){      //This while loops finds the factorial of current n value , ie. 5 ;  
                           fact = fact * f  ;
                            f -- ;
                 }
                i = 1 / fact ;       // i finds the 1/fact! of the formula 
                x = i + x ;                // x = i + x ; where x = 0 ; , This eq adds all the 1/fact values , atm its adding 1/ 5 ! 
                n-- ;              // n decrements to 4 , and the loop repeats till n = 1 , and finally x has all the 1/factorial part of the eulers formula 
 }
   //exiting all the loops since, we now have the values of all the 1/factorial,i.e x :  part of the eulers formula 
              e = 1 + x  ;   //   eulers e = 1 + x , where x represents all the addition of  1/factorial part of the eulers formula 
          printf ("e : %f",e ); //Finally printing e 
return 0 ;

}

输出:

e : 2.716667

最新更新