Open In App

Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given the number of terms i.e. n. Find the sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms.

Examples: 

Input: 2
Output: 0.66
Explanation: sum of the series upto 2 terms: 0.6 + 0.06 = 0.66.

Input: 3
Output: 0.666
Explanation: sum of the series upto 3 terms: 0.6 + 0.06 + 0.006 = 0.666.

Naive Approach - O(n) Time and O(1) Space

Let's compute series for various n:

  • n = 1, 0.6
  • n = 2, 0.6 + 0.06 = 0.66,
  • n = 3, 0.6 + 0.06 + 0.006= 0.666
  • n = 4, 0.6 + 0.06 + 0.006 + 0.0006= 0.6666

Each subsequent term in the series involves dividing 6 by increasing powers of 10.

Below is the implementation of above approach:

C++
// C++ implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms
#include <iostream>
using namespace std;

// Function to print the sum of the series
double sumOfSeries(int n) {

    // Variable to store powers of 10
    double pow = 1;

    // Variable to store the cumulative
  	// sum of the series
    double sum = 0;

    // Loop until n becomes 0
    while (n) {

        // Multiply `pow` by 10 at each step
        pow *= 10;

        // Add 6 divided by the current value 
      	// of `pow` to `sum`
        sum += (6 / pow);

        // Decrement n
        n--;
    }

    return sum;
}

int main() {
    int n = 3;
    cout << sumOfSeries(n);
    return 0;
}
C
// C implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms
#include <stdio.h>

// Function to print the sum of the series
double sumOfSeries(int n) {

    // Variable to store powers of 10
    double pow = 1;

    // Variable to store the cumulative 
  	// sum of the series
    double sum = 0;

    // Loop until n becomes 0
    while (n) {

        // Multiply `pow` by 10 at each step
        pow *= 10;

        // Add 6 divided by the current value 
      	// of `pow` to `sum`
        sum += (6 / pow);

        // Decrement n
        n--;
    }

    return sum;
}

int main() {
  
    int n = 3;
    printf("%lf", sumOfSeries(n));
    return 0;
}
Java
// Java implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms
class GfG {

    // Function to print the sum of the series
    static double sumOfSeries(int n) {

        // Variable to store powers of 10
        double pow = 1;

        // Variable to store the cumulative sum of the
        // series
        double sum = 0;

        // Loop until n becomes 0
        while (n > 0) {

            // Multiply `pow` by 10 at each step
            pow *= 10;

            // Add 6 divided by the current value of `pow`
            // to `sum`
            sum += (6 / pow);

            // Decrement n
            n--;
        }

        return sum;
    }

    public static void main(String[] args) {
        int n = 3;
        System.out.println(sumOfSeries(n));
    }
}
Python
# Python implementation to find Sum of the series 0.6,
# 0.06, 0.006, 0.0006, ...to n terms

# Function to print the sum of the series
def sumOfSeries(n):

    # Variable to store powers of 10
    pow = 1.0

    # Variable to store the cumulative 
    # sum of the series
    sum = 0.0

    # Loop until n becomes 0
    while n > 0:

        # Multiply `pow` by 10 at each step
        pow *= 10

        # Add 6 divided by the current 
        # value of `pow` to `sum`
        sum += (6 / pow)

        # Decrement n
        n -= 1

    return sum


n = 3
print(sumOfSeries(n))
C#
// C# implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms
using System;

class GfG {
  
    // Function to print the sum of the series
    static double sumOfSeries(int n) {

        // Variable to store powers of 10
        double pow = 1;

        // Variable to store the cumulative sum of the
        // series
        double sum = 0;

        // Loop until n becomes 0
        while (n > 0) {

            // Multiply `pow` by 10 at each step
            pow *= 10;

            // Add 6 divided by the current value of `pow`
            // to `sum`
            sum += (6 / pow);

            // Decrement n
            n--;
        }

        return sum;
    }

    static void Main(string[] args) {
        int n = 3;
        Console.WriteLine(sumOfSeries(n));
    }
}
JavaScript
// Javascript implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms

// Function to print the sum of the series
function sumOfSeries(n) {

    // Variable to store powers of 10
    let pow = 1;

    // Variable to store the cumulative sum of the series
    let sum = 0;

    // Loop until n becomes 0
    while (n > 0) {

        // Multiply `pow` by 10 at each step
        pow *= 10;

        // Add 6 divided by the current value of `pow` to
        // `sum`
        sum += (6 / pow);

        // Decrement n
        n--;
    }

    return sum;
}

let n = 3;
console.log(sumOfSeries(n));

Output
0.666

Using Geometric Progression - O(log(n)) time and O(1) space

We can observe that our series is a geometric progression(gp) with a equal to 0.6 and r equal to (1/10). So we use a formula for sum of gp which goes as follows:
Let’s denote the sum by Sn

Sum-of-the-series

Below is the implementation of above approach:

C++
// C++ implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms using GP

#include <iostream>
#include <cmath> 
using namespace std;

// Function to calculate the sum of the series
double sumOfSeries(int n) {
  
    // Calculate the sum using the formula:
    // sum = (2 * (1 - 1 / 10^n))/3
    double sum = (2 * ((1 - 1 / pow(10, n)))) / 3;
    
    
    return sum;
}

int main() {
    int n = 3;
    cout<<sumOfSeries(n); 
    return 0;
}
C
// C implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms using GP
#include <stdio.h>
#include <math.h> 

// Function to calculate the sum of the series
double sumOfSeries(int n) {
  
    // Calculate the sum using the formula:
    // sum = (2 * (1 - 1 / 10^n))/3
    double sum = (2 *(1 - 1 / pow(10, n))) / 3;
    
    
    return sum;
}

int main() {
    int n = 3;
    printf("%lf\n", sumOfSeries(n));
    return 0;
}
Java
// Java implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms using GP
import java.lang.Math; 

class GfG {
  
    // Function to calculate the sum of the series
    static double sumOfSeries(int n) {
      
        // Calculate the sum using the formula:
        // sum = (2 * (1 - 1 / 10^n))/3
        double sum = (2 *(1 - 1 / Math.pow(10, n))) / 3;
        
        
        return sum;
    }

    public static void main(String[] args) {
        int n = 3; 
       System.out.println(sumOfSeries(n)); 
    }
}
Python
# Python implementation to find Sum of the series 0.6,
# 0.06, 0.006, 0.0006, ...to n terms using GP
import math

# Function to calculate the sum of the series
def sumOfSeries(n):
  
    # Calculate the sum using the formula:
    # sum = (2 * (1 - 1 / 10^n))/3
    sum = (2 *(1 - 1 / math.pow(10, n))) / 3
    
    
    return sum

n = 3 
print(sumOfSeries(n))
C#
// C# implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms using GP
using System;

class GfG {
  
    // Function to calculate the sum of the series
    static double sumOfSeries(int n) {
      
        // Calculate the sum using the formula:
        // sum = (2 * (1 - 1 / 10^n))/3
        double sum = (2 * (1 - 1 / Math.Pow(10, n))) / 3;
        
        
        return sum;
    }

    static void Main(string[] args) {
        int n = 3; 
        Console.WriteLine(sumOfSeries(n)); 
    }
}
JavaScript
// Javascript implementation to find Sum of the series 0.6,
// 0.06, 0.006, 0.0006, ...to n terms using GP

// Function to calculate the sum of the series
function sumOfSeries(n) {

    // Calculate the sum using the formula:
    // sum = (2 * (1 - 1 / 10^n))/3
    let sum = (2 *(1 - 1 / Math.pow(10, n))) / 3;
    
    return sum;
}

let n = 3; 
console.log(sumOfSeries(n)); 

Output
0.666

Explore