Michael Watts - 1 year ago 104

Python Question

I have 2 questions I need to ask for help with. One question I don't entirely understand so if someone could help me to that would be great.

Question One (the one I don't entirely understand):

One definition of e is

Formula for e

This can be calculated as my_e = 1/math.factorial(0) + 1/math.factorial(1) + 1/math.factorial(2) + 1/math.factorial(3) + …

Let n be the input number of the math.factorial() function. n successively takes on 0, 1, 2, 3 and so on. Find the smallest n such that the absolute value of (my_e – math.e) is less than or equal to 10-10. That is, abs(my_e - math.e) <= (10 ** -10).

I just don't entirely understand what I am being asked to do. Clarification would be great. Thanks!

Question 2:

Ask the user to type in a series of integers. Sum them up and print out the sum and the number of integers the user has entered.

My code

So what should happen is after I enter the numbers I want to enter and hit the enter key, it should calculate and print out "sum = 25 count = 3". The screenshot shows what error message I am getting.

Any help you have is welcomed and greatly appreciated.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

As far as you first question goes:

```
>>> import math
>>> math.e
2.718281828459045
>>> sum(1.0/math.factorial(i) for i in range(5))
2.708333333333333
>>> abs(sum(1.0/math.factorial(i) for i in range(5)) - math.e) < 10**-10
False
>>> abs(sum(1.0/math.factorial(i) for i in range(30)) - math.e) < 10**-10
True
```

So, somewhere between `n == 5`

and `n == 30`

you get 10 decimal places for `e`

. Create the sum term by term in a while-loop (instead of by using the sum function as I have, since you are unlikely to have seen that syntax yet). At each pass through the loop, compare the sum with `math.e`

. Stop when you get the target accuracy. Return the final `n`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**