Efficient program to calculate e^x

29 Oct, 2021
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
Java
Python3
C#
PHP
Javascript
// 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 Seriesfloat 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 codeint 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
// 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 Seriesfloat 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 functionint main(){    int n = 10;    float x = 1.0f;    printf("e^x = %f", exponential(n, x));    return 0;}
// Java efficient program to calculate// e raise to the power ximport 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
# Python program to calculate# e raise to the power x # Function to calculate value# using sum of first n terms of# Taylor Seriesdef 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 functionn = 10x = 1.0exponential(n, x) # This code is contributed by Danish Raza
// C# efficient program to calculate// e raise to the power xusing 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.
<?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 Seriesfunction 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.?>
<script>// javascript efficient program to calculate// e raise to the power x     // Function returns approximate value of e^x    // using sum of first n terms of Taylor Series    function exponential(n , x) {        // initialize sum of series        var sum = 1;         for (i = n - 1; i > 0; --i)            sum = 1 + x * sum / i;         return sum;    }     // driver program             var n = 10;        var x = 1;        document.write("e^x = " + exponential(n, x).toFixed(6)); // This code contributed by Rajput-Ji </script>
Output: 
e^x = 2.718282
Time Complexity: O(n)
Auxiliary Space: O(1)
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 
nitin mittal
jit_t
Kirti_Mangal
rathbhupendra
Rajput-Ji
subhammahato348
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.