Open In App

Find the Sum of the series 1, 2, 3, 6, 9, 18, 27, 54, … till N terms

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the sum of the below series till N terms.
 

1, 2, 3, 6, 9, 18, 27, 54, 81, 162...


Examples: 
 

Input: N = 8 
Output: 201 
1 + 2 + 3 + 6 + 9 + 18 + 27 + 54 + 81 = 201
Input: N = 12 
Output: 1821 
1 + 2 + 3 + 6 + 9 + 18 + 27 + 54 + 81 + 162 + 243 + 486 + 729 = 1821 
 


 


Approach: From the given series, find the formula for Nth term: 
 

1st term = 1
2nd term = 2 = 2 * 1
3rd term = 3 = 3/2 * 2
4th term = 6 = 2 * 3
5th term = 9 = 3/2 * 6
6th term = 18 = 2 * 9
.
.
Nth term = [2 * (N-1)th term], if N is even
           [3/2 * (N-1)th term], if N is odd


Therefore: 
 

Nth term of the series 1, 2, 3, 6, 9, 18, 27, 54, 81, 162... = \begin{cases} 2\times (N-1)^{th}\text{ term, } & \text{ if N is even } \\ \frac{3}{2}\times (N-1)^{th}\text{ term, } & \text{ if N is odd }\ \end{cases}


Then iterate over numbers in the range [1, N] to find all the terms using the above formula and compute their sum.
Approach: By observing the pattern in the given series, the next numbers of the series are alternatively multiplied by 2 and 3/2.
Below is the implementation of the above approach: 
 

C++

// C++ program for the above series
#include <iostream>
using namespace std;
 
// Function to find the sum of series
void printSeriesSum(int N)
{
    double sum = 0;
 
    int a = 1;
    int cnt = 0;
 
    // Flag to find the multiplicating
    // factor.. i.e, by 2 or 3/2
    bool flag = true;
 
    // First term
    sum += a;
 
    while (cnt < N) {
 
        int nextElement;
 
        // If flag is true, multiply by 2
        if (flag) {
            nextElement = a * 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // If flag is false, multiply by 3/2
        else {
            nextElement = a * 3 / 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // Update the previous element
        // to nextElement
        a = nextElement;
        cnt++;
    }
 
    // Print the sum
    cout << sum << endl;
}
 
// Driver Code
int main()
{
 
    int N = 8;
 
    printSeriesSum(N);
    return 0;
}

                    

Java

// Java program for the above series
class GFG {
     
    // Function to find the sum of series
    static void printSeriesSum(int N)
    {
        double sum = 0;
     
        int a = 1;
        int cnt = 0;
     
        // Flag to find the multiplicating
        // factor.. i.e, by 2 or 3/2
        boolean flag = true;
     
        // First term
        sum += a;
     
        while (cnt < N) {
     
            int nextElement;
     
            // If flag is true, multiply by 2
            if (flag == true) {
                nextElement = a * 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // If flag is false, multiply by 3/2
            else {
                nextElement = a * 3 / 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // Update the previous element
            // to nextElement
            a = nextElement;
            cnt++;
        }
     
        // Print the sum
        System.out.println(sum);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
     
        int N = 8;
     
        printSeriesSum(N);
    }
}
// This code is contributed by AnkitRai01

                    

Python3

# Python3 program for the above series
 
# Function to find the sum of series
def printSeriesSum(N) :
 
    sum = 0;
 
    a = 1;
    cnt = 0;
 
    # Flag to find the multiplicating
    # factor.. i.e, by 2 or 3/2
    flag = True;
 
    # First term
    sum += a;
 
    while (cnt < N) :
 
        nextElement = None;
 
        # If flag is true, multiply by 2
        if (flag) :
            nextElement = a * 2;
            sum += nextElement;
            flag = not flag;
 
        # If flag is false, multiply by 3/2
        else :
            nextElement = a * (3 / 2);
            sum += nextElement;
            flag = not flag;
 
        # Update the previous element
        # to nextElement
        a = nextElement;
        cnt += 1
 
    # Print the sum
    print(sum);
 
# Driver Code
if __name__ == "__main__" :
 
 
    N = 8;
 
    printSeriesSum(N);
     
    # This code is contributed by AnkitRai01

                    

C#

// C# program for the above series
using System;
 
class GFG {
     
    // Function to find the sum of series
    static void printSeriesSum(int N)
    {
        double sum = 0;
     
        int a = 1;
        int cnt = 0;
     
        // Flag to find the multiplicating
        // factor.. i.e, by 2 or 3/2
        bool flag = true;
     
        // First term
        sum += a;
     
        while (cnt < N) {
     
            int nextElement;
     
            // If flag is true, multiply by 2
            if (flag == true) {
                nextElement = a * 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // If flag is false, multiply by 3/2
            else {
                nextElement = a * 3 / 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // Update the previous element
            // to nextElement
            a = nextElement;
            cnt++;
        }
     
        // Print the sum
        Console.WriteLine(sum);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
     
        int N = 8;
     
        printSeriesSum(N);
    }
}
 
// This code is contributed by AnkitRai01

                    

Javascript

<script>
 
// javascript program for the above series
 
 
// Function to find the sum of series
function printSeriesSum( N)
{
    let sum = 0;
 
    let a = 1;
    let cnt = 0;
 
    // Flag to find the multiplicating
    // factor.. i.e, by 2 or 3/2
    let flag = true;
 
    // First term
    sum += a;
 
    while (cnt < N) {
 
        let nextElement;
 
        // If flag is true, multiply by 2
        if (flag) {
            nextElement = a * 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // If flag is false, multiply by 3/2
        else {
            nextElement = a * 3 / 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // Update the previous element
        // to nextElement
        a = nextElement;
        cnt++;
    }
 
    // Print the sum
   document.write(sum );
}
 
// Driver Code
 
    let N = 8;
 
    printSeriesSum(N);
     
// This code is contributed by todaysgaurav
 
 
</script>

                    

Output: 
201

 

Time Complexity: O(N)

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



Last Updated : 19 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads