Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a number n that denotes that nth term of the series. Our task is to create a Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++.

Problem description − Here, we will find the sum of series whose nth terms is n times the sum of number n. This means it is a series of square numbers.

Let’s take an example to understand the problem

Input

n = 4

Output

30

Explanation

Sum of series till 4th term = 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 + 4 = 30

Solution Approach

The most effective solution to the problem would be using the general formula for the sum of series.

But let’s discuss all possible solutions of the problem that one might think of.

The simplest solution to the problem is directly by adding numbers of series till n. This will require two nested loops, one of term and the inner one for the values in each term.

Algorithm

Initialize − sumVar = 0;

  • Step 1 − Loop for i -> 1 to n.
    • Step 1.1 − Loop for j -> 1 to i.
      • Step 1.1.1 − Update sumVar, sumVar+=i;
  • Step 2 − Print sumVar.

Program to illustrate the working of our solution

Example

 Live Demo

#include <iostream>
using namespace std;
int calcSeriesSum(int n){
   int sumVar = 0;
   for(int i = 1; i <= n; i++){
      for(int j = 1; j <= i; j++){
         sumVar += i;
      }
   }
   return sumVar;
}
int main(){
   int n = 7;
   cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n);
   return 0;
}

Output

The sum of series till 7 is 140

This solution is simple but is not effective as it has two nested loops making its time complexity of the order O(n2).

An Effective solution is based on the fact that if a number(n) is added to itself n times. Then, the result can be achieved by multiplying the number with itself.

i.e. 5+5+5+5+5 = 5*5.

So, we can use the multiplication instead of one loop to solve the problem.

Algorithm

Initialize − sumVal = 0;

  • Step 1 − loop for i -> 0 to n.
    • Step 2 − update sumVal, sumVal += (i*i)

Program to illustrate the working of our solution

Example

 Live Demo

#include <iostream>
using namespace std;
int calcSeriesSum(int n){
   int sumVar = 0;
   for(int i = 1; i <= n; i++){
      sumVar += (i*i);
   }
   return sumVar;
}
int main(){
   int n = 7;
   cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n);
   return 0;
}

Output

The sum of series till 7 is 140

The solution is better as it takes only one loop and has a time complexity of the order O(n). But it isn’t the best possible solution as the same can be done in O(1) time complexity.

The most effective solution is using a general formula for the sum of the given series.

Sum of series =

1 + 2 + 2 + 3 + 3 + 3 + …. N.

This can be made as

1 + (2+2) + (3+3+3) + … + (N+N+N..N)
1*1 + 2*2 + 3*3 + … N*N.
12 + 22 + 32 + … N2.

$Sum =\sum_{\square=1}^\square\blacksquare\square^2$

The formula for the sum of squares is n*(n+1)*(2n+1)/6. And we can find the sum using this formula too.

Program to illustrate the working of our solution

Example

 Live Demo

#include <iostream>
using namespace std;
int calcSeriesSum(int n){
   int sumVar = 0;
   sumVar = ((n*(n + 1)*( 2 * n + 1))/6 );
      return sumVar;
}
int main(){
   int n = 7;
   cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n);
   return 0;
}

Output

The sum of series till 7 is 140
raja
Published on 27-May-2020 13:15:59
Advertisements