Given a positive integer n, the problem is to find the sum of the given series of n terms:
1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . . . . + 1/(n*(n+1))
Input : 3
Output : 0.75
( 1/(1*2)+ 1/(2*3) + 1/(3*4) )
= (1/2 + 1/6 + 1/12)
= 0.75
Input : 10
Output : 0.909
( 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) +
1/(5*6) + 1/(6*7) + 1/(7*8) + 1/(8*9) +
1/(9*10) + 1/(10*11) )
= (1/2 + 1/6 + 1/12 + 1/20 + 1/30 +
1/42 + 1/56 + 1/72 + 1/90 + 1/110)
= 0.909
Naive Approach: Use a for loop to calculate each term iteratively and add it to the final sum.
C++
#include <bits/stdc++.h>
using namespace std;
double sumOfTheSeries( int n)
{
double sum = 0.0;
for ( int i = 1; i <= n; i++)
sum += 1.0 / (i * (i + 1));
return sum;
}
int main()
{
int n = 10;
cout << sumOfTheSeries(n);
return 0;
}
|
Java
class demo {
public static double sumOfTheSeries( int n)
{
double sum = 0.0 ;
for ( int i = 1 ; i <= n; i++)
sum += 1.0 / (i * (i + 1 ));
return sum;
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(sumOfTheSeries(n));
}
}
|
Python3
def sumOfTheSeries( n ):
sum = 0
for i in range ( 1 , n + 1 ):
sum + = 1.0 / (i * (i + 1 ));
return sum
if __name__ = = '__main__' :
ans = sumOfTheSeries( 10 )
print ( round (ans, 6 ))
|
C#
using System;
class demo {
public static double sumOfTheSeries( int n)
{
double sum = 0.0;
for ( int i = 1; i <= n; i++)
sum += 1.0 / (i * (i + 1));
return sum;
}
public static void Main()
{
int n = 10;
Console.Write(sumOfTheSeries(n));
}
}
|
PHP
<?php
function sumOfTheSeries( $n )
{
$sum = 0.0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += 1.0 / ( $i * ( $i + 1));
return $sum ;
}
$n = 10;
echo sumOfTheSeries( $n );
?>
|
Javascript
<script>
function sumOfTheSeries(n)
{
let sum = 0.0;
for (let i = 1; i <= n; i++)
sum += 1.0 / (i * (i + 1));
return sum;
}
let n = 10;
document.write(sumOfTheSeries(n));
</script>
|
Efficient Approach: Use the formula n/(n+1)
Validity of the formula:
Sum upto n terms = 1/(1*2) + 1/(2*3) + 1/(3*4) +
........ + 1/(n*(n+1))
where
1st term = 1/(1*2)
2nd term = 1/(2*3)
3rd term = 1/(3*4)
.
.
.
.
n-th term = 1/(n*(n+1))
i.e. the k-th term is of the form 1/(k*(k+1))
which can further be written as k-th term =
1/k - 1/(k+1)
So sum upto n terms can be calculated as:
(1/1 - 1/1+1) + (1/2 - 1/2+1) + (1/3 - 1/3+1)
+ ......... + (1/n-1 - /1n) + (1/n - 1/n+1)
= (1 - 1/2) + (1/2 - 1/3) + (1/3 - 1/4) + .........
+ (1/n-1 - 1/n) + (1/n - 1/n+1)
= 1 - 1/n+1
= ((n+1) - 1)/n+1
= n/n+1
Hence sum upto n terms = n/n+1
C++
#include <bits/stdc++.h>
using namespace std;
double sumOfTheSeries( int n)
{
return ( double )n / (n + 1);
}
int main()
{
int n = 10;
cout << sumOfTheSeries(n);
return 0;
}
|
Java
class demo {
public static double sumOfTheSeries( int n)
{
return ( double )n / (n + 1 );
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(sumOfTheSeries(n));
}
}
|
Python3
def sumOfTheSeries(n):
return ( float (n) / (n + 1 ))
if __name__ = = '__main__' :
n = 10
ans = sumOfTheSeries(n)
print ( round (ans, 6 ))
|
C#
using System;
class demo {
public static double sumOfTheSeries( int n)
{
return ( double )n / (n + 1);
}
public static void Main()
{
int n = 10;
Console.Write(sumOfTheSeries(n));
}
}
|
PHP
<?php
function sumOfTheSeries( $n )
{
return $n / ( $n + 1);
}
$n = 10;
echo sumOfTheSeries( $n );
?>
|
Javascript
<script>
function sumOfTheSeries(n)
{
return (n / (n + 1));
}
let n = 10;
document.write(sumOfTheSeries(n).toFixed(6));
</script>
|
Time complexity: O(1) as constant operations are performed
Contact Us