Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIFind the sum of n terms of the series 12, 105, 1008,...

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-

S_{n} = \frac{10}{9}*(10^{n}-1) + \frac{n}{2}[3n+1]

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.a*\frac{r^{n}-1}{r-1}
where a is the first term a, r is the common ratio and n is the number of terms.

A.P.\frac{n}{2}[2a +(n - 1)d]
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,

10*\frac{10^{n}-1}{10-1} + \frac{n}{2}[2*2+(n-1)*3]

\frac{10}{9}*10^{n}-1 + \frac{n}{2}[4+3n-3]

\frac{10}{9}*(10^{n}-1) + \frac{n}{2}[3n+1]

So,  S_{n} = \frac{10}{9}*(10^{n}-1) + \frac{n}{2}[3n+1]

Illustration:

Input: n = 4
Output: 11136
Explanation:
S_{n} = \frac{10}{9}*(10^{4}-1) + \frac{4}{2}[3*4+1]                
S_{n} = \frac{10}{9}*(9999) + \frac{4}{2}[13]
S_{n} = 11110 + 26
S_{n} = 11136

This gives ans 11136.

Below is the implementation of the above approach:

C++




// 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




// 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.


Python3




# 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#




// 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.


Javascript




<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.

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!

RELATED ARTICLES

Most Popular

Recent Comments