- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- Related Questions & Answers
- Program to find out the efficient way to study in Python
- Program to find nCr values for r in range 0 to n, in an efficient way in Python
- What is an efficient way to repeat a string to a certain length in Python?
- What is the most efficient way to deep clone an object in JavaScript?
- Efficient way to remove all entries from MongoDB?
- An efficient way to check whether n-th Fibonacci number is multiple of 10?
- Program to find minimum costs needed to fill fruits in optimized way in Python
- How to become an efficient listener?
- Is there a way to find an element by attributes in Python Selenium?
- Write an Efficient C Program to Reverse Bits of a Number in C++
- In MongoDB, what is the most efficient way to get the first and last document?
- Python Program to find largest element in an array
- What's the most efficient way to turn all the keys of an object to lower case - JavaScript?
- What is the most efficient way to select a specific number of random rows in MySQL?
- Python Program for Efficient program to print all prime factors of a given number

Advertisements