Find the Sum of the series 1, 2, 3, 6, 9, 18, 27, 54, … till N terms
Last Updated :
19 Aug, 2022
Given a number N, the task is to find the sum of the below series till N terms.
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
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++
#include <iostream>
using namespace std;
void printSeriesSum( int N)
{
double sum = 0;
int a = 1;
int cnt = 0;
bool flag = true ;
sum += a;
while (cnt < N) {
int nextElement;
if (flag) {
nextElement = a * 2;
sum += nextElement;
flag = !flag;
}
else {
nextElement = a * 3 / 2;
sum += nextElement;
flag = !flag;
}
a = nextElement;
cnt++;
}
cout << sum << endl;
}
int main()
{
int N = 8;
printSeriesSum(N);
return 0;
}
|
Java
class GFG {
static void printSeriesSum( int N)
{
double sum = 0 ;
int a = 1 ;
int cnt = 0 ;
boolean flag = true ;
sum += a;
while (cnt < N) {
int nextElement;
if (flag == true ) {
nextElement = a * 2 ;
sum += nextElement;
flag = !flag;
}
else {
nextElement = a * 3 / 2 ;
sum += nextElement;
flag = !flag;
}
a = nextElement;
cnt++;
}
System.out.println(sum);
}
public static void main (String[] args)
{
int N = 8 ;
printSeriesSum(N);
}
}
|
Python3
def printSeriesSum(N) :
sum = 0 ;
a = 1 ;
cnt = 0 ;
flag = True ;
sum + = a;
while (cnt < N) :
nextElement = None ;
if (flag) :
nextElement = a * 2 ;
sum + = nextElement;
flag = not flag;
else :
nextElement = a * ( 3 / 2 );
sum + = nextElement;
flag = not flag;
a = nextElement;
cnt + = 1
print ( sum );
if __name__ = = "__main__" :
N = 8 ;
printSeriesSum(N);
|
C#
using System;
class GFG {
static void printSeriesSum( int N)
{
double sum = 0;
int a = 1;
int cnt = 0;
bool flag = true ;
sum += a;
while (cnt < N) {
int nextElement;
if (flag == true ) {
nextElement = a * 2;
sum += nextElement;
flag = !flag;
}
else {
nextElement = a * 3 / 2;
sum += nextElement;
flag = !flag;
}
a = nextElement;
cnt++;
}
Console.WriteLine(sum);
}
public static void Main ( string [] args)
{
int N = 8;
printSeriesSum(N);
}
}
|
Javascript
<script>
function printSeriesSum( N)
{
let sum = 0;
let a = 1;
let cnt = 0;
let flag = true ;
sum += a;
while (cnt < N) {
let nextElement;
if (flag) {
nextElement = a * 2;
sum += nextElement;
flag = !flag;
}
else {
nextElement = a * 3 / 2;
sum += nextElement;
flag = !flag;
}
a = nextElement;
cnt++;
}
document.write(sum );
}
let N = 8;
printSeriesSum(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...