Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Approximate e in an Infinite Series in C
    text
    copied!<p>So I am trying to do this problem:</p> <p><img src="https://i.stack.imgur.com/QLpU6.png" alt="Problem"></p> <p>However, I'm not entirely sure where to start or what exactly I am looking for. </p> <p>In addition, I was told I should expect to give the program inputs such as: zero (0), very small (0.00001), and not so small (0.1).</p> <p>I was given this: <a href="http://en.wikipedia.org/wiki/E_%28mathematical_constant%29" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/E_%28mathematical_constant%29</a> as a reference, but that formula doesn't look exactly like the one in the problem.</p> <p>And finally, I was told that the input to the program is a small number Epsilon. You may assume 0.00001f, for example.</p> <p>You keep adding the infinite series until the current term's value is below the Epsilon.</p> <p>But all in all, I have no clue what that means. I somewhat understand the equation on the wiki. However, I'm not sure where to start with the problem given. Looking at it, does anyone know what kind of formula I should be looking to use in C and what "E" is and where it comes into play here (i.e. within the formula, I understand it's suppose to be the user input).</p> <p><strong>Code So Far</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; //Program that takes in multiple dates and determines the earliest one int main(void) { float e = 0; float s = 0; float ct = 1; float ot= 1; int n = 0; float i = 0; float den = 0; int count = 0; printf("Enter a value for E: "); scanf("%f", &amp;e); printf("The value of e is: %f", e); for(n = 0; ct &gt; e; n++) { count++; printf("The value of the current term is: %f", ct); printf("In here %d\n", count); den = 0; for(i = n; i &gt; 0; i--) { den *= i; } //If the old term is one (meaning the very first term), then just set that to the current term if (ot= 1) { ct = ot - (1.0/den); } //If n is even, add the term as per the rules of the formula else if (n%2 == 0) { ct = ot + (1.0/den); ot = ct; } //Else if n is odd, subtract the term as per the rules of the formula else { ct = ot - (1.0/den); ot = ct; } //If the current term becomes less than epsilon (the user input), printout the value and break from the loop if (ct &lt; epsilon) { printf("%f is less than %f",ct ,e); break; } } return 0; } </code></pre> <p><strong>Current Output</strong></p> <pre><code>Enter a value for E: .00001 The value of e is: 0.000010 The value of the current term is: 1.000000 In here 1 -1.#INF00 is less than 0.000010 </code></pre> <p>So based on everyone's comments, and using the 4th "Derangements" equation from wikipedia like I was told, this is the code I've come up with. The logic in my head seems to be in line with what everyone has been saying. But the output is not at all what I am trying to achieve. Does anyone have any idea from looking at this code what I might be doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload