Reputation: 31
I recently started programming and I was doing some exercises when I bumped into one that said:
Write a program that can calculate an approximate value of the e constant with the formula
e=1+1/1!+1/2!+1/3!+...
using while and if if necessary. You cannot use do...while or for.
I wrote my code and I could almost swear the program needs two while loops, but, as you may be guessing, it doesn't work properly. Here's my code:
#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 = : %f\n", euler);
return 0;
}
It doesn't display the correct answer, hope you can help me. Thanks a lot!
Upvotes: 0
Views: 2800
Reputation: 1
Finding eulers constant 'e' with the formula e = 1 + 1/1! + 1 /2! ....
only using while loop
For beginners
#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 ;
}
Output :
e : 2.716667
Upvotes: 0
Reputation: 1
#include <stdio.h>
#include <math.h>
int main()
{
printf("e = : %.20lf\n", M_E );
return 0;
}
C math library have constant M_E as Euler's number. But, this may not be what you want.
Upvotes: 0
Reputation: 154174
As pointed by @MikeCAT, various coding errors.
As OP's iteration count was low: 3 resulting in low accuracy. As all the terms are eventually added to 1.0 (missed by OP), once a term plus 1.0 is still 1.0, it is about time to quit searching for smaller terms. Typically about 18 iterations with typical double
.
When computing the sum of a series, a slightly more accurate answer is available by summing the smallest terms first, in this case, the last terms as done by OP. This can be done using a recursive summation to avoid lots of factorial recalculation.
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...");
}
Output
2.7182818284590451
2.7182818284590451
2.71828182845904523536028747135266249775724709369995...
Upvotes: 1
Reputation: 75062
factorial
in each loop to calculate factorial in this way.1/1!
.Try this:
#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;
}
Upvotes: 2