ef calculate_e (n): *** Here use the variable e to denote and return the value of e. I have initiated the value of e (e=0) and defined the point where yo The value of variable n denotes the number of terms to use in the se So write the loop to compute the first n terms. "** e = 0. X = 1 # your code here (hản(onu nuthon

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 20PE
icon
Related questions
Question

Need help on this question and can you explain how as well?

def calculate_e(n):
Here use the variable e to denote and return the value of e.
I have initiated the value of e (e=0) and defined the point where you will evaluate the series - that is, at x=1.
The value of variable n denotes the number of terms to use in the series.
So write the loop to compute the first n terms.
e = 0.
X - 1
# your code here
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import math
def calculate(n):
e = 0
x = 1
for i in range (n):
# here we do not raising the x to power
# because x is 1 and 1 to the power any
# number is 1 but in case of change the
# x value
# uncomment this line and comment the below line
# e += (x**i/math.factorial (i))
e +=(x/math.factorial(i))
return e
calculate(16)
#Ln[3]
calculate(22)
#Ln[4]
calculate(44)
#Ln[5]
calculate(66)
2.7182818284590455
Transcribed Image Text:def calculate_e(n): Here use the variable e to denote and return the value of e. I have initiated the value of e (e=0) and defined the point where you will evaluate the series - that is, at x=1. The value of variable n denotes the number of terms to use in the series. So write the loop to compute the first n terms. e = 0. X - 1 # your code here #!/usr/bin/env python # coding: utf-8 # In[2]: import math def calculate(n): e = 0 x = 1 for i in range (n): # here we do not raising the x to power # because x is 1 and 1 to the power any # number is 1 but in case of change the # x value # uncomment this line and comment the below line # e += (x**i/math.factorial (i)) e +=(x/math.factorial(i)) return e calculate(16) #Ln[3] calculate(22) #Ln[4] calculate(44) #Ln[5] calculate(66) 2.7182818284590455
Q4: complete the function that calculates the Euler's number e from the series you learnt during the maths lessons. The funcion takes in the value n, this will be the term that you truncate the series at. Also use the following hints.
Hint 1: remember the Maclaurin Expansion e* = 1+ x +
Hint 2: you will need to use a for loop,
Hint 3: e = e', when x=1 for e*
Hint 4: use the math.factorial(i) to caluclate factorial of i. THAT IS: i! = math.factorial(i)
Transcribed Image Text:Q4: complete the function that calculates the Euler's number e from the series you learnt during the maths lessons. The funcion takes in the value n, this will be the term that you truncate the series at. Also use the following hints. Hint 1: remember the Maclaurin Expansion e* = 1+ x + Hint 2: you will need to use a for loop, Hint 3: e = e', when x=1 for e* Hint 4: use the math.factorial(i) to caluclate factorial of i. THAT IS: i! = math.factorial(i)
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning