Open In App

Find the sum of n terms of the series 12, 105, 1008, 10011, …

Given a positive integer n. Find the sum of the first n term of the series 

12, 105, 1008, 10011, …..



Examples:

Input: n = 4
Output: 11136



Input: n = 7
Output: 11111187

Approach:

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

The above solution can be derived following a series of steps:

Given Series-

12 + 105 + 1008 + 10011 +…….

10 + 2 + 100 + 5 + 1000 + 8 + 10000 + 11 +……..

(10 + 100 + 1000 + 10000+……) + (2 + 5 + 8 + 11+……)     -(1)

The first term in the above equation is Geometric progression and the second term is Arithmetic progression.

G.P.
where a is the first term a, r is the common ratio and n is the number of terms.

A.P.
where a is the first term a, a is the common difference and n is the number of terms.

So after substituting values in equation of G.P. and A.P. and substituting corresponding equations in equation (1) we get,

So,  

Illustration:

Input: n = 4
Output: 11136
Explanation:
 


This gives ans 11136.

Below is the implementation of the above approach:

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
// Function to return sum of
// N term of the series
 
ll findSum(ll n)
{
    ll x = 10 * (pow(10, n) - 1) / 9;
    ll y = n * (3 * n + 1) / 2;
 
    return x + y;
}
 
// Driver Code
 
int main()
{
    ll n = 4;
    cout << findSum(n);
    return 0;
}

                    
// Java program to implement
// the above approach
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int n) {
    int x = (int)(10 * (Math.pow(10, n) - 1) / 9);
    int y = n * (3 * n + 1) / 2;
 
    return x + y;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int n = 4;
    System.out.println(findSum(n));
  }
}
 
// This code is contributed by saurabh_jaiswal.

                    
# Python program to implement
# the above approach
# include <bits/stdc++.h>
# define ll long long
 
# Function to return sum of
# N term of the series
def findSum(n):
    x = 10 * ((10 ** n) - 1) / 9
    y = n * (3 * n + 1) / 2
 
    return int(x + y)
 
# Driver Code
n = 4
print(findSum(n))
 
# This code is contributed by saurabh_jaiswal.

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

                    
<script>
      // JavaScript code for the above approach
 
      // Function to return sum of
      // N term of the series
      function findSum(n) {
 
          let x = 10 * (Math.pow(10, n) - 1) / 9;
          let y = n * (3 * n + 1) / 2;
 
          return Math.floor(x) + Math.floor(y);
      }
      // Driver Code
 
      // Get the value of N
      let N = 4;
      document.write(findSum(N));
 
// This code is contributed by Potta Lokesh
  </script>

                    

Output
11136

Time Complexity: O(logN) since it is using pow function

Auxiliary Space: O(1), since no extra space has been taken.


Article Tags :