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++
#include <bits/stdc++.h>
using namespace std;
int findSum( int n)
{
return pow (n, 3);
}
int main()
{
int n = 3;
cout << findSum(n);
return 0;
}
|
Java
class GFG
{
static int findSum( int n)
{
return ( int )Math.pow(n, 3 );
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(findSum(n));
}
}
|
Python3
def findSum(n) :
return n * * 3 ;
if __name__ = = "__main__" :
n = 3 ;
print (findSum(n));
|
C#
using System;
class GFG
{
static int findSum( int n)
{
return ( int )Math.Pow(n, 3);
}
public static void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
|
Javascript
<script>
function findSum(n)
{
return Math.pow(n, 3);
}
var n = 3;
document.write(findSum(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
10 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...