Open In App

Sum of first n terms of a given series 3, 6, 11, …..

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a series and a number n, the task is to find the sum of its first n terms. Below is the series:
 

3, 6, 11, 20, ….


Examples: 
 

Input: N = 2
Output: 9
The sum of first 2 terms of Series is
3 + 6 = 9

Input: N = 3
Output: 20
The sum of first 3 terms of Series is
3 + 6 + 11 = 20


 


Approach: This problem can easily solved by observing that the nth term of the series :
 

Sn = 3 + 6 + 11 + 20 … + upto nth term 
Sn = (1 + 2^1) + (2 + 2^2) + (3 + 2^3)+ (4 + 2^4) …… + upto nth term 
Sn = (1 + 2 + 3 + 4 …. + upto nth term) + ( 2^1 + 2^2 + 2^3 …… + unto nth term )


We observe that Sn is a summation of two series: AP and GP 
As we know the sum of first n terms of AP is given by 

    $$S_n=\frac{n}{2} \left(2 \times a1+(n-1) \times d\right)$$

And also the Sum of first n terms of G.P is given by 

    $$Sn=a2 \times \left(\frac{r^n-1}{r-1}\right)$$


Hence the total sum is given by sum of both AP and GP. 

    $$Total=\frac{n}{2} \left(2 \times a1+(n-1) \times d\right) +a2 \times \left(\frac{r^n-1}{r-1}\right)$$


Below is the implementation of above approach. 
 

C++

// C++ program to find sum of first n terms
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum
int calculateSum(int n)
{
    // starting number
    int a1 = 1, a2 = 2;
 
    // Common Ratio
    int r = 2;
 
    // Common difference
    int d = 1;
 
    return (n) * (2 * a1 + (n - 1) * d) / 2 + a2
               * (pow(r, n) - 1) / (r - 1);
}
 
// Driver code
int main()
{
 
    // N th term to be find
    int n = 5;
 
    // find the Sn
    cout << "Sum = " << calculateSum(n);
 
    return 0;
}

                    

Java

// Java program to find sum of first n terms
 
import java.io.*;
 
class GFG {
 
// Function to calculate the sum
static int calculateSum(int n)
{
    // starting number
    int a1 = 1, a2 = 2;
 
    // Common Ratio
    int r = 2;
 
    // Common difference
    int d = 1;
 
    return (n) * (2 * a1 + (n - 1) * d) / 2 + a2
            * (int)(Math.pow(r, n) - 1) / (r - 1);
}
 
// Driver code
    public static void main (String[] args) {
        // N th term to be find
    int n = 5;
 
    // find the Sn
    System.out.print( "Sum = " + calculateSum(n));
    }
}
// This code is contributed by inder_verma.

                    

Python3

# Python3 program to find
# sum of first n terms
def calculateSum(n):
    # First term of AP
    a1 = 1;
     
    # First term of GP
    a2 = 2;
     
    # common ratio of GP
    r = 2;
     
    # common difference Of AP
    d = 1;
    return ((n) * (2 * a1 + (n - 1) * d) /
                   2 + a2 * (pow(r, n) - 1) /
                  (r - 1));
 
# Driver Code
 
# no. of the terms
# for the sum
n = 5;
 
# Find the Sn
print ("Sum =", int(calculateSum(n)))
 
# This code is contributed
# by Surendra_Gangwar

                    

C#

// C# program to find sum
// of first n terms
using System;
 
class GFG
{
 
// Function to calculate the sum
static int calculateSum(int n)
{
    // starting number
    int a1 = 1, a2 = 2;
 
    // Common Ratio
    int r = 2;
 
    // Common difference
    int d = 1;
 
    return (n) * (2 * a1 + (n - 1) * d) / 2 + a2 *
             (int)(Math.Pow(r, n) - 1) / (r - 1);
}
 
// Driver code
public static void Main ()
{
    // N th term to be find
    int n = 5;
     
    // find the Sn
    Console.WriteLine("Sum = " + calculateSum(n));
}
}
 
// This code is contributed
// by inder_verma

                    

PHP

<?php
// PHP program to find sum of first n terms
 
// Function to calculate the sum
function calculateSum($n)
{
    // starting number
    $a1 = 1;
    $a2 = 2;
     
    // Common Ratio
    $r = 2;
     
    // Common difference
    $d = 1;
     
    return ($n) * (2 * $a1 + ($n - 1) * $d) / 2 +
            $a2 * (pow($r, $n) - 1) / ($r - 1);
}
 
// Driver code
 
// Nth term to be find
$n = 5;
 
// find the Sn
echo "Sum = ", calculateSum($n);
 
// This code is contributed
// by Shashank_Sharma
?>

                    

Javascript

<script>
 
// Javascript program to find sum of first n terms
// Function to calculate the sum
 
function calculateSum(n)
{
    // starting number
    let a1 = 1, a2 = 2;
 
    // Common Ratio
    let r = 2;
 
    // Common difference
    let d = 1;
 
    return (n) * (2 * a1 + (n - 1) * d) / 2 + a2
            * (Math.pow(r, n) - 1) / (r - 1);
}
 
// Driver code
 
    // N th term to be find
    let n = 5;
 
    // find the Sn
    document.write("Sum = " + calculateSum(n));
 
 
 
// This code is contributed by Mayank Tyagi
 
</script>

                    

Output: 
Sum = 77

 

Time Complexity: O(logn) 

Auxiliary Space: O(1)



Similar Reads

Find n terms of Fibonacci type series with given first two terms
Given first two numbers of series, find n terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms. Examples: Input: first = 5, sec = 8, n = 5 Output: 5, 8, 13, 21, 34 Input: first = 2, sec = 4, n = 5 Output: 2, 4, 6, 10, 16 Approach: The approach is
7 min read
Number of quadruples where the first three terms are in AP and last three terms are in GP
Given an array arr[] of N integers. The task is to find the number of index quadruples (i, j, k, l) such that a[i], a[j] and a[k] are in AP and a[j], a[k] and a[l] are in GP. All the quadruples have to be distinct.Examples: Input: arr[] = {2, 6, 4, 9, 2} Output: 2 Indexes of elements in the quadruples are (0, 2, 1, 3) and (4, 2, 1, 3) and correspon
9 min read
Sum of P terms of an AP if Mth and Nth terms are given
Given Mth and Nth terms of arithmetic progression. The task is to find the sum of its first p terms.Examples: Input: m = 6, n = 10, mth = 12, nth = 20, p = 5 Output:30Input:m = 10, n = 20, mth = 70, nth = 140, p = 4 Output:70 Approach: Let a is the first term and d is the common difference of the given AP. Therefore mth term = a + (m-1)d and nth te
7 min read
Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
Given an integer N. The task is to find the sum upto N terms of the given series: 2×3 + 4×4 + 6×5 + 8×6 + ... + upto n terms Examples: Input : N = 5 Output : Sum = 170 Input : N = 10 Output : Sum = 990 Let the N-th term of the series be tN. t1 = 2 × 3 = (2 × 1)(1 + 2) t2 = 4 × 4 = (2 × 2)(2 + 2) t3 = 6 × 5 = (2 × 3)(3 + 2) t4 = 8 × 6 = (2 × 4)(4 +
4 min read
Sum of the first N terms of the series 5,12, 23, 38....
Given a number N, the task is to find the sum of first N terms of the below series: Sn = 5 + 12 + 23 + 38 + … upto n terms Examples: Input: N = 2 Output: 17 5 + 12 = 17 Input: N = 4 Output: 80 5 + 12 + 23 + 38 = 78 Approach: Let, the nth term be denoted by tn. This problem can easily with the help of a general formula for these type of series,The s
4 min read
Sum of the first N terms of the series 2,10, 30, 68,....
Given a number N, the task is to find the sum of first N terms of the below series: Sn = 2 + 10 + 30 + 68 + … upto n terms Examples: Input: N = 2 Output: 12 2 + 10 = 12 Input: N = 4 Output: 40 2 + 10 + 30 + 68 = 110 Approach: Let, the nth term be denoted by tn. This problem can easily be solved by splitting each term as follows : Sn = 2 + 10 + 30 +
4 min read
Sum of the first N terms of the series 2, 6, 12, 20, 30....
Given a number N, the task is to find the sum of the first N terms of the below series: Sn = 2 + 6 + 12 + 20 + 30 … upto n terms Examples: Input: N = 2 Output: 8 Explanation: 2 + 6 = 8 Input: N = 4 Output: 40 Explanation: 2 + 6+ 12 + 20 = 40 Approach: Let, the nth term be denoted by Sn. This problem can easily be solved by splitting each term as fo
4 min read
Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
Given an integer N, the task is to find the sum of first N terms of the series: (2 * 3 * 5), (3 * 5 * 7), (4 * 7 * 9), ... Examples: Input: N = 3 Output: 387 S3 = (2 * 3 * 5) + (3 * 5 * 7) + (4 * 7 * 9) = 30 + 105 + 252 = 387Input: N = 5 Output: 1740 Approach: Let the Nth term of the series be Tn. Sum of the series can be easily found by observing
4 min read
Sum of the first N terms of XOR Fibonacci series
Given three positive integers A, B, and N where A and B are the first two terms of the XOR Fibonacci series, the task is to find the sum of the first N terms of XOR Fibonacci series which is defined as follows: F(N) = F(N – 1) ^ F(N – 2) where ^ is the bitwise XOR and F(0) is 1 and F(1) is 2. Examples: Input: A = 0, B = 1, N = 3Output: 2Explanation
18 min read
Find the sum of first N terms of the series 2, 5, 8, 11, 14..
Given a positive integer N, the task is to find the sum of the first N terms of the series 2, 5, 8, 11, 14.. Examples: Input: N = 5Output: 40 Input: N = 10Output: 155 Approach: 1st term = 2 2nd term = (2 + 3) = 5 3rd term = (5 + 3) = 8 4th term = (8 + 3) = 11 . . Nth term = (2 + (N - 1) * 3) = 3N - 1 The sequence is formed by using the following pa
5 min read