1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Dhaka, Bangladesh
    Rep Power

    Using a loop to find the value of 'e'

    The value of e can be found by this infinite series:
    "1 + 1/1! + 1/2! +... +1/n!"
    n will be provided as an input. The program will do the rest of the job. Any idea how to set up the algorithm?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    I'd take the input as a command line argument.

    int main(int ac,char*av[])

    I'd verify the input.
      if ((ac < 2) || (sscanf(av[1], "%d", &n) != 1))
        use(*av, "number_of_terms");
    Although the input isn't really all that important. So you might just as well bind whatever the values are to a reasonable range, say, 1 to 24 terms. The reciprocal of factorial 23 (in j:
    ) won't contribute to a double precision value. Next I'd compute the reciprocals of the factorials into an array, successively. You know the maximum array size because you limited n. These I'd add from smallest to largest, and finally print the result to 18 figures. All-in-all, n-1 divisions and additions.

    I checked to see if the addition order influences the least significant digit. It does, with the result that adding in my suggested order is closer to Euler's constant.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo