Find the sum of the series 1+(1+2)/2+(1+2+3)/3+… till N terms
Last Updated :
12 Jul, 2022
Given a number N, the task is to find the sum of the below series till N terms.
1+(1+2)/2+(1+2+3)/3+… till N terms
Examples:
Input: N = 3
Output: 4.5
Input: N = 4
Output: 7
Approach:
From the given series, find the formula for the Nth term:
1st term = 1 = 1
2nd term = (1+2)/2 = 1.5
3rd term = (1+2+3)/3 = 2
4th term = (1+2+3+4)/4 = 2.5
.
.
Nth term = N*(N +1)/(2*N) = (N+1)/2
Derivation:
The following series of steps can be used to derive the formula to find the sum of N terms-
The sequence-
1+(1+2)/2+(1+2+3)/3+… till N terms
can be written as-
1 +1.5 +2 +2.5 +3 +3.5 +4 +…till N terms
The above series is an Arithmetic Progression(AP) series. So, we can directly apply the formula of sum of N terms in AP.
The Sum of N terms of an AP can also be given by SN,
SN = N * [First term + Last term] / 2
SN = N * [2 * a + (N – 1) * d] / 2 -(1)
From the above equation, it is known that,
a(first term)=1, d(common difference) = 1.5 -1= 0.5
Substituting the values of a and d in the equation (1), we get-
SN = N * (2 + (N – 1) * 0.5) / 2
Illustration:
Input: N = 5
Output: 10
Explanation:
SN = 5 * [2 * 1 + (5 – 1) * 0.5] / 2
= 5 * (2 + 2) / 2
= 5 * 2
= 10
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double sumOfSeries( int N)
{
return (( double )N
* (2 + (( double )N - 1) * 0.5))
/ 2;
}
int main()
{
int N = 6;
cout << sumOfSeries(N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static double sumOfSeries( int N)
{
return (( double )N
* ( 2 + (( double )N - 1 ) * 0.5 ))
/ 2 ;
}
public static void main(String args[])
{
int N = 6 ;
System.out.println(sumOfSeries(N));
}
}
|
Python
def sumOfSeries(N):
return (N * ( 2 + (N - 1 ) * 0.5 ) / 2 )
N = 6
print (sumOfSeries(N))
|
C#
using System;
class GFG
{
static double sumOfSeries( int N)
{
return (( double )N
* (2 + (( double )N - 1) * 0.5))
/ 2;
}
public static void Main()
{
int N = 6;
Console.Write(sumOfSeries(N));
}
}
|
Javascript
<script>
function sumOfSeries(N) {
return (N
* (2 + (N - 1) * 0.5))
/ 2;
}
let N = 6;
document.write(sumOfSeries(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...