📌  相关文章
📜  程序获得系列总和:1 – x ^ 2/2! + x ^ 4/4! -…。至第n个学期

📅  最后修改于: 2021-04-29 14:57:23             🧑  作者: Mango

这是一个数学级数程序,用户必须输入要找到级数之和的项数。接下来,我们还需要x的值,该值构成了序列的基础。

例子:

Input : x = 9, n = 10
Output : -5.1463

Input : x = 5, n = 15
Output : 0.2837

简单方法:
我们使用两个嵌套循环来计算阶乘,并使用幂函数来计算幂。

C
// C program to get the sum of the series
#include 
#include 
 
// Function to get the series
double Series(double x, int n)
{
    double sum = 1, term = 1, fct, j, y = 2, m;
 
    // Sum of n-1 terms starting from 2nd term
    int i;
    for (i = 1; i < n; i++) {
        fct = 1;
        for (j = 1; j <= y; j++) {
            fct = fct * j;
        }
        term = term * (-1);
        m = term * pow(x, y) / fct;
        sum = sum + m;
        y += 2;
    }
    return sum;
}
 
// Driver Code
int main()
{
    double x = 9;
    int n = 10;
    printf("%.4f", Series(x, n));
    return 0;
}


Java
// Java program to get the sum of the series
import java.io.*;
 
class MathSeries {
 
    // Function to get the series
    static double Series(double x, int n)
    {
        double sum = 1, term = 1, fct, j, y = 2, m;
 
       // Sum of n-1 terms starting from 2nd term
        int i;
        for (i = 1; i < n; i++) {
            fct = 1;
            for (j = 1; j <= y; j++) {
                fct = fct * j;
            }
            term = term * (-1);
            m = Math.pow(x, y) / fct;
            m = m * term;
            sum = sum + m;
            y += 2;
        }
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        double x = 3;
        int n = 4;
        System.out.println(Math.round(Series(x, n) *
                                10000.0) / 10000.0);
    }
}


Python3
# Python3 code to get the sum of the series
import math
 
# Function to get the series
def Series( x , n ):
    sum = 1
    term = 1
    y = 2
     
    # Sum of n-1 terms starting from 2nd term
    for i in range(1,n):
        fct = 1
        for j in range(1,y+1):
            fct = fct * j
         
        term = term * (-1)
        m = term * math.pow(x, y) / fct
        sum = sum + m
        y += 2
     
    return sum
 
# Driver Code
x = 9
n = 10
print('%.4f'% Series(x, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to get the sum of the series
using System;
 
class GFG {
 
    // Function to get the series
    static double Series(double x, int n)
    {
        double sum = 1, term = 1, fct, j, y = 2, m;
 
    // Sum of n-1 terms starting from 2nd term
        int i;
        for (i = 1; i < n; i++) {
            fct = 1;
            for (j = 1; j <= y; j++) {
                fct = fct * j;
            }
            term = term * (-1);
            m = Math.Pow(x, y) / fct;
            m = m * term;
            sum = sum + m;
            y += 2;
        }
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        double x = 9;
        int n = 10;
        Console.Write(Series(x, n) *
                            10000.0 / 10000.0);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program to get the sum of the series
#include 
#include 
 
// Function to get the series
double Series(double x, int n)
{
    double sum = 1, term = 1, fct = 1, p = 1, multi = 1;
     
    // Computing sum of remaining n-1 terms.
    for (int i = 1; i < n; i++) {
        fct = fct * multi * (multi+1);
        p = p*x*x;
        term = (-1) * term;       
        multi += 2;
        sum = sum + (term * p)/fct;
    }
    return sum;
}
 
// Driver Code
int main()
{
    double x = 9;
    int n = 10;
    printf("%.4f", Series(x, n));
    return 0;
}


Java
// Java program to get
// the sum of the series
import java.io.*;
 
class GFG {
     
    // Function to get
    // the series
    static double Series(double x, int n)
    {
        double sum = 1, term = 1, fct = 1;
        double p = 1, multi = 1;
         
        // Computing sum of remaining
        // n-1 terms.
        for (int i = 1; i < n; i++)
        {
            fct = fct * multi * (multi + 1);
            p = p * x * x;
            term = (-1) * term;    
            multi += 2;
            sum = sum + (term * p) / fct;
        }
        return sum;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        double x = 9;
        int n = 10;
        System.out.printf("%.4f", Series(x, n));
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python3 code to get the sum of the series
 
# Function to get the series
def Series(x, n):
    sum = 1
    term = 1
    fct = 1
    p = 1
    multi = 1
     
    # Computing sum of remaining n-1 terms.
    for i in range(1, n):
        fct = fct * multi * (multi+1)
        p = p*x*x
        term = (-1) * term
        multi += 2
        sum = sum + (term * p)/fct
     
    return sum
 
# Driver Code
x = 9
n = 10
print('%.4f'% Series(x, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to get
// the sum of the series
using System;
 
class GFG {
     
    // Function to get
    // the series
    static float Series(double x, int n)
    {
        double sum = 1, term = 1, fct = 1;
        double p = 1, multi = 1;
         
        // Computing sum of remaining
        // n-1 terms.
        for (int i = 1; i < n; i++)
        {
            fct = fct * multi * (multi + 1);
            p = p * x * x;
            term = (-1) * term;
            multi += 2;
            sum = sum + (term * p) / fct;
        }
        return (float)sum;
    }
     
    // Driver Code
    public static void Main()
    {
        double x = 9;
        int n = 10;
        Console.Write(Series(x, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

-5.1463

高效的方法:
通过使用先前迭代中计算出的值,我们可以避免内部循环和幂函数的使用。

C++

// C++ program to get the sum of the series
#include 
#include 
 
// Function to get the series
double Series(double x, int n)
{
    double sum = 1, term = 1, fct = 1, p = 1, multi = 1;
     
    // Computing sum of remaining n-1 terms.
    for (int i = 1; i < n; i++) {
        fct = fct * multi * (multi+1);
        p = p*x*x;
        term = (-1) * term;       
        multi += 2;
        sum = sum + (term * p)/fct;
    }
    return sum;
}
 
// Driver Code
int main()
{
    double x = 9;
    int n = 10;
    printf("%.4f", Series(x, n));
    return 0;
}

Java

// Java program to get
// the sum of the series
import java.io.*;
 
class GFG {
     
    // Function to get
    // the series
    static double Series(double x, int n)
    {
        double sum = 1, term = 1, fct = 1;
        double p = 1, multi = 1;
         
        // Computing sum of remaining
        // n-1 terms.
        for (int i = 1; i < n; i++)
        {
            fct = fct * multi * (multi + 1);
            p = p * x * x;
            term = (-1) * term;    
            multi += 2;
            sum = sum + (term * p) / fct;
        }
        return sum;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        double x = 9;
        int n = 10;
        System.out.printf("%.4f", Series(x, n));
    }
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Python3 code to get the sum of the series
 
# Function to get the series
def Series(x, n):
    sum = 1
    term = 1
    fct = 1
    p = 1
    multi = 1
     
    # Computing sum of remaining n-1 terms.
    for i in range(1, n):
        fct = fct * multi * (multi+1)
        p = p*x*x
        term = (-1) * term
        multi += 2
        sum = sum + (term * p)/fct
     
    return sum
 
# Driver Code
x = 9
n = 10
print('%.4f'% Series(x, n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program to get
// the sum of the series
using System;
 
class GFG {
     
    // Function to get
    // the series
    static float Series(double x, int n)
    {
        double sum = 1, term = 1, fct = 1;
        double p = 1, multi = 1;
         
        // Computing sum of remaining
        // n-1 terms.
        for (int i = 1; i < n; i++)
        {
            fct = fct * multi * (multi + 1);
            p = p * x * x;
            term = (-1) * term;
            multi += 2;
            sum = sum + (term * p) / fct;
        }
        return (float)sum;
    }
     
    // Driver Code
    public static void Main()
    {
        double x = 9;
        int n = 10;
        Console.Write(Series(x, n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

-5.1463