Find Nth term of the series 1^3/1+(1^3+2^3)/(1+3)+(1^3+2^3+3^3)/(1+3+5)+…
Given a positive integer N. The task is to find Nth term of the series:
Examples:
Input: N = 2
Output: 2.25
Input: N = 3
Output: 4
Approach:
From the given series, find the formula for Nth term:
1st term = 1^3/1 = 1/1 = 1
2nd term = (1^3+2^3)/(1+3) = (1+8)/4 = 9/4 = 2.25
3rd term = (1^3+2^3+3^3)/(1+3+5) = (1+8+27)/9 = 4
4th term = (1^3+2^3+3^3+4^3)/(1+3+5+7) = (1+8+27+64)/16 = 6.25
.
.
Nth term = ((N*(N+1)/2)^2)/(N*(2+(N-1)*2)/2) = (N+1)^2/4 = (N^2+2N+1)/4
Derivation:
For the series-
Nth term can be written as-
Here,
and 1+3+5+….+(2*N-1) are in A.P.
Rewriting the above equation using the formula for A.P. as-
The Nth term of the given series can be generalized as:
Illustration:
Input: N = 2
Output: 2.25
Explanation: (1^3+2^3)/(1+3)
= (1 +8)/4
= 9/4
= 2.25
Below is the implementation of the above problem:
C++
#include <bits/stdc++.h>
using namespace std;
double nthTerm( int N)
{
return ( pow (N, 2) +
2 * N + 1) / 4;
}
int main()
{
int N = 5;
cout << nthTerm(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double nthTerm( int N)
{
return (Math.pow(N, 2 ) + 2 * N + 1 ) / 4 ;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(nthTerm(N));
}
}
|
Python
import sys
def nthTerm(N):
return ( pow (N, 2 ) + 2 * N + 1 ) / 4
if __name__ = = "__main__" :
N = 5
print (nthTerm(N))
|
C#
using System;
class GFG
{
static double nthTerm( int N)
{
return (Math.Pow(N, 2) +
2 * N + 1) / 4;
}
public static void Main()
{
int N = 5;
Console.Write(nthTerm(N));
}
}
|
Javascript
<script>
const nthTerm = (N) => {
return (Math.pow(N, 2) +
2 * N + 1) / 4;
}
let N = 5;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
16 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...