Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmFind the sum of N terms of the series 0.1, 0.11, 0.111,...

Find the sum of N terms of the series 0.1, 0.11, 0.111, …

Given a positive integer, N. Find the sum of the first N term of the series-

 0.1, 0.11, 0.111, 0.1111, ….till N terms

Examples:

Input: N = 6
Output: 0.654321

Input: N = 1
Output: 0.1

 

Approach:

1st term = 0.1

2nd term = 0.11

3rd term = 0.111

4th term = 0.1111

.

.

Nth term = 1/9(1 – (1/10) ^ N)

The sequence is formed by using the following pattern. For any value N-

S_{N}=\frac{1}{9}(N-\frac{1}{9}(1-(\frac{1}{10})^{N}))

Derivation:

The following series of steps can be used to derive the formula to find the sum of N terms-

The series 0.1, 0.11, 0.111, …till N terms can be written as

\frac{1}{9}(0.9+0.99+0.999+...+N terms)

\frac{1}{9}((1-0.1)+(1-0.11)+(1-0.111)+...+Nterms)

\frac{1}{9}((1+1+1+...+N)-(0.1+0.11+0.111+...+0.1^{N}))

\frac{1}{9}(N-(0.1+0.11+0.111+...+0.1^{N}))                   -(1)

The series 0.1+0.11+0.111+...+0.1^{N}              is in G.P. with

First term a = 0.1  = 10-1

Common Ratio r = 10-1

Sum of G.P. for r<1 can be expressed as-

S_{N}=\frac{a*(1-r^{N})}{1-r}

Substituting the values of a and r in the equation-

S_{N}=\frac{0.1*(1-0.1^{N})}{1-0.1}

S_{N}=\frac{1}{9}(1-(\frac{1}{10})^{N})                      -(2)

Substituting the equation (2) in (1), we get-

S_{N}=\frac{1}{9}(N-\frac{1}{9}(1-(\frac{1}{10})^{N}))

Illustration:

Input: N = 6
Output: 0.654321
Explanation:
S_{N}=\frac{1}{9}(N-\frac{1}{9}(1-(\frac{1}{10})^{N}))
S_{N}=\frac{1}{9}(6-\frac{1}{9}(1-(\frac{1}{10})^{6}))
S_{N}=\frac{1}{9}(6-0.111111)
S_{N}=\frac{1}{9}(5.888889)
S_{N}=0.654321

Below is the implementation of the above approach-

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
double findSum(int N)
{
    int a = pow(10, N);
    return (double)(N * 9 * a - a + 1)
           / (81 * a);
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << findSum(N);
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
// Function to return sum of
// N term of the series
static double findSum(double N)
{
    double a = Math.pow(10, N);
    return (double)(N * 9 * a - a + 1)
           / (81 * a);
}
 
// Driver Code
    public static void main (String[] args) {
          double N = 6;
        System.out.print(findSum(N));
    }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python 3 program for the above approach
 
# Function to return sum of
# N term of the series
def findSum(N):
    a = pow(10, N)
    return (N * 9 * a - a + 1) / (81 * a)
 
# Driver Code
if __name__ == "__main__":
   
    # Value of N
    N = 6   
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#




// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static double findSum(int N)
  {
    int a = (int)Math.Pow(10, N);
    return (double)(N * 9 * a - a + 1)
      / (81 * a);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 6;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to return sum of
        // N term of the series
        function findSum(N) {
            let a = Math.pow(10, N);
            return (N * 9 * a - a + 1)
                / (81 * a);
        }
 
        // Driver Code
        let N = 6;
        document.write(findSum(N));
 
       // This code is contributed by Potta Lokesh
    </script>


 
 

Output

0.654321

 

Time Complexity: O(logN) because it is using inbuilt pow function
Auxiliary Space: O(1)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
16 Aug, 2022
Like Article
Save Article


Previous


Next


Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments