Find Nth term of the series 5, 10, 20, 40...

Last Updated : 25 Apr, 2023

Given a positive integer N, the task is to find the Nth term of the series

5, 10, 20, 40....till N terms

Examples:

Input: N = 5
Output: 80

Input: N = 3
Output: 20

 

Approach: 

1st term = 5 * (2 ^ (1 - 1))  = 5

2nd term = 5 * (2 ^ (2 - 1)) = 10

3rd term = 5 * (2 ^ (3 - 1)) = 20

4th term = 5 * (2 ^ (4 - 1)) = 40

.

.

Nth term = 5 * (2 ^ (N - 1))

The Nth term of the given series can be generalized as-

TN = (a * (r ^ (N - 1))

The following steps can be followed to derive the formula-

The series 5, 10, 20, 40....till N terms 

is in G.P. with 

first term a = 5

common ratio r = 2 because each term is double the one before it.

The Nth term of a G.P. is

TN = (a * (r ^ (N - 1))

Illustration:

Input: N = 5
Output: 80
Explanation:
TN = (a * (r ^ (N - 1))
     = (5 * (2 ^ (5 - 1))
     = (5 * 16)
     = 80

Below is the implementation of the above approach-

C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to calculate nth term
int nTerm(int a, int r, int n)
{
    return a * pow(r, n - 1);
}

// Driver code
int main()
{
    // Value of N
    int N = 5;

    // First term of the series
    int a = 5;

    // Common ratio
    int r = 2;

    cout << nTerm(a, r, N);
    return 0;
}
C
// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>

// Function to calculate nth term
int nTerm(int a, int r, int n)
{
    return a * pow(r, n - 1);
}

// Driver code
int main()
{
    // Value of N
    int N = 5;

    // First term
    int a = 5;

    // Common ratio
    int r = 2;

    printf("%d", nTerm(a, r, n));
    return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;

class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Value of N
        int N = 5;

        // First term
        int a = 5;

        // Common ratio
        int r = 2;
        System.out.println(nTerm(a, r, N));
    }

    // Function to calculate nth term
    public static int nTerm(int a, int r, int n)
    {
        return a * ((int)Math.pow(r, n - 1));
    }
}
Python3
# python3 program to implement
# the above approach

# Function to calculate nth term
def nTerm(a, r, n):

    return a * pow(r, n - 1)

# Driver code
if __name__ == "__main__":

    # Value of N
    N = 5

    # First term of the series
    a = 5

    # Common ratio
    r = 2

    print(nTerm(a, r, N))

# This code is contributed by rakeshsahni
C#
using System;

public class GFG
{
  
    // Function to calculate nth term
    public static int nTerm(int a, int r, int n)
    {
        return a * ((int)Math.Pow(r, n - 1));
    }
    static public void Main()
    {

        // Code
        // Value of N
        int N = 5;

        // First term
        int a = 5;

        // Common ratio
        int r = 2;
        Console.Write(nTerm(a, r, N));
    }
}

// This code is contributed by Potta Lokesh
JavaScript
  <script>
        // JavaScript code for the above approach 

        // Function to calculate nth term
        function nTerm(a, r, n) {
            return a * Math.pow(r, n - 1);
        }

        // Driver code

        // Value of N
        let N = 5;

        // First term of the series
        let a = 5;

        // Common ratio
        let r = 2;

        document.write(nTerm(a, r, N));


       // This code is contributed by Potta Lokesh
    </script>

Output
80

Time complexity: O(logrn) because it is using inbuilt pow function 

Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant

Comment