Monday, January 13, 2025
Google search engine
HomeData Modelling & AIFind the sum of series 3, -6, 12, -24 . . ....

Find the sum of series 3, -6, 12, -24 . . . upto N terms

Given an integer N. The task is to find the sum upto N terms of the given series:
 

3, -6, 12, -24, … upto N terms

Examples
 

Input : N = 5
Output : Sum = 33

Input : N = 20
Output : Sum = -1048575

 

On observing the given series, it can be seen that the ratio of every term with their previous term is same which is -2. Hence the given series is a GP(Geometric Progression) series.
You can learn more about GP series here.
So, S_{n} = \frac{a(1-r^{n})}{1-r}    when r < 0.
In above GP series the first term i:e a = 3 and common ratio i:e r = (-2).
Therefore, S_{n} = \frac{3(1-(-2)^{n})}{1-(-2)}
Thus, S_{n} = 1-(-2)^{n}    .
Below is the implementation of above approach: 
 

C++




//C++ program to find sum upto N term of the series:
// 3, -6, 12, -24, .....
 
#include<iostream>
#include<math.h>
using namespace std;
//calculate sum upto N term of series
 
class gfg
{
    public:
    int Sum_upto_nth_Term(int n)
    {
        return (1 - pow(-2, n));
    }
};
// Driver code
int main()
{
    gfg g;
    int N = 5;
    cout<<g.Sum_upto_nth_Term(N);
}


Java




//Java program to find sum upto N term of the series:
// 3, -6, 12, -24, .....
 
import java.util.*;
//calculate sum upto N term of series
 
class solution
{
 
static int Sum_upto_nth_Term(int n)
{
    return (1 -(int)Math.pow(-2, n));
}
 
// Driver code
public static void main (String arr[])
{
    int N = 5;
    System.out.println(Sum_upto_nth_Term(N));
}
 
}


Python




# Python program to find sum upto N term of the series:
# 3, -6, 12, -24, .....
 
# calculate sum upto N term of series
def Sum_upto_nth_Term(n):
    return (1 - pow(-2, n))
 
# Driver code
N = 5
print(Sum_upto_nth_Term(N))


C#




// C# program to find sum upto
// N term of the series:
// 3, -6, 12, -24, .....
 
// calculate sum upto N term of series
class GFG
{
 
static int Sum_upto_nth_Term(int n)
{
    return (1 -(int)System.Math.Pow(-2, n));
}
 
// Driver code
public static void Main()
{
    int N = 5;
    System.Console.WriteLine(Sum_upto_nth_Term(N));
}
}
 
// This Code is contributed by mits


PHP




<?php
// PHP program to find sum upto
// Nth term of the series:
// 3, -6, 12, -24, .....
 
// calculate sum upto N term of series
function Sum_upto_nth_Term($n)
{
    return (1 - pow(-2, $n));
}
 
// Driver code
$N = 5;
echo (Sum_upto_nth_Term($N));
 
// This code is contributed
// by Sach_Code
?>


Javascript




<script>
// Java program to find sum upto N term of the series:
// 3, -6, 12, -24, .....
 
// calculate sum upto N term of series
function Sum_upto_nth_Term( n) {
    return (1 - parseInt( Math.pow(-2, n)));
}
 
// Driver code
 
    let N = 5;
    document.write(Sum_upto_nth_Term(N));
 
// This code is contributed by 29AjayKumar
</script>


Output: 

33

 

Time Complexity: O(logn), where n is the given integer.

Auxiliary Space: O(1), no extra space is required, so it is a constant.

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