Given a positive integer N. The task is to find Nth term of the series:
\frac{1^{3}}{1}+\frac{(1^{3}+2^{3})}{1+3}+\frac{(1^{3}+2^{3}+3^{3})}{(1+3+5)}+....
Examples:
Input: N = 2
Output: 2.25Input: 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-
\frac{1^{3}}{1}+\frac{(1^{3}+2^{3})}{1+3}+\frac{(1^{3}+2^{3}+3^{3})}{(1+3+5)}+.... Nth term can be written as-
T_{N}=\frac{(1^{3}+2^{3}+3^{3}+....+N^{3})}{(1+3+5+....+(2*N-1))} Here,
1^{3}+2^{3}+3^{3}+....+N^{3} and 1+3+5+....+(2*N-1) are in A.P.
1^{3}+2^{3}+3^{3}+....+N^{3} = (\frac{(N*(N+1))}{2})^{2}
1+3+5+....+(2*N-1)=\frac{(N*(2*1+(N-1)*2))}{2} Rewriting the above equation using the formula for A.P. as-
T_{N}=\frac{(\frac{(N*(N+1))}{2})^{2}}{\frac{(N*(2*1+(N-1)*2))}{2}}
T_{N}=\frac{(N^{2}*(N+1)^{2})}{(2*N*(2+(N-1)*2))}
T_{N}=\frac{(N*(N+1)^{2})}{(4*(1+N-1))}
T_{N}=\frac{(N*(N+1)^{2})}{4*N}
T_{N}=\frac{(N+1)^{2}}{4}
T_{N}=\frac{(N^{2}+2*N+1)}{4}
The Nth term of the given series can be generalized as:
T_{N}=\frac{(N^{2}+2*N+1)}{4}
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++ program to find N-th term
// in the series
#include <bits/stdc++.h>
using namespace std;
// Function to find N-th term
// in the series
double nthTerm(int N)
{
return (pow(N, 2) +
2 * N + 1) / 4;
}
// Driver Code
int main()
{
// Get the value of N
int N = 5;
cout << nthTerm(N);
return 0;
}
// Java code for the above approach
import java.io.*;
class GFG {
// Function to find N-th term
// in the series
static double nthTerm(int N)
{
return (Math.pow(N, 2) + 2 * N + 1) / 4;
}
public static void main(String[] args)
{
// Get the value of N
int N = 5;
System.out.println(nthTerm(N));
}
}
// This code is contributed by Potta Lokesh
# python 3 program for the above approach
import sys
# Function to find N-th term
# in the series
def nthTerm(N):
return (pow(N, 2) + 2 * N + 1) / 4
# Driver Code
if __name__ == "__main__":
N = 5
print(nthTerm(N))
# This code is contributed by hrithikgarg03188
// C# program to find N-th term
// in the series
using System;
class GFG
{
// Function to find N-th term
// in the series
static double nthTerm(int N)
{
return (Math.Pow(N, 2) +
2 * N + 1) / 4;
}
// Driver Code
public static void Main()
{
// Get the value of N
int N = 5;
Console.Write(nthTerm(N));
}
}
// This code is contributed by Samim Hosdsain Mondal.
<script>
// JavaScript program to find N-th term
// in the series
// Function to find N-th term
// in the series
const nthTerm = (N) => {
return (Math.pow(N, 2) +
2 * N + 1) / 4;
}
// Driver Code
// Get the value of N
let N = 5;
document.write(nthTerm(N));
// This code is contributed by rakeshsahni
</script>
Output
9
Time Complexity: O(1)
Auxiliary Space: O(1)