📜  程序找出1 + x / 2的总和! + x ^ 2/3! +…+ x ^ n /(n + 1)!

📅  最后修改于: 2021-05-06 22:02:55             🧑  作者: Mango

给定数字x和n,任务是找到以下x系列的和,直到n个项:

例子:

输入:x = 5,n = 2输出:7.67说明:前两个项的和1+\frac{5}{2!}+\frac{5^{2}}{3!} = 7.67输入:x = 5,n = 4输出:18.08说明: 1+\frac{5}{2!}+\frac{5^{2}}{3!}+\frac{5^{3}}{4!}+\frac{5^{4}}{5!} = 18.08

方法:迭代循环直到第n个项,在每次迭代中计算公式,即

系列的第n个项= \frac{x^{i}}{(i+1)!}

下面是上述方法的实现:

C++
// C++ Program to compute sum of
// 1 + x/2! + x^2/3! +...+x^n/(n+1)!
 
#include 
#include 
using namespace std;
 
// Method to find the factorial of a number
int fact(int n)
{
    if (n == 1)
        return 1;
 
    return n * fact(n - 1);
}
 
// Method to compute the sum
double sum(int x, int n)
{
    double i, total = 1.0;
 
    // Iterate the loop till n
    // and compute the formula
    for (i = 1; i <= n; i++) {
        total = total + (pow(x, i) / fact(i + 1));
    }
 
    return total;
}
 
// Driver code
int main()
{
 
    // Get x and n
    int x = 5, n = 4;
 
    // Print output
    cout << "Sum is: " << sum(x, n);
 
    return 0;
}


Java
// Java Program to compute sum of
// 1 + x/2! + x^2/3! +...+x^n/(n+1)!
 
public class SumOfSeries {
 
    // Method to find factorial of a number
    static int fact(int n)
    {
        if (n == 1)
            return 1;
 
        return n * fact(n - 1);
    }
 
    // Method to compute the sum
    static double sum(int x, int n)
    {
        double total = 1.0;
 
        // Iterate the loop till n
        // and compute the formula
        for (int i = 1; i <= n; i++) {
            total = total + (Math.pow(x, i) / fact(i + 1));
        }
 
        return total;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        System.out.print("Sum is: " + sum(x, n));
    }
}


Python3
# Python3 Program to compute sum of
# 1 + x / 2 ! + x ^ 2 / 3 ! +...+x ^ n/(n + 1)!
 
# Method to find the factorial of a number
def fact(n):
    if n == 1:
        return 1
    else:
        return n * fact(n - 1)
 
# Method to compute the sum
def sum(x, n):
    total = 1.0
 
    # Iterate the loop till n
    # and compute the formula
    for i in range (1, n + 1, 1):
        total = total + (pow(x, i) / fact(i + 1))
 
    return total
 
# Driver code
if __name__== '__main__':
     
    # Get x and n
    x = 5
    n = 4
 
    # Print output
    print ("Sum is: {0:.4f}".format(sum(x, n)))
     
# This code is contributed by
# SURENDRA_GANGWAR


C#
// C# Program to compute sum of
// 1 + x/2! + x^2/3! +...+x^n/(n+1)!
using System;
 
class SumOfSeries {
 
    // Method to find factorial of a number
    static int fact(int n)
    {
        if (n == 1)
            return 1;
 
        return n * fact(n - 1);
    }
 
    // Method to compute the sum
    static double sum(int x, int n)
    {
        double total = 1.0;
 
        // Iterate the loop till n
        // and compute the formula
        for (int i = 1; i <= n; i++) {
            total = total + (Math.Pow(x, i) / fact(i + 1));
        }
 
        return total;
    }
 
    // Driver Code
    public static void Main()
    {
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        Console.WriteLine("Sum is: " + sum(x, n));
    }
}
 
// This code is contributed
// by anuj_67..


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to compute the series sum
double sum(int x, int n)
{
    double total = 1.0;
 
    // To store the value of S[i-1]
    double previous = 1.0;
 
    // Iterate over n to store sum in total
    for (int i = 1; i <= n; i++)
    {
 
        // Update previous with S[i]
        previous = (previous * x) / (i + 1);
        total = total + previous;
    }
    return total;
}
 
// Driver code
int main()
{
    // Get x and n
    int x = 5, n = 4;
     
    // Find and print the sum
    cout << "Sum is: " << sum(x, n);
 
    return 0;
}
 
// This code is contributed by jit_t


Java
// Java implementation of the approach
 
public class GFG {
 
    // Function to compute the series sum
    static double sum(int x, int n)
    {
 
        double total = 1.0;
 
        // To store the value of S[i-1]
        double previous = 1.0;
 
        // Iterate over n to store sum in total
        for (int i = 1; i <= n; i++) {
 
            // Update previous with S[i]
            previous = (previous * x) / (i + 1);
            total = total + previous;
        }
 
        return total;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        System.out.print("Sum is: " + sum(x, n));
    }
}


Python3
# Python implementation of the approach
 
# Function to compute the series sum
def sum(x, n):
    total = 1.0;
 
    # To store the value of S[i-1]
    previous = 1.0;
 
    # Iterate over n to store sum in total
    for i in range(1, n + 1):
         
        # Update previous with S[i]
        previous = (previous * x) / (i + 1);
        total = total + previous;
 
    return total;
 
# Driver code
if __name__ == '__main__':
     
    # Get x and n
    x = 5;
    n = 4;
 
    # Find and prthe sum
    print("Sum is: ", sum(x, n));
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to compute the series sum
    public double sum(int x, int n)
    {
        double total = 1.0;
 
        // To store the value of S[i-1]
        double previous = 1.0;
 
        // Iterate over n to store sum in total
        for (int i = 1; i <= n; i++)
        {
 
            // Update previous with S[i]
            previous = ((previous * x) / (i + 1));
            total = total + previous;
        }
 
        return total;
    }
}
 
// Driver code
class geek
{
    public static void Main()
    {
        GFG g = new GFG();
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        Console.WriteLine("Sum is: " + g.sum(x, n));
    }
}
 
// This code is contributed by SoM15242


输出:
Sum is: 18.0833

高效方法:以上算法的时间复杂度为O( n^{2}  ),因为对于每个和迭代,要计算的阶乘为O(n)。可以观察到i^{th}  该系列的术语可以写成S_i = (\frac{x}{i+1}) \cdot S_{i-1}  , 在哪里S_0 = 1  。现在我们可以迭代i  计算总和。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to compute the series sum
double sum(int x, int n)
{
    double total = 1.0;
 
    // To store the value of S[i-1]
    double previous = 1.0;
 
    // Iterate over n to store sum in total
    for (int i = 1; i <= n; i++)
    {
 
        // Update previous with S[i]
        previous = (previous * x) / (i + 1);
        total = total + previous;
    }
    return total;
}
 
// Driver code
int main()
{
    // Get x and n
    int x = 5, n = 4;
     
    // Find and print the sum
    cout << "Sum is: " << sum(x, n);
 
    return 0;
}
 
// This code is contributed by jit_t

Java

// Java implementation of the approach
 
public class GFG {
 
    // Function to compute the series sum
    static double sum(int x, int n)
    {
 
        double total = 1.0;
 
        // To store the value of S[i-1]
        double previous = 1.0;
 
        // Iterate over n to store sum in total
        for (int i = 1; i <= n; i++) {
 
            // Update previous with S[i]
            previous = (previous * x) / (i + 1);
            total = total + previous;
        }
 
        return total;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        System.out.print("Sum is: " + sum(x, n));
    }
}

Python3

# Python implementation of the approach
 
# Function to compute the series sum
def sum(x, n):
    total = 1.0;
 
    # To store the value of S[i-1]
    previous = 1.0;
 
    # Iterate over n to store sum in total
    for i in range(1, n + 1):
         
        # Update previous with S[i]
        previous = (previous * x) / (i + 1);
        total = total + previous;
 
    return total;
 
# Driver code
if __name__ == '__main__':
     
    # Get x and n
    x = 5;
    n = 4;
 
    # Find and prthe sum
    print("Sum is: ", sum(x, n));
 
# This code is contributed by 29AjayKumar

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to compute the series sum
    public double sum(int x, int n)
    {
        double total = 1.0;
 
        // To store the value of S[i-1]
        double previous = 1.0;
 
        // Iterate over n to store sum in total
        for (int i = 1; i <= n; i++)
        {
 
            // Update previous with S[i]
            previous = ((previous * x) / (i + 1));
            total = total + previous;
        }
 
        return total;
    }
}
 
// Driver code
class geek
{
    public static void Main()
    {
        GFG g = new GFG();
 
        // Get x and n
        int x = 5, n = 4;
 
        // Find and print the sum
        Console.WriteLine("Sum is: " + g.sum(x, n));
    }
}
 
// This code is contributed by SoM15242
输出:
Sum is: 18.083333333333336

时间复杂度: O(n)