- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find ex in an efficient way in Python
Suppose we have a number n. We have to find $e^{x}$ efficiently, without using library functions. The formula for $e^{x}$ is like
$$e^{x} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + ...$$
So, if the input is like x = 5, then the output will be 148.4131 because e^x = 1 + 5 + (5^2/2!) + (5^3/3!) + ... = 148.4131...
To solve this, we will follow these steps −
- fact := 1
- res := 1
- n := 20 it can be large for precise results
- nume := x
- for i in range 1 to n, do
- res := res + nume/fact
- nume := nume * x
- fact := fact *(i+1)
- return res
Example
Let us see the following implementation to get better understanding −
def solve(x): fact = 1 res = 1 n = 20 nume = x for i in range(1,n): res += nume/fact nume = nume * x fact = fact * (i+1) return res x = 5 print(solve(x))
Input
5
Output
143
Advertisements