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++
#include <bits/stdc++.h>
using namespace std;
float exponential(int n, float x)
{
float sum = 1.0f;
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
int main()
{
int n = 10;
float x = 1.0f;
cout << "e^x = " << fixed << setprecision(5) << exponential(n, x);
return 0;
}
|
C
#include <stdio.h>
float exponential(int n, float x)
{
float sum = 1.0f;
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
int main()
{
int n = 10;
float x = 1.0f;
printf("e^x = %f", exponential(n, x));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static float exponential(int n, float x)
{
float sum = 1;
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
public static void main (String[] args)
{
int n = 10;
float x = 1;
System.out.println("e^x = "+exponential(n,x));
}
}
|
Python3
def exponential(n, x):
sum = 1.0
for i in range(n, 0, -1):
sum = 1 + x * sum / i
print ("e^x =", sum)
n = 10
x = 1.0
exponential(n, x)
|
C#
using System;
class GFG
{
static float exponential(int n, float x)
{
float sum = 1;
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
public static void Main ()
{
int n = 10;
float x = 1;
Console.Write("e^x = " + exponential(n, x));
}
}
|
PHP
<?php
function exponential($n, $x)
{
$sum = 1.0;
for ($i = $n - 1; $i > 0; --$i )
$sum = 1 + $x * $sum / $i;
return $sum;
}
$n = 10;
$x = 1.0;
echo("e^x = " . exponential($n, $x));
?>
|
Javascript
<script>
function exponential(n , x) {
var sum = 1;
for (i = n - 1; i > 0; --i)
sum = 1 + x * sum / i;
return sum;
}
var n = 10;
var x = 1;
document.write("e^x = " + exponential(n, x).toFixed(6));
</script>
|
Output:
e^x = 2.718282
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
You'll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We're here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000 geeks! Whether it's
DSA in C++,
Java,
Python, or
JavaScript we've got you covered. Let's embark on this exciting journey together!