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.
C++
// C++ Efficient program to calculate // e raise to the power x #include <bits/stdc++.h> using namespace std; // Returns approximate value of e^x // using sum of first n terms of Taylor Series float exponential(int n, float x) { float sum = 1.0f; // initialize sum of series for (int i = n - 1; i > 0; --i ) sum = 1 + x * sum / i; return sum; } // Driver code int main() { int n = 10; float x = 1.0f; cout << "e^x = " << fixed << setprecision(5) << exponential(n, x); return 0; } // This code is contributed by rathbhupendra |
chevron_right
filter_none
C
// C Efficient program to calculate // e raise to the power x #include <stdio.h> // Returns approximate value of e^x // using sum of first n terms of Taylor Series float exponential(int n, float x) { float sum = 1.0f; // initialize sum of series for (int i = n - 1; i > 0; --i ) sum = 1 + x * sum / i; return sum; } // Driver program to test above function int main() { int n = 10; float x = 1.0f; printf("e^x = %f", exponential(n, x)); return 0; } |
chevron_right
filter_none
Java
// Java efficient program to calculate // e raise to the power x import java.io.*; class GFG { // Function returns approximate value of e^x // using sum of first n terms of Taylor Series static float exponential(int n, float x) { // initialize sum of series float sum = 1; for (int i = n - 1; i > 0; --i ) sum = 1 + x * sum / i; return sum; } // driver program public static void main (String[] args) { int n = 10; float x = 1; System.out.println("e^x = "+exponential(n,x)); } } // Contributed by Pramod Kumar |
chevron_right
filter_none
Python3
# Python program to calculate # e raise to the power x # Function to calculate value # using sum of first n terms of # Taylor Series def exponential(n, x): # initialize sum of series sum = 1.0 for i in range(n, 0, -1): sum = 1 + x * sum / i print ("e^x =", sum) # Driver program to test above function n = 10x = 1.0exponential(n, x) # This code is contributed by Danish Raza |
chevron_right
filter_none
C#
// C# efficient program to calculate // e raise to the power x using System; class GFG { // Function returns approximate value of e^x // using sum of first n terms of Taylor Series static float exponential(int n, float x) { // initialize sum of series float sum = 1; for (int i = n - 1; i > 0; --i ) sum = 1 + x * sum / i; return sum; } // driver program public static void Main () { int n = 10; float x = 1; Console.Write("e^x = " + exponential(n, x)); } } // This code is contributed by nitin mittal. |
chevron_right
filter_none
PHP
<?php // PHP Efficient program to calculate // e raise to the power x // Returns approximate value of e^x // using sum of first n terms // of Taylor Series function exponential($n, $x) { // initialize sum of series $sum = 1.0; for ($i = $n - 1; $i > 0; --$i ) $sum = 1 + $x * $sum / $i; return $sum; } // Driver Code $n = 10; $x = 1.0; echo("e^x = " . exponential($n, $x)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
e^x = 2.718282
This article is compiled by Rahul and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Write an Efficient C Program to Reverse Bits of a Number
- Efficient program to print all prime factors of a given number
- Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
- Efficient program to print the number of factors of n numbers
- Efficient way to multiply with 7
- Write an Efficient Method to Check if a Number is Multiple of 3
- Space and time efficient Binomial Coefficient
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Space efficient iterative method to Fibonacci number
- Check if a given number is Pronic | Efficient Approach
- C program to calculate the value of nPr
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program to calculate the number of odd days in given number of years
- Program to calculate distance between two points
- Program to calculate area and volume of a Tetrahedron
- Program to calculate value of nCr
- Program to calculate Area Of Octagon
- Program to calculate GST from original and net prices
- Program to calculate Percentile of a student based on rank
- Program to calculate the Surface Area of a Triangular Prism

