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

时间:2016-06-09 00:24:33

标签: c while-loop

我最近开始编程,当我碰到一个说:

时,我正在做一些练习
  

编写一个程序,可以使用计算 e 常量的近似值,使用,而,如果,必要。您无法使用执行...

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

<script type="text/javascript">
//to get every mail by line in textarea
var textarea = tt.replace("\r\n", "\n");
var textarea = textarea.replace("\r", "\n");
var textarea = textarea.split("\n");


var indice=0;
for(var i=0;i<count(textarea);i++){
var line = textarea[i];
var indice= indice+100/count(textarea);

$('#waiting').load('sent.php',{ln:line,indc:indice}, function(){
var finale = $('#waiting').text(); //return the current value of indice
$('#progressbar').css({ "width" : finale+"%"});
setTimeout('#waiting', 1000);
});
}
</script>

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

4 个答案:

答案 0 :(得分:2)

  • 你的迭代太少了。迭代更多以获得更准确的价值。
  • 您必须在每个循环中初始化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 = : %f\n", euler);
  return 0;
}

答案 1 :(得分:1)

正如@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("%.*f\n", DBL_DECIMAL_DIG - 1, e());
  printf("%.*f\n", DBL_DECIMAL_DIG - 1, exp(1));
  puts("2.71828182845904523536028747135266249775724709369995...");
}

输出

2.7182818284590451
2.7182818284590451
2.71828182845904523536028747135266249775724709369995...

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

用公式 e = 1 + 1/1 求欧拉常数 'e'! + 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