Program to Calculate e^x by Recursion ( using Taylor Series ) - GeeksforGeeks

16 Sep, 2021
The value of the Exponential function can be calculated using Taylor Series. 
 = 1 + x/1! + /2! + /3! + ...... + until n terms
As the number of terms increases the more precise value of ex is obtained.
To find e^x using the recursive function, we need to use static variables. A function can return only one value, and when we need to include multiple values in a recursive function, we use static variables. The Taylor Series is a combination of multiple values like sum, power and factorial term, hence we will use static variables.
For the power of x, we will use p, and for factorials, we will use f as static variables. 
The function shown below is used to increase the power of x.  
p = p*x 
The function below is used to find factorials. 
f = f*n
The function below is used to calculate the summation of the series. 
r+p/f
Where r is the recursive call to the function.
Below is the implementation of the above idea.  
C++
C
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Recursive Function with static// variables p and fdouble e(int x, int n){    static double p = 1, f = 1;    double r;     // Termination condition    if (n == 0)        return 1;     // Recursive call    r = e(x, n - 1);     // Update the power of x    p = p * x;     // Factorial    f = f * n;     return (r + p / f);} // Driver codeint main(){    int x = 4, n = 15;    cout<<"\n"<< e(x, n);     return 0;} // this code is contributed by shivanisinghss2110
// C implementation of the approach#include <stdio.h> // Recursive Function with static// variables p and fdouble e(int x, int n){    static double p = 1, f = 1;    double r;     // Termination condition    if (n == 0)        return 1;     // Recursive call    r = e(x, n - 1);     // Update the power of x    p = p * x;     // Factorial    f = f * n;     return (r + p / f);} // Driver codeint main(){    int x = 4, n = 15;    printf("%lf \n", e(x, n));     return 0;}
// Java implementation of the approachimport java.text.*; class GFG {     // Recursive Function with static    // variables p and f    static double p = 1, f = 1;    static double e(int x, int n)    {        double r;         // Termination condition        if (n == 0)            return 1;         // Recursive call        r = e(x, n - 1);         // Update the power of x        p = p * x;         // Factorial        f = f * n;         return (r + p / f);    }     // Driver code    public static void main(String[] args)    {        int x = 4, n = 15;        DecimalFormat df = new DecimalFormat("0.######");        System.out.println(df.format(e(x, n)));    }} // This code is contributed by mits
# Python implementation of the approach # Recursive Function# global variables p and fp = 1.0f = 1.0  def e(x, n):     global p, f     # Termination condition    if (n == 0):        return 1     # Recursive call    r = e(x, n - 1)     # Update the power of x    p = p * x     # Factorial    f = f * n     return (r + p / f) # Driver code  x = 4n = 15print(e(x, n)) # This contributed by ihritik
// C# implementation of the approachusing System; class GFG {     // Recursive Function with static    // variables p and f    static double p = 1, f = 1;    static double e(int x, int n)    {        double r;         // Termination condition        if (n == 0)            return 1;         // Recursive call        r = e(x, n - 1);         // Update the power of x        p = p * x;         // Factorial        f = f * n;         return (r + p / f);    }     // Driver code    static void Main()    {        int x = 4, n = 15;        Console.WriteLine(Math.Round(e(x, n), 6));    }} // This code is contributed by mits
<script> // Javascript implementation of the approach // Recursive Function with static// variables p and fp = 1, f = 1;function e(x, n){    var r;     // Termination condition    if (n == 0)        return 1;     // Recursive call    r = e(x, n - 1);     // Update the power of x    p = p * x;     // Factorial    f = f * n;     return (r + p / f);} // Driver Codevar x = 4, n = 15;var res = e(x, n); document.write(res.toFixed(6)); // This code is contributed by kirti </script>
54.597883
 
Time Complexity: 
To find this we will determine the total multiplication performed.
e^x = 1 + x/1! + x^2/2! + x^3/3! + ...... + until n terms
       = 1 + x/1  + x*x/1*2 + x*x*x/1*2*3 + x*x*x*x/1*2*3*4 ...... + until n terms
           0     0           2                  4                        8                   Number of Multiplications in above terms
So, for n terms total multiplication performed is comparable to sum of n natural numbers (as a parallel series of even numbers is formed).
and we know sum of n natural numbers = n*(n+1)/2 whose order is n2
Hence, the time complexity if this approach is O(n2)
Auxiliary Space: 
The recursive call will take place n+1 times and hence n + 1 activation records will get created at max. That shows the space complexity is O(n).
Mithun Kumar
ihritik
Kirti_Mangal
pankajsharmagfg
amoghpete
sagartomar9927
shivanisinghss2110
series
Algorithms
Mathematical
Recursion
Mathematical
Recursion
series
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Converting Roman Numerals to Decimal lying between 1 to 3999
K means Clustering - Introduction
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7