![]() |
VOOZH | about |
The value of Exponential Function e^x can be expressed using following Taylor Series.
e^x = 1 + x/1! + x^2/2! + x^3/3! + ......
How to efficiently calculate the sum of above series?
The series can be re-written as
e^x = 1 + (x/1) (1 + (x/2) (1 + (x/3) (........) ) )
Let the sum needs to be calculated for n terms, we can calculate sum using following loop.
for (i = n - 1, sum = 1; i > 0; --i ) sum = 1 + x * sum / i;
Following is implementation of the above idea.
Output:
e^x = 2.718282
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
This article is compiled by Rahul and reviewed by GeeksforGeeks team.