Find sum of the series ?3 + ?12 +.... upto N terms in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an integer value N. Our task is to find Sum of Series ?3 + ?12 + ... upto n terms.

The series is $\sqrt3 + \sqrt12 + \sqrt27 + \sqrt48 + ...$

I.e. It is a series of square roots.

Let's take an example to understand the problem,

Input : N = 3
Output : 10.3922

Explanation

$\sqrt3 + \sqrt12 + \sqrt27 = 1.7320 + 3.4641 + 5.1961 = 10.3922$

Solution Approach

A simple approach to solve the problem is finding the general term of the series and then finding the sum till n terms. And calculating the sum using formula will reduce the time to O(1).

The series is,

$\sqrt3 + \sqrt12 + \sqrt27 + \sqrt48 + ...$

Here, we have $\sqrt3$ common in all terms. On taking it as common we have,

$\Rightarrow\:\sqrt{3}(\sqrt{1}\:+\:\sqrt{4}\: +\: \sqrt{9} \:+\: \sqrt{16}\:+\:\dotsm)$

$\Rightarrow\:\sqrt{3}(1\:+\:2\:+\:3\:+\:4+\:\dotsm)$

So, the general term is,

$\mathrm{T_n\:=\:n*\sqrt{3}}$

Using this we can find the sum till n terms of the series,

$\mathrm{Sum}\:=\:\sum{n}^*\sqrt{3}$

$\mathrm{Sum}\:=\:\sqrt{3}^*\sum{n}$

$\mathrm{Sum}\:=\:(\sqrt{3})^*(n^*(n+1))/2-n$

Example

Program to illustrate the working of our solution

#include<iostream>
#include<math.h>
using namespace std;
float calcSumNTerms(float n) {
   return ((sqrt(3)) * ((n*(n+1))/2));
}
int main() {
   float n = 25;
   cout<<"The sum of series upto n terms is "<<calcSumNTerms(n);
   return 0;
}

Output

The sum of series upto n terms is 562.917
raja
Updated on 27-Jan-2022 08:43:32

Advertisements