
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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?
- What is the most efficient way to concatenate many Python strings together?
- Efficient way to remove all entries from MongoDB?
- Program to find minimum costs needed to fill fruits in optimized way in Python
- An efficient way to check whether n-th Fibonacci number is multiple of 10?
- 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
- Program to traverse binary tree level wise in alternating way in Python
- Python Program for Efficient program to print all prime factors of a given number
- What is the most efficient way to select a specific number of random rows in MySQL?

Advertisements