Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That summation symbol gives you a clue: you need a loop.</p> <p>What's <code>0!</code>? 1, of course. So your starting value for e is 1. </p> <p>Next you'll write a loop for n from 1 to some larger value (infinity might suggest a while loop) where you calculate each successive term, see if its size exceeds your epsilon, and add it to the sum for e.</p> <p>When your terms get smaller than your epsilon, stop the loop.</p> <p>Don't worry about user input for now. Get your function working. Hard code an epsilon and see what happens when you change it. Leave the input for the last bit.</p> <p>You'll need a good factorial function. (Not true - thanks to Mat for reminding me.)</p> <p>Did you ask where the constant e comes from? And the series? The series is the Taylor series expansion for the exponential function. See any intro calculus text. And the constant e is simple the exponential function with exponent 1.</p> <p>I've got a nice Java version working here, but I'm going to refrain from posting it. It looks just like the C function will, so I don't want to give it away.</p> <p>UPDATE: Since you've shown yours, I'll show you mine:</p> <pre><code>package cruft; /** * MathConstant uses infinite series to calculate constants (e.g. Euler) * @author Michael * @link * @since 10/7/12 12:24 PM */ public class MathConstant { public static void main(String[] args) { double epsilon = 1.0e-25; System.out.println(String.format("e = %40.35f", e(epsilon))); } // value should be 2.71828182845904523536028747135266249775724709369995 // e = 2.718281828459045 public static double e(double epsilon) { double euler = 1.0; double term = 1.0; int n = 1; while (term &gt; epsilon) { term /= n++; euler += term; } return euler; } } </code></pre> <p>But if you ever need a factorial function I'd recommend a table, memoization, and the gamma function over the naive student implementation. Google for those if you don't know what those are. Good luck.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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