📜  高效的程序来计算e ^ x

📅  最后修改于: 2021-04-29 04:09:53             🧑  作者: Mango

指数函数e ^ x的值可以使用以下泰勒级数表示。

e^x = 1 + x/1! + x^2/2! + x^3/3! + ...... 

如何有效地计算上述系列的总和?
该系列可以重写为

e^x = 1 + (x/1) (1 + (x/2) (1 + (x/3) (........) ) ) 

假设需要为n个项计算总和,我们可以使用以下循环来计算总和。

for (i = n - 1, sum = 1; i > 0; --i )
    sum = 1 + x * sum / i; 

以下是上述想法的实现。

C++
// C++ Efficient program to calculate
// e raise to the power x
#include 
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


C
// C Efficient program to calculate
// e raise to the power x
#include 
 
// 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;
}


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


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 = 10
x = 1.0
exponential(n, x)
 
# This code is contributed by Danish Raza


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.


PHP
 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.
?>


Javascript


输出:

e^x = 2.718282