📌  相关文章
📜  程序来查找级数1 + 1/2 ^ 2 + 1/3 ^ 3 +….. + 1 / n ^ n的和

📅  最后修改于: 2021-05-04 11:39:02             🧑  作者: Mango

给您一个级数1 + 1/2 ^ 2 + 1/3 ^ 3 +….. + 1 / n ^ n,找出直到第n个项的级数之和。
例子:

Input : n = 3
Output : 1.28704
Explanation : 1 + 1/2^2 + 1/3^3

Input : n = 5
Output : 1.29126
Explanation : 1 + 1/2^2 + 1/3^3 + 1/4^4 + 1/5^5

我们使用函数来计算功效。

C/C++
// C program to calculate the following series
#include 
#include 
  
// Function to calculate the following series
double Series(int n)
{
    int i;
    double sums = 0.0, ser;
    for (i = 1; i <= n; ++i) {
        ser = 1 / pow(i, i);
        sums += ser;
    }
    return sums;
}
  
// Driver Code
int main()
{
    int n = 3;
    double res = Series(n);
    printf("%.5f", res);
    return 0;
}


Java
// Java program to calculate the following series
import java.io.*;
  
class Maths {
  
    // Function to calculate the following series
    static double Series(int n)
    {
        int i;
        double sums = 0.0, ser;
        for (i = 1; i <= n; ++i) {
            ser = 1 / Math.pow(i, i);
            sums += ser;
        }
        return sums;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
        double res = Series(n);
        res = Math.round(res * 100000.0) / 100000.0;
        System.out.println(res);
    }
}


Python
# Python program to calculate the following series
def Series(n):
    sums = 0.0
    for i in range(1, n + 1):
        ser = 1 / (i**i)
        sums += ser
    return sums
  
# Driver Code
n = 3
res = round(Series(n), 5)
print(res)


C#
// C# program to calculate the following series
using System;
  
class Maths {
  
    // Function to calculate the following series
    static double Series(int n)
    {
        int i;
        double sums = 0.0, ser;
        for (i = 1; i <= n; ++i) {
            ser = 1 / Math.Pow(i, i);
            sums += ser;
        }
        return sums;
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 3;
        double res = Series(n);
        res = Math.Round(res * 100000.0) / 100000.0;
        Console.Write(res);
    }
}
/*This code is contributed by vt_m.*/


PHP

output:1.28704


输出:

1.28704