- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find sum of the series ?3 + ?12 +.... upto N terms in C++
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