Open In App

JavaScript Program for Sum of n terms of Geometric Progression

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as a common ratio. We are going to learn how we can find the sum of n in terms of Geometric Progression in different ways using JavaScript including iteration, recursion, and reduce function.

These are the following approaches to find the sum of n terms of Geometric Progression:

Using Iteration

In this approach, we define a function and initialize two variables sum which will return the sum and nTerm to store the current sum. We use a loop to iterate n times and in each iteration, we will add the current term to the sum and update the value of nTerm by multiplying it with the common ratio r.

Example: Implementation of finding a Sum of n terms of Geometric Progression using Iteration.

JavaScript
function sumOfGeometricProgression(a, r, n) {
    let sum = 0;
    let nterm = a;

    for (let i = 0; i < n; i++) {
        sum += nterm;
        nterm *= r;
    }

    return sum;
}

const a = 3;
const r = 2;
const n = 7;

const sum = sumOfGeometricProgression(a, r, n);
console.log("Sum of", n,
    "terms of the geometric progression is  ", sum);

Output
Sum of 7 terms of the geometric progression is   381

Time Complexity: O(n)

Space Complexity: O(1)

Using Recursion

In this approach, we will define a recursive function. This function stops when several terms are equal to zero it returns the sum as zero. If n is greater than 0, recursively call the function with the updated first term (a * r), the same common ratio r, and n – 1 as the number of terms. Return the result after the recursive call stops.

Example: Implementation of finding a Sum of n terms of Geometric Progression using Recursion

JavaScript
function SumOfGPRecursive(a, r, n) {
    if (n === 0) {
        return 0;
    }
    return a + SumOfGPRecursive(a * r, r, n - 1);
}
const a = 3;
const r = 2;
const n = 7;
const sum = SumOfGPRecursive(a, r, n);
console.log("Sum of", n,
    "terms of the geometric progression is ", sum);

Output
Sum of 7 terms of the geometric progression is  381

Time Complexity: O(n)

Space Complexity: O(n)

Using Reduce Function

In this approach, we will be using the Reduce Function that calculates the sum of the first ‘n’ terms of a geometric progression. It generates an array of ‘n’ terms using the formula for the terms of a geometric progression. Then, it utilizes the ‘reduce’ function to sum up all the terms in the array. Finally, it returns the sum, which represents the total of the geometric progression. When executed with ‘a’ (first term) as 3, ‘r’ (common ratio) as 2, and ‘n’ (number of terms) as 7, it outputs “Sum of first 7 terms of GP with first term 3 and common ratio 2 is 381”.

Example: Implementation of finding a Sum of n terms of Geometric Progression using Reduce Function.

Javascript
function sumOfFirstNTermsGP(a, r, n) {

    const terms = Array.from({ length: n },
        (_, i) => a * Math.pow(r, i));

    const sum = terms.reduce((acc, curr) =>
        acc + curr, 0);

    return sum;
}

const a = 3;
const r = 2;
const n = 7;

const result = sumOfFirstNTermsGP(a, r, n);
console.log("Sum of first", n,
    "terms of GP with first term",
    a, "and common ratio", r, "is:", result);

Output
Sum of first 7 terms of GP with first term 3 and common ratio 2 is: 381

Time Complexity: O(1)

Space Complexity : O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads