Sum of all the numbers in the Nth parenthesis

Given an integer N and a sequence (1), (3, 5), (7, 9, 11), (13, 15, 17, 19), ….. the task is to find the sum of all the numbers in Nth parenthesis.

Examples:

Input: N = 2
Output: 8
3 + 5 = 8



Input: N = 3
Output: 27
7 + 9 + 11 = 27

Approach: It can be observed that for the values of N = 1, 2, 3, … a series will be formed as 1, 8, 27, 64, 125, 216, 343, … whose Nth term is N3

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the sum of the
// numbers in the nth parenthesis
int findSum(int n)
{
    return pow(n, 3);
}
  
// Driver code
int main()
{
    int n = 3;
  
    cout << findSum(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to return the sum of the
// numbers in the nth parenthesis
static int findSum(int n)
{
    return (int)Math.pow(n, 3);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 3;
  
    System.out.println(findSum(n));
}
}
  
// This code is contributed by Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the sum of the 
# numbers in the nth parenthesis 
def findSum(n) :
  
    return n ** 3
  
# Driver code 
if __name__ == "__main__"
  
    n = 3
  
    print(findSum(n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
class GFG
{
      
// Function to return the sum of the
// numbers in the nth parenthesis
static int findSum(int n)
{
    return (int)Math.Pow(n, 3);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 3;
  
    Console.WriteLine(findSum(n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

27

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.