Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion - GeeksforGeeks

17 Mar, 2021
Given an integer n, the task is to find the sum of the series 11 + 22 + 33 + ..... + nn using recursion.Examples:  
Input: n = 2 Output: 5 11 + 22 = 1 + 4 = 5 Input: n = 3 Output: 32 11 + 22 + 33 = 1 + 4 + 27 = 32  
 
Approach: Starting from n, start adding all the terms of the series one by one with the value of n getting decremented by 1 in each recursive call until the value of n = 1 for which return 1 as 11 = 1.Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define ll long long int // Recursive function to return// the sum of the given seriesll sum(int n){     // 1^1 = 1    if (n == 1)        return 1;    else         // Recursive call        return ((ll)pow(n, n) + sum(n - 1));} // Driver codeint main(){    int n = 2;    cout << sum(n);     return 0;}
// Java implementation of the approachclass GFG {     // Recursive function to return    // the sum of the given series    static long sum(int n)    {         // 1^1 = 1        if (n == 1)            return 1;        else             // Recursive call            return ((long)Math.pow(n, n) + sum(n - 1));    }     // Driver code    public static void main(String args[])    {        int n = 2;        System.out.println(sum(n));    }}
# Python3 implementation of the approach # Recursive function to return# the sum of the given seriesdef sum(n):    if n == 1:        return 1    else:         # Recursive call        return pow(n, n) + sum(n - 1) # Driver coden = 2print(sum(n)) # This code is contributed# by Shrikant13
// C# implementation of the approachusing System;class GFG {     // Recursive function to return    // the sum of the given series    static long sum(int n)    {        // 1^1 = 1        if (n == 1)            return 1;        else             // Recursive call            return ((long)Math.Pow(n, n) + sum(n - 1));    }     // Driver code    public static void Main()    {        int n = 2;        Console.Write(sum(n));    }}
<?php// PHP implementation of the approach // Recursive function to return// the sum of the given seriesfunction sum($n){     // 1^1 = 1    if ($n == 1)        return 1;    else         // Recursive call        return (pow($n, $n) + sum($n - 1));} // Driver code$n = 2;echo(sum($n)); // This code is contributed// by Code_Mech?>
<script>  // Javascript implementation of the approach // Recursive function to return// the sum of the given seriesfunction sum(n){     // 1^1 = 1    if (n == 1)        return 1;    else         // Recursive call        return (Math.pow(n, n) + sum(n - 1));} // Driver codevar n = 2;document.write(sum(n)); // This code is contributed by rutvik_56.</script>
5
 
shrikanth13
Code_Mech
rutvik_56
maths-power
series-sum
Mathematical
Recursion
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Program for Tower of Hanoi
Backtracking | Introduction
Print all possible combinations of r elements in a given array of size n
Program for Sum of the digits of a given number