Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + .....+ 1/n^n - GeeksforGeeks

01 Nov, 2021
You have been given a series 1 + 1/2^2 + 1/3^3 + .....+ 1/n^n, find out the sum of the series till nth term.
Examples:
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
We use power function to compute power.
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to calculate the following series#include <bits/stdc++.h>using namespace std; // Function to calculate the following seriesdouble 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 Codeint main(){    int n = 3;    double res = Series(n);         cout << res;    return 0;} // This code is contributed by Ankita saini
// C program to calculate the following series#include <math.h>#include <stdio.h>   // Function to calculate the following seriesdouble 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 Codeint main(){    int n = 3;    double res = Series(n);    printf("%.5f", res);    return 0;}
// Java program to calculate the following seriesimport 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 program to calculate the following seriesdef Series(n):    sums = 0.0    for i in range(1, n + 1):        ser = 1 / (i**i)        sums += ser    return sums   # Driver Coden = 3res = round(Series(n), 5)print(res)
// C# program to calculate the following seriesusing 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// PHP program to calculate// the following series   // Function to calculate// the following seriesfunction Series($n){    $i;    $sums = 0.0;    $ser;    for ($i = 1; $i <= $n; ++$i)    {        $ser = 1 / pow($i, $i);        $sums += $ser;    }    return $sums;}       // Driver Code    $n = 3;    $res = Series($n);    echo $res;   // This code is contributed by Vishal Tripathi.?>
<script> // Javascript program to calculate// the following seriesfunction Series(n){    let sums = 0.0;         for(let i = 1; i < n + 1; i++)    {        ser = 1 / Math.pow(i, i);        sums += ser;    }    return sums;} // Driver Codelet n = 3;let res = Math.round(Series(n) * 100000) / 100000; document.write(res); // This code is contributed by rohitsingh07052 </script>
Output: 
1.28704
Time Complexity: O(n)
Auxiliary Space: O(1)
 
vt_m
rohitsingh07052
ankita_saini
souravmahato348
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
How to check if a given point lies inside or outside a polygon?
Check if a number is Palindrome
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
Program to multiply two matrices
Singular Value Decomposition (SVD)