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


In this article, we are given a number n that denotes the 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++.

This series is different from the other because it include terms that are repeated according to their values. The series 1 + 2 + 2 + 3 + 3 + 3 + … + n consists of repeated terms that are repeated infinitely only by their value. Such illustration 1 comes once, ’2 twice’, and ’3 thrice’ up to the nth digit which appears n times.

To evaluate the sum of the series you may start by identifying an overall pattern in the sequence and then define which numbers the pattern repeats. So that you can create the series.

Following is the input/output scenario where 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.

Input

n = 5

Output

55

Explanation

Sum of series till 4th term = 12 + 22 + 32 + 42 + 52 = 1 + 2+2 + 3+3+3 + 4+4+4+4 + 5+5+5+5+5 = 55

Solution Approach

Let us discuss all possible solutions for this problem −

There are three approaches to find the sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n.

  • Using Nested Loop
  • Using Multiplication
  • Using Mathematical Formula

Using Nested Loop

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

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

Algorithm

  • Step 1 − Loop for i -> 1 to n.
  • Step 2 − With in this loop, Loop for j -> 1 to i.
  • Step 3 − Then, update sumVar, sumVar+=i;
  • Step 4 − Print sumVar.

Example

Following is the program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n using nested loop in C++ −

#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

Using Multiplication

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.

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.

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

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

Algorithm

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

Example

Following is the program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n using multiplication in C++ −

#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

Using Mathematical Formula

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.

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

Example

Following is the program to illustrate the working of our solution using Mathematical Formula in C++ −

#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

Updated on: 22-May-2024

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements