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.5Input: 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++ program to find the sum of the // series 1+(1+2)/2+(1+2+3)/3+... // till N terms #include <bits/stdc++.h> using namespace std;
// Function to return the sum // upto N term of the series double sumOfSeries( int N)
{ return (( double )N
* (2 + (( double )N - 1) * 0.5))
/ 2;
} // Driver Code int main()
{ // Get the value of N
int N = 6;
cout << sumOfSeries(N);
return 0;
} |
// Java program to find the sum of the // series 1+(1+2)/2+(1+2+3)/3+... // till N terms import java.util.*;
public class GFG
{ // Function to return the sum
// upto N term of the series
static double sumOfSeries( int N)
{
return (( double )N
* ( 2 + (( double )N - 1 ) * 0.5 ))
/ 2 ;
}
// Driver Code
public static void main(String args[])
{
// Get the value of N
int N = 6 ;
System.out.println(sumOfSeries(N));
}
} // This code is contributed by Samim Hossain Mondal. |
# Python program to find the sum of the # series 1+(1+2)/2+(1+2+3)/3+... # till N terms # Function to return the sum # upto N term of the series def sumOfSeries(N):
return (N * ( 2 + (N - 1 ) * 0.5 ) / 2 )
# Driver Code # Get the value of N N = 6
print (sumOfSeries(N))
# This code is contributed by Samim Hossain Mondal. |
// C# program to find the sum of the // series 1+(1+2)/2+(1+2+3)/3+... // till N terms using System;
class GFG
{ // Function to return the sum
// upto N term of the series
static double sumOfSeries( int N)
{
return (( double )N
* (2 + (( double )N - 1) * 0.5))
/ 2;
}
// Driver Code
public static void Main()
{
// Get the value of N
int N = 6;
Console.Write(sumOfSeries(N));
}
} // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript code for the above approach
// Function to return the sum
// upto N term of the series
function sumOfSeries(N) {
return (N
* (2 + (N - 1) * 0.5))
/ 2;
}
// Driver Code
// Get the value of N
let N = 6;
document.write(sumOfSeries(N));
// This code is contributed by Potta Lokesh
</script>
|
13.5
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.