Open In App

JavaScript Program to find nth term of Geometric Progression

Last Updated : 03 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Geometric Progression is a sequence of numbers in which each term is obtained by multiplying the previous term by a constant number called the common ratio. In this article, we will learn how to find the nth term of Geometric Progression using JavaScript. We will explore various approaches that can be used to find the nth term of G.P.

Given the first term (a), common ratio (r), and an integer N of the Geometric Progression series, the task is to find the Nth term of the series.

Examples: 

Input: a = 2 r = 2, N = 4
Output: The 4th term of the series is : 16

Input: a = 2 r = 3, N = 5
Output: The 5th term of the series is : 162

Iterative Approach

In this approach, We initialize the nth term (nthTerm) with the first term (a) and multiply it by the common ratio (r) in each iteration from the second to the nth term, returning the calculated nthTerm result. This method efficiently finds the nth term of a Geometric Progression (G.P.) without using recursion.

Example: To demonstrate finding nth terms of the G.P. series using an iterative method.

JavaScript
function nthTermOfGPIterative(a, r, n) {
    let nthterm = a;
    for (let i = 2; i <= n; i++) {
        nthterm *= r;
    }
    return nthterm;
}
const a = 3;
const r = 2;
const n = 7;
const term = nthTermOfGPIterative(a, r, n);
console.log(
    "The", n, 
    "th term of the geometric progression is ", term);

Output
The 7 th term of the geometric progression is  192

Time Complexity : O(n)

Space Complexity : O(1)

Recursive Approach

In this approach, we define a recursive function that stops (i.e., base case) when n is equal to 1, returning the first term a. If n is greater than 1, the function recursively calls itself with the same first term a, common ratio r, and n – 1 as the term number. It multiplies the result of the recursive call by the common ratio r and returns the result after the recursive call stops.

Example: To demonstrate finding nth terms of the G.P. series using recursive method.

JavaScript
function nthTermOfGPRecursive(a, r, n) {
    if (n === 1) {
        return a;
    }
    return r * nthTermOfGPRecursive(a, r, n - 1);
}
const a = 3;
const r = 2;
const n = 7;
const term = nthTermOfGPRecursive(a, r, n);
console.log(`The ${n}th term of the geometric 
             progression using recursive 
             function is ${term}`);

Output
The 7 th term of the geometric progression using recursive function is  192

Time Complexity : O(n) , as function make recursive call n times.

Space Complexity : O(n) , n recursive calls are made.

Direct Formula Approach

In this approach we will use direct formula to find nth term of G.P. The formula for calculating nth term of G.P. is an = a1 × r(n-1) . We will define the function which will return the nth term of G.P. by using its formula.

Syntax:

an = a1  × r(n-1) 

Parameters
an: It represents the nth term of G.P.
a1: It is the first term of G.P.
r : It is the common ration of G.P.
n: It is the number of term which we are calculating.

Example: To demonstrate finding nth terms of the G.P. series using a function that utilizes the nth term formula to calculate the nth terms of a G.P. series and prints the result.

JavaScript
function nthTermGP(firstTerm, commonRatio, n) {
    return firstTerm * Math.pow(commonRatio, n - 1);
}
const firstTerm = 4;
const commonRatio = 2;
const n = 6;
const nthTerm = nthTermGP(firstTerm, commonRatio, n);
console.log("The", n + "th term of the G.P. is:", nthTerm);

Output
The 6th term of the G.P. is: 128

Time Complexity: O(logn), we are using power inbuilt function

Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads