Sum of the series 5+55+555+.. up to n terms


5, 55, 555, ... is a series that can be derived from geometric progression and, thus, computed with the help of GP formulae.

Geometric progression is a type of series in which each succeeding term is the product of some specific term (ratio) with the preceding term. We will utilize the knowledge of GP, to find the sum of the given series.

Problem Statement

Given a number n, find the sum of the series 5+5+555+... up to n terms.

Examples

Input −  N = 3
Output − 595

Explanation

5 + 5 + 555 = 595.

Input −  N = 5
Output − 61716

Explanation

5 + 5 + 5 + 5 + 5 = 61716.

Solution

Let sum = 5 + 55 + 555 +… n terms. 
This is not GP, but we can relate it to GP in the following manner:
= 5(1) + 5(11) + 5(111) + … n terms
Taking 5 common:
= 5[1 + 11 + 111 + …n terms]
Divide and multiply by 9:
= 5/9[9 + 99 + 999 + … n terms] 
= 5/9[(10 – 1) + (100 – 1) + (1000 – 1) + … n terms] 
= 5/9[(10^1 – 1) + (10^2 – 1) + (10^3 – 1) + … n terms] 
= 5/9[10^1 + 10^2 + 10^3 ...n terms – (1 + 1 + … n times)] 
= 5/9[10^1 + 10^2 + 10^3 ...n terms – n] 
We will solve (10^1 + 10^2 + 10^3 ...n terms) as following:
We can observe that it is a GP, where the first term a = 10.
And the common ratio r = 10^2/10 = 10.
Hence, the GP formula:
Sum of n terms = a(r^n-1) / (r-1) (where r>1)
Putting the values of r and a:
10^1 + 10^2 + 10^3 ...n terms = 10(10^n-1)/(10-1)
Substituting the values:
 5/9[10^1 + 10^2 + 10^3 ...n terms – n]  
= 5/9[10(10n-1)/(10-1) – n] 
= 50/81(10n – 1) – 5n/9

We can use the above formula to program the solution.

Pseudo Code

main():

  • Initialize n as 5.

  • Function call : sumOfSeries(n)

sumOfSeries(int n):

  • product = 0.6172 * (pow(10,n)-1) - 0.55 * n

  • Print product

Example

Below is a C++ program for finding the sum of the series 5 + 55 + 555.....n

#include <bits/stdc++.h>
using namespace std;
// Function to calculate the
// the sum of series
int sumOfSeries(int n){
   int product = 0.6172 * (pow(10,n)-1) - 0.55 * n;
   return product;
}
int main(){
   //Input
   int n = 5;
   //Function call to calculate the sum of series
   cout << "Given Series; 5 + 55 + 555 + ..." << endl;
   int answer = sumOfSeries(n);
   //Print the answer
   cout << "Sum up to 5 terms: " << answer<< endl;
   return 0;
}

Output

Given Series; 5 + 55 + 555 + ...
Sum up to 5 terms: 61716

Analysis

Time Complexity − O(log n).

The time complexity of the program is logarithmic because of the power function.

Space Complexity − O(1)

The space complexity is constant since no extra space was used.

Conclusion

In this article, we discussed the problem of finding the sum of the series 5+55+555+… up to n terms. N is given in the input.

We solved the series using GP and wrote the pseudocode and the C++ program.

Updated on: 16-Aug-2023

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements