📌  相关文章
📜  系列的总和1 + x / 1 + x ^ 2/2 + x ^ 3/3 + .. + x ^ n / n

📅  最后修改于: 2021-05-04 14:09:50             🧑  作者: Mango

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

Input : base = 2, range = 5
Output : 18.07

Input : base = 1, range = 10
Output : 3.93

方法1(简单)我们只需要按照序列进行操作,将基数的值放在x处,将值范围放在n处即可得到总和。

C++
// C++ program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
#include 
#include 
class gfg
{
public :
double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (pow(x, i) / i);
    return total;
}
};
// Driver code
int main()
{
    gfg g;
    int x = 2;
    int n = 5;
    //std::cout<


C
// C program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
#include 
 
double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (pow(x, i) / i);
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    printf("%.2f", sum(x, n));
    return 0;
}


Java
// Java program to find sum of series
// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/n
import static java.lang.Math.pow;
 
class GFG
{
     
// Java code to print the
// sum of the series
static double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (Math.pow(x, i) / i);
 
    return total;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2;
    int n = 5;
    System.out.printf("%.2f", sum(x, n));
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python3 code to find sum of series
# 1 + x/1 + x^2/2 + x^3/3 + .. .+ x^n/n
 
def SUM(x, n):
    total = 1
    for i in range(1, n + 1):
        total = total + ((x**i)/i)
    return total
 
# Driver Code
x = 2
n = 5
s = SUM(x, n)
print(round(s, 2))


C#
// C# program to find sum of series
// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/n
using System;
 
class GFG
{
 
    // Java code to print the
    // sum of the series
    static float sum(int x, int n)
    {
        double i, total = 1.0;
        for (i = 1; i <= n; i++)
            total = total +
                    (Math.Pow(x, i) / i);
 
        return (float)total;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 2;
        int n = 5;
        Console.WriteLine(sum(x, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
using namespace std;
 
// C++ code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    cout << fixed << setprecision(2) << sum(x, n);
    return 0;
}
 
// This code is contributed by shubhamsingh10


C
// C program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
#include 
 
// C code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++) {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    printf("%.2f", sum(x, n));
    return 0;
}


Java
// Java program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
class GFG
{
 
// Java code to print the sum
// of the given series
static double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2;
    int n = 5;
    System.out.printf("%.2f", sum(x, n));
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python 3 program to find sum of series
# 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
# Python 3 code to print the
# sum of the series
def sum(x, n):
 
    total = 1.0
    multi = x
    for i in range(1, n+1):
        total = total + multi / i
        multi = multi * x
     
    return total
 
 
# Driver code
x = 2
n = 5
print(round(sum(x, n), 2))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
using System;
 
class GFG
{
 
    // Java code to print the sum
    // of the given series
    static float sum(int x, int n)
    {
        double i, total = 1.0, multi = x;
        for (i = 1; i <= n; i++)
        {
            total = total + multi / i;
            multi = multi * x;
        }
        return (float)total;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 2;
        int n = 5;
        Console.WriteLine(sum(x, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

18.07

方法2(优化)我们可以避免使用pow()函数,而可以重用先前计算的功率。

C++

// C++ program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
using namespace std;
 
// C++ code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    cout << fixed << setprecision(2) << sum(x, n);
    return 0;
}
 
// This code is contributed by shubhamsingh10

C

// C program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include 
#include 
 
// C code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++) {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    printf("%.2f", sum(x, n));
    return 0;
}

Java

// Java program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
class GFG
{
 
// Java code to print the sum
// of the given series
static double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2;
    int n = 5;
    System.out.printf("%.2f", sum(x, n));
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal

Python3

# Python 3 program to find sum of series
# 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
# Python 3 code to print the
# sum of the series
def sum(x, n):
 
    total = 1.0
    multi = x
    for i in range(1, n+1):
        total = total + multi / i
        multi = multi * x
     
    return total
 
 
# Driver code
x = 2
n = 5
print(round(sum(x, n), 2))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
using System;
 
class GFG
{
 
    // Java code to print the sum
    // of the given series
    static float sum(int x, int n)
    {
        double i, total = 1.0, multi = x;
        for (i = 1; i <= n; i++)
        {
            total = total + multi / i;
            multi = multi * x;
        }
        return (float)total;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 2;
        int n = 5;
        Console.WriteLine(sum(x, n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

18.07